Skip to content

Commit

Permalink
convert all, prefix and check write access
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardKoschicek committed Sep 22, 2023
1 parent 2533ed9 commit c8a5b7c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

IIIF_ACTIVATE = False
IIIF_DIR = ''
IIIF_PREFIX = ''

# For system checks
WRITABLE_DIRS = [
Expand Down
18 changes: 12 additions & 6 deletions openatlas/display/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,11 @@ def system_warnings(_context: str, _unneeded_string: str) -> str:
warnings.append(
f"Database version {app.config['DATABASE_VERSION']} is needed but "
f"current version is {g.settings['database_version']}")
if app.config['IIIF_ACTIVATE']:
app.config['WRITABLE_DIRS'].append(app.config['IIIF_DIR'])
if app.config['IIIF_ACTIVATE'] and app.config['IIIF_DIR']:
path = Path(app.config['IIIF_DIR']) / app.config['IIIF_PREFIX']
check_write_access(path, warnings)
for path in app.config['WRITABLE_DIRS']:
if not os.access(path, os.W_OK):
warnings.append(
'<p class="uc-first">' + _('directory not writable') +
f" {str(path).replace(app.root_path, '')}</p>")
check_write_access(path, warnings)
if is_authorized('admin'):
from openatlas.models.user import User
user = User.get_by_username('OpenAtlas')
Expand All @@ -455,6 +453,14 @@ def system_warnings(_context: str, _unneeded_string: str) -> str:
f'{"<br>".join(warnings)}</div>' if warnings else ''


def check_write_access(path, warnings):
if not os.access(path, os.W_OK):
warnings.append(
'<p class="uc-first">' + _('directory not writable') +
f" {str(path).replace(app.root_path, '')}</p>")
return warnings


@app.template_filter()
def tooltip(text: str) -> str:
if not text:
Expand Down
1 change: 1 addition & 0 deletions openatlas/templates/admin/data.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ <h1 class="mb-1">{{ _('image processing')|uc_first }}</h1>
<div class="col-auto">{{ 'admin/file'|manual|safe }}</div>
<div class="col-auto">{{ _('create resized images')|button(url_for('admin_resize_images'))|safe }}</div>
<div class="col-auto">{{ _('delete orphaned resized images')|button(url_for('admin_delete_orphaned_resized_images'))|safe }}</div>
<div class="col-auto">{{ _('convert all images to iiif')|button(url_for('admin_convert_all_to_iiif'))|safe }}</div>
</div>
{% endif %}
{% if 'manager'|is_authorized %}
Expand Down
12 changes: 12 additions & 0 deletions openatlas/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from openatlas.models.settings import Settings
from openatlas.models.type import Type
from openatlas.models.user import User
from openatlas.views.file import convert_image_to_iiif


@app.route('/admin', methods=['GET', 'POST'], strict_slashes=False)
Expand Down Expand Up @@ -777,3 +778,14 @@ def get_disk_space_info() -> Optional[dict[str, Any]]:
'percent_used': percent_free,
'percent_project': percent_files,
'percent_other': 100 - (percent_files + percent_free)}


@app.route('/admin/admin_convert_all_to_iiif')
@required_group('admin')
def admin_convert_all_to_iiif() -> Response:
for entity in Entity.get_by_class('file'):
if entity.id in g.file_stats \
and g.file_stats[entity.id]['ext'] \
in app.config['ALLOWED_IMAGE_EXT']:
convert_image_to_iiif(entity.id)
return redirect(url_for('admin_index') + '#tab-data')
13 changes: 7 additions & 6 deletions openatlas/views/file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import subprocess
from pathlib import Path
from subprocess import call, run
from typing import Any, Union

from flask import g, render_template, request, send_from_directory, url_for
Expand Down Expand Up @@ -74,18 +73,20 @@ def file_add(id_: int, view: str) -> Union[str, Response]:
@app.route('/file/iiif/<int:id_>', methods=['GET'])
@required_group('contributor')
def make_iiif_available(id_: int):
convert_image_to_iiif(id_)
return redirect(url_for('view', id_=id_))


def convert_image_to_iiif(id_):
path = Path(app.config['IIIF_DIR']) / app.config['IIIF_PREFIX'] / str(id_)
command = \
(f"vips.exe tiffsave "
f"{get_file_path(id_)} {Path(app.config['IIIF_DIR']) / str(id_)} "
(f"vips.exe tiffsave {get_file_path(id_)} {path} "
f"--tile --pyramid --compression deflate "
f"--tile-width 256 --tile-height 256")
subprocess.Popen(command, shell=True)
return redirect(url_for('view', id_=id_))


@app.route('/iiif/<int:id_>', methods=['GET'])
@required_group('contributor')
def view_iiif(id_: int):
return redirect(url_for('view', id_=id_))


0 comments on commit c8a5b7c

Please sign in to comment.