Skip to content

Commit

Permalink
IIIF configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWatzinger committed Oct 20, 2023
1 parent 20e0731 commit ee7a870
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 40 deletions.
2 changes: 2 additions & 0 deletions openatlas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def before_request() -> None:
app.config['RESIZED_IMAGES'],
app.config['UPLOAD_PATH'],
app.config['TMP_PATH']]
if g.settings['iiif'] and g.settings['iiif_path']:
g.writable_paths.append(g.settings['iiif_path'])
if request.path.startswith('/api/'):
ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr)
if not current_user.is_authenticated \
Expand Down
6 changes: 2 additions & 4 deletions openatlas/api/endpoints/iiif.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from flask import jsonify, Response, url_for, g
from flask_restful import Resource

from openatlas import app
from openatlas.api.resources.model_mapper import get_entity_by_id
from openatlas.api.resources.util import get_license_name
from openatlas.models.entity import Entity
Expand Down Expand Up @@ -128,9 +127,8 @@ def get_manifest_version_2(id_: int) -> dict[str, Any]:


def get_metadata(entity: Entity) -> dict[str, Any]:
ext = '.tiff' if app.config['IIIF']['conversion'] \
else entity.get_file_ext()
image_url = f"{app.config['IIIF']['url']}{entity.id}{ext}"
ext = '.tiff' if g.settings['iiif_conversion'] else entity.get_file_ext()
image_url = f"{g.settings['iiif_url']}{entity.id}{ext}"
req = requests.get(f"{image_url}/info.json")
image_api = req.json()
return {'entity': entity, 'img_url': image_url, 'img_api': image_api}
Expand Down
3 changes: 1 addition & 2 deletions openatlas/display/base_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ def get_type_data(self) -> dict[str, Any]:

def add_file_tab_thumbnails(self) -> None:
if 'file' in self.tabs and current_user.settings['table_show_icons']:
if app.config['IIIF']['enabled'] \
or g.settings['image_processing']:
if g.settings['iiif'] or g.settings['image_processing']:
self.tabs['file'].table.header.insert(1, _('icon'))
for row in self.tabs['file'].table.rows:
row.insert(1, file_preview(
Expand Down
3 changes: 1 addition & 2 deletions openatlas/display/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from flask import g, url_for
from flask_babel import lazy_gettext as _

from openatlas import app
from openatlas.display.base_display import (
ActorDisplay, BaseDisplay, EventsDisplay, PlaceBaseDisplay,
ReferenceBaseDisplay, TypeBaseDisplay)
Expand Down Expand Up @@ -78,7 +77,7 @@ def add_button_others(self) -> None:
if check_iiif_activation() \
and self.entity.get_file_ext() in g.display_file_ext:
if check_iiif_file_exist(self.entity.id) \
or not app.config['IIIF']['conversion']:
or not g.settings['iiif_conversion']:
self.buttons.append(button(
_('view in IIIF'),
url_for('view_iiif', id_=self.entity.id)))
Expand Down
25 changes: 10 additions & 15 deletions openatlas/display/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ def profile_image(entity: Entity) -> str:
src = url_for('display_file', filename=path.name)
url = src
width = g.settings["profile_image_width"]
if app.config['IIIF']['enabled'] and check_iiif_file_exist(entity.id):
if g.settings['iiif'] and check_iiif_file_exist(entity.id):
url = url_for('view_iiif', id_=entity.id)
iiif_ext = '.tiff' if app.config['IIIF']['conversion'] \
iiif_ext = '.tiff' if g.settings['iiif_conversion'] \
else g.files[entity.id].suffix
src = \
f"{app.config['IIIF']['url']}{entity.id}{iiif_ext}" \
f"{g.settings['iiif_url']}{entity.id}{iiif_ext}" \
f"/full/!{width},{width}/0/default.jpg"
elif g.settings['image_processing'] and check_processed_image(path.name):
if path_ := get_file_path(
Expand Down Expand Up @@ -427,9 +427,6 @@ 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']['enabled']:
if path := app.config['IIIF']['path']:
check_write_access(path, warnings)
for path in g.writable_paths:
check_write_access(path, warnings)
if is_authorized('admin'):
Expand Down Expand Up @@ -753,26 +750,24 @@ def get_entities_linked_to_type_recursive(


def check_iiif_activation() -> bool:
iiif = app.config['IIIF']
return bool(iiif['enabled'] and os.access(Path(iiif['path']), os.W_OK))
return bool(g.settings['iiif'] and
os.access(Path(g.settings['iiif_path']), os.W_OK))


def check_iiif_file_exist(id_: int) -> bool:
if app.config['IIIF']['conversion']:
if g.settings['iiif_conversion']:
return get_iiif_file_path(id_).is_file()
return bool(get_file_path(id_))


def get_iiif_file_path(id_: int) -> Path:
ext = '.tiff' if app.config['IIIF']['conversion'] \
else g.files[id_].suffix
return Path(app.config['IIIF']['path']) / f'{id_}{ext}'
ext = '.tiff' if g.settings['iiif_conversion'] else g.files[id_].suffix
return Path(g.settings['iiif_path']) / f'{id_}{ext}'


def convert_image_to_iiif(id_: int) -> None:
compression = app.config['IIIF']['compression'] \
if app.config['IIIF']['compression'] in ['deflate', 'jpeg'] \
else 'deflate'
compression = g.settings['iiif_conversion'] \
if g.settings['iiif_conversion'] in ['deflate', 'jpeg'] else 'deflate'
vips = "vips" if os.name == 'posix' else "vips.exe"
command = \
(f"{vips} tiffsave {get_file_path(id_)} {get_iiif_file_path(id_)} "
Expand Down
13 changes: 12 additions & 1 deletion openatlas/forms/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask_wtf import FlaskForm
from wtforms import (
BooleanField, IntegerField, SelectField, StringField, FieldList)
from wtforms.validators import Email, InputRequired
from wtforms.validators import Email, InputRequired, Optional, URL

from openatlas import app
from openatlas.forms.field import SubmitField, RemovableListField
Expand Down Expand Up @@ -60,6 +60,17 @@ class MailForm(FlaskForm):
save = SubmitField(_('save'))


class IiifForm(FlaskForm):
iiif = BooleanField('IIIF')
iiif_url = StringField(_('URL'))
iiif_version = SelectField(_('version'), choices=(('2', '2'),))
iiif_conversion = SelectField(
_('conversion'),
choices=(('', _('none')), ('deflate', _('deflate')), ('jpeg', 'jpeg')))
iiif_path = StringField(_('path'))
save = SubmitField(_('apply'))


class LogForm(FlaskForm):
limit = SelectField(
_('limit'),
Expand Down
11 changes: 9 additions & 2 deletions openatlas/views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from openatlas.forms.field import SubmitField
from openatlas.forms.setting import (
ApiForm, ContentForm, FilesForm, GeneralForm, LogForm, MailForm, MapForm,
ModulesForm, SimilarForm, TestMailForm)
ModulesForm, SimilarForm, TestMailForm, IiifForm)
from openatlas.forms.util import get_form_settings, set_form_settings
from openatlas.models.content import get_content, update_content
from openatlas.models.entity import Entity
Expand Down Expand Up @@ -157,6 +157,13 @@ def admin_index(
button(_('edit'), url_for('admin_settings', category='mail'))])
if g.settings['mail']:
tabs['email'].content += display_form(form)
tabs['iiif'] = Tab(
'IIIF',
display_info(get_form_settings(IiifForm())),
buttons=[
manual('admin/iiif'),
button(_('edit'), url_for('admin_settings', category='iiif'))])

if is_authorized('manager'):
tabs['modules'] = Tab(
_('modules'),
Expand Down Expand Up @@ -328,7 +335,7 @@ def admin_delete_single_type_duplicate(
@app.route('/admin/settings/<category>', methods=['GET', 'POST'])
@required_group('manager')
def admin_settings(category: str) -> Union[str, Response]:
if category in ['general', 'mail'] and not is_authorized('admin'):
if category in ['general', 'mail', 'iiif'] and not is_authorized('admin'):
abort(403)
form_name = f"{uc_first(category)}Form"
form = getattr(
Expand Down
2 changes: 1 addition & 1 deletion openatlas/views/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,6 @@ def delete_files(id_: int) -> None:
path.unlink()
for resized_path in app.config['RESIZED_IMAGES'].glob(f'**/{id_}.*'):
resized_path.unlink()
if app.config['IIIF']['enabled'] and check_iiif_file_exist(id_):
if g.settings['iiif'] and check_iiif_file_exist(id_):
if path := get_iiif_file_path(id_):
path.unlink()
14 changes: 7 additions & 7 deletions openatlas/views/entity_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_table(view: str) -> Table:
if view == 'file':
table.order = [[0, 'desc']]
table.header = ['date'] + table.header
if (g.settings['image_processing'] or app.config['IIIF']['enabled']) \
if (g.settings['image_processing'] or g.settings['iiif']) \
and current_user.settings['table_show_icons']:
table.header.insert(1, _('icon'))
for entity in Entity.get_by_class('file', types=True):
Expand All @@ -53,8 +53,7 @@ def get_table(view: str) -> Table:
entity.get_file_size(),
entity.get_file_ext(),
entity.description]
if (g.settings['image_processing']
or app.config['IIIF']['enabled']) \
if (g.settings['image_processing'] or g.settings['iiif']) \
and current_user.settings['table_show_icons']:
data.insert(1, file_preview(entity.id))
table.rows.append(data)
Expand All @@ -79,11 +78,12 @@ def get_table(view: str) -> Table:
def file_preview(entity_id: int) -> str:
size = app.config['IMAGE_SIZE']['table']
param = "loading='lazy' alt='image' max-width='100px' max-height='100px'"
if app.config['IIIF']['enabled'] and check_iiif_file_exist(entity_id):
ext = '.tiff' if app.config['IIIF']['conversion'] \
if g.settings['iiif'] and check_iiif_file_exist(entity_id):
ext = '.tiff' if g.settings['iiif_conversion'] \
else g.files[entity_id].suffix
url = (f"{app.config['IIIF']['url']}{entity_id}{ext}"
f"/full/!100,100/0/default.jpg")
url =\
f"{g.settings['iiif_url']}{entity_id}{ext}" \
f"/full/!100,100/0/default.jpg"
return f"<img src='{url}' {param}>" \
if ext in g.display_file_ext else ''
if icon_path := get_file_path(
Expand Down
11 changes: 5 additions & 6 deletions openatlas/views/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ def make_iiif_available(id_: int) -> Response:
def view_iiif(id_: int) -> str:
return render_template(
'iiif.html',
manifest_url=
url_for(
'api.iiif_manifest',
id_=id_,
version=app.config['IIIF']['version'],
_external=True))
manifest_url=url_for(
'api.iiif_manifest',
id_=id_,
version=g.settings['iiif_version'],
_external=True))

0 comments on commit ee7a870

Please sign in to comment.