Skip to content

Commit

Permalink
config: added links for thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
0einstein0 committed Aug 21, 2024
1 parent fe40e25 commit 498514d
Showing 1 changed file with 80 additions and 1 deletion.
81 changes: 80 additions & 1 deletion invenio_rdm_records/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@

"""RDM Record Service."""

import itertools
from copy import deepcopy
from os.path import splitext
from pathlib import Path

from flask import current_app
from invenio_app_rdm.records_ui.previewer.iiif_simple import (
previewable_extensions as image_extensions,
)
from invenio_communities.communities.records.api import Community
from invenio_drafts_resources.services.records.components import (
DraftMediaFilesComponent,
Expand Down Expand Up @@ -41,12 +47,20 @@
SearchOptionsMixin,
ServiceConfig,
)
from invenio_records_resources.services.base.links import Link, NestedLinks
from invenio_records_resources.services.base.links import (
Link,
NestedLinks,
preprocess_vars,
)
from invenio_records_resources.services.files.links import FileLink
from invenio_records_resources.services.files.schema import FileSchema
from invenio_records_resources.services.records.config import (
RecordServiceConfig as BaseRecordServiceConfig,
)
from invenio_records_resources.services.records.links import (
RecordLink,
pagination_links,
)
from invenio_records_resources.services.records.params import (
FacetsParam,
PaginationParam,
Expand All @@ -55,6 +69,7 @@
from invenio_requests.services.requests import RequestItem, RequestList
from invenio_requests.services.requests.config import RequestSearchOptions
from requests import Request
from werkzeug.exceptions import NotFound

from invenio_rdm_records.records.processors.tiles import TilesProcessor

Expand Down Expand Up @@ -142,6 +157,36 @@ def lock_edit_published_files(service, identity, record=None, draft=None):
return True


def record_thumbnail_sizes():
"""Return configured sizes for thumbnails."""
return current_app.config["APP_RDM_RECORD_THUMBNAIL_SIZES"]


def record_thumbnail(size, record=None, **kwargs):
"""Generate the URL for a record's thumbnail."""
# Verify against allowed thumbnail sizes
if size not in current_app.config["APP_RDM_RECORD_THUMBNAIL_SIZES"]:
raise NotFound("Thumbnail size not allowed")

files = record.files
default_preview = files.get("default_preview")
file_entries = files.entries
if file_entries:
# Verify file has allowed extension and selects default preview file if present else the first valid file
file_key = next(
(
key
for key in itertools.chain([default_preview], file_entries)
if key and Path(key).suffix[1:] in image_extensions
),
None,
)
if file_key:
return f"{file_key}/full/{size},/0/default.png"

raise NotFound("Thumbnail not found")


class RecordPIDLink(Link):
"""Record PID link."""

Expand Down Expand Up @@ -295,6 +340,36 @@ class RDMFileRecordServiceConfig(FileServiceConfig, ConfiguratorMixin):
file_schema = FileSchema


class RDMThumbnailLink(RecordLink):
"""RDM thumbnail links dictionary."""

def __init__(self, *args, sizes=None, **kwargs):
"""Constructor."""
self._sizes = sizes
super().__init__(*args, **kwargs)

def expand(self, obj, context):
"""Expand the thumbs size dictionary of URIs."""
vars = {}
vars.update(deepcopy(context))
self.vars(obj, vars)
if self._vars_func:
self._vars_func(obj, vars)
vars = preprocess_vars(vars)

thumbnail_links = {}
for size in self._sizes():
try:
thumbnail_url = record_thumbnail(size, record=obj)
thumbnail_links[str(size)] = (
self._uritemplate.expand(**vars) + thumbnail_url
)
except NotFound:
# Handle the case where the thumbnail does not exist
thumbnail_links[str(size)] = None
return thumbnail_links


# Helper link definitions
record_doi_link = ConditionalLink(
cond=is_datacite_test,
Expand Down Expand Up @@ -455,6 +530,10 @@ class RDMRecordServiceConfig(RecordServiceConfig, ConfiguratorMixin):
if_=RecordLink("{+api}/records/{id}/media-files"),
else_=RecordLink("{+api}/records/{id}/draft/media-files"),
),
"thumbnails": RDMThumbnailLink(
"{+api}/iiif/record:{id}:",
sizes=record_thumbnail_sizes,
),
"archive": ConditionalLink(
cond=is_record,
if_=RecordLink(
Expand Down

0 comments on commit 498514d

Please sign in to comment.