Skip to content

Commit

Permalink
previewer: renamed and updated image previewer
Browse files Browse the repository at this point in the history
  • Loading branch information
0einstein0 authored and slint committed Aug 30, 2024
1 parent ec941f9 commit badc173
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 45 deletions.
10 changes: 7 additions & 3 deletions invenio.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,7 @@ IIIF_TILES_STORAGE_BASE_PATH = "images/" # relative to the instance path
PREVIEWER_PREFERENCE = [
"csv_papaparsejs",
"pdfjs",
"mirador",
"iiif_simple",
"image_previewer",
"json_prismjs",
"xml_prismjs",
"mistune",
Expand All @@ -870,9 +869,14 @@ PREVIEWER_PREFERENCE = [
]
"""Preferred previewers."""

PREVIEWER_MAX_IMAGE_SIZE_BYTES = 10 * (10**6) # 10 MB
PREVIEWER_MAX_IMAGE_SIZE_BYTES = 15 * (10**6) # 15 MB
"""Maximum file size in bytes for image files."""

PREVIEWER_IMAGE_FAILED_PROCESSING_TIMEDELTA = {
"hours": 1,
}
"""Threshold time for image processing time for failed images."""

MIRADOR_PREVIEW_EXTENSIONS = [
"png",
"jp2",
Expand Down
2 changes: 1 addition & 1 deletion site/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ invenio_access.actions =
media_files_management_action = zenodo_rdm.generators:media_files_management_action
manage_external_doi_files_action = zenodo_rdm.generators:manage_external_doi_files_action
invenio_previewer.previewers =
mirador = zenodo_rdm.previewer.mirador
image_previewer = zenodo_rdm.previewer.image_previewer

[bdist_wheel]
universal = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

.mirador-iiif-simple-preview {
display: flex;
justify-content: center;
Expand All @@ -22,7 +23,7 @@
box-shadow: 0px 0px 0px 1px #A9D5DE inset, 0px 0px 0px 0px rgba(0, 0, 0, 0);
}

.image-container {
.iframe-container {
height: 100vh;
display: flex;
justify-content: center;
Expand All @@ -46,7 +47,3 @@ i.info.icon {
color: #F8FFFF;
font-weight: 600;
}

h3, p {
font-family: 'Helvetica', 'Helvetica Neue', Arial, Helvetica, sans-serif;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# ZenodoRDM is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Mirador preview."""
"""Image previewer."""

from copy import deepcopy
from datetime import datetime, timedelta, timezone
Expand All @@ -29,22 +29,17 @@ def can_preview(file):
:param file: The file to be previewed.
:returns: Boolean.
"""
# supported_extensions list needs . prefixed -
preview_extensions = current_app.config["MIRADOR_PREVIEW_EXTENSIONS"]
supported_extensions = ["." + ext for ext in preview_extensions]
# supported_extensions list for image formats
preview_extensions = current_app.config["IIIF_FORMATS"]
supported_extensions = ["." + ext for ext in preview_extensions if ext != "pdf"]

if is_pdf_previewable(file):
return True

if not file.has_extensions(*supported_extensions):
return False

metadata = file.data["metadata"]
# If any of metadata, height or width aren't present, return false
if not (metadata and "height" in metadata and "width" in metadata):
return False
if file.has_extensions(*supported_extensions):
return True

return True
return False


def preview(file):
Expand All @@ -64,37 +59,54 @@ def preview(file):
last_updated_time = datetime.fromisoformat(file.data["updated"])
current_time = datetime.now(timezone.utc)
time_diff = current_time - last_updated_time
threshold_time = timedelta(
**current_app.config["PREVIEWER_IMAGE_FAILED_PROCESSING_TIMEDELTA"]
)

# If the image tile status size is processing and the image was last updated more than an hour ago
if tile_status == "processing" and time_diff > timedelta(hours=1):
if tile_status == "processing" and time_diff > threshold_time:
# The image size is less than configured size, fall back to IIIF
if file.size < current_app.config["PREVIEWER_MAX_IMAGE_SIZE_BYTES"]:
return render_template(
"invenio_app_rdm/records/previewers/simple_image_preview.html",
css_bundles=["mirador-previewer.css"],
css_bundles=["image-previewer.css"],
file_url=file.file.links["iiif_api"],
)
# The image size is greater than configured size,
# image cannot be previewed
elif file.size > current_app.config["PREVIEWER_MAX_IMAGE_SIZE_BYTES"]:
return render_template(
"invenio_app_rdm/records/previewers/default.html",
css_bundles=["mirador-previewer.css"],
"invenio_app_rdm/records/previewers/preview_unavailable.html",
css_bundles=current_app.config[
"PREVIEWER_BASE_CSS_BUNDLES"
] # Basic bundle which includes Font-Awesome/Bootstrap
+ ["image-previewer.css"],
file=file,
)
else:
metadata = file.data.get("metadata")
width = metadata.get("width") if metadata else None
height = metadata.get("height") if metadata else None

# If metadata is missing, or if width or height are missing or smaller than the configured tile size
if (
not metadata
or not (width and height)
or width <= iiif_config["tile_width"]
or height <= iiif_config["tile_height"]
):
return render_template(
"invenio_app_rdm/records/previewers/simple_image_preview.html",
css_bundles=["image-previewer.css"],
file_url=file.uri,
)

# If the image size is smaller than 256x256, preview the default image URL instead of IIIF
if (
file.data["metadata"]["width"] <= iiif_config["tile_width"]
or file.data["metadata"]["height"] <= iiif_config["tile_height"]
):
return render_template(
"invenio_app_rdm/records/previewers/simple_image_preview.html",
css_bundles=["mirador-previewer.css"],
file_url=file.uri,
)

show_mirador = tile_status == "finished"
supported_mirador_extensions = [
"." + ext for ext in current_app.config["MIRADOR_PREVIEW_EXTENSIONS"]
]
show_mirador = tile_status == "finished" and file.has_extensions(
*supported_mirador_extensions
)
tpl_ctx["show_mirador"] = show_mirador

if show_mirador:
Expand Down Expand Up @@ -155,7 +167,7 @@ def preview(file):

return render_template(
"invenio_app_rdm/records/previewers/mirador_preview.html",
css_bundles=["mirador-previewer.css"],
css_bundles=["image-previewer.css"],
file=file,
**tpl_ctx,
)
2 changes: 1 addition & 1 deletion site/zenodo_rdm/webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"zenodo-rdm-citations": "./js/zenodo_rdm/src/citations/index.js",
"zenodo-rdm-communities-carousel": "./js/zenodo_rdm/src/communities-carousel.js",
"zenodo-rdm-blr-search": "./js/zenodo_rdm/src/blr-related-works/index.js",
"mirador-previewer": "./less/zenodo_rdm/previewer/mirador-previewer.less",
"image-previewer": "./less/zenodo_rdm/previewer/image-previewer.less",
},
dependencies={
"@babel/runtime": "^7.9.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
{%- extends config.PREVIEWER_ABSTRACT_TEMPLATE %}

{% block panel %}
<div class="container">
<div class="col-md-2 col-md-offset-3">
<h3>{{_('Image size too big to be previewed')}}</h3>
<p>{{_('Sorry, we are unfortunately not able to preview this file.')}}</p>
</div>

<div class="iframe-container">
<h3 class="ui header">
<i class="eye outline slash icon"></i>
<div class="content">
{{_('We are unable to preview the image')}}
<div class="sub header">{{_('The image file is too big to be previewed.')}}</div>
</div>
</h3>
</div>

{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{%- extends config.PREVIEWER_ABSTRACT_TEMPLATE %}

{% block panel %}
<div class="image-container">
<div class="iframe-container">
<img src="{{ file_url }}"/>
</div>
{% endblock %}

0 comments on commit badc173

Please sign in to comment.