Skip to content

Commit

Permalink
config: added links for thumbnails (#1799)
Browse files Browse the repository at this point in the history
  • Loading branch information
0einstein0 authored Aug 28, 2024
1 parent e14e18b commit b717fb7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
77 changes: 76 additions & 1 deletion invenio_rdm_records/services/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

"""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_communities.communities.records.api import Community
Expand Down Expand Up @@ -41,12 +44,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 +66,8 @@
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 werkzeug.local import LocalProxy

from invenio_rdm_records.records.processors.tiles import TilesProcessor

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


def has_image_files(record, ctx):
"""Return if the record has any image file."""
for file in record.files.entries:
file_ext = splitext(file)[1].replace(".", "").lower()
if file_ext in current_app.config["IIIF_FORMATS"]:
return True


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


def get_record_thumbnail_file(record, **kwargs):
"""Generate the URL for a record's thumbnail."""
files = record.files
default_preview = files.get("default_preview")
file_entries = files.entries
image_extensions = current_app.config["IIIF_FORMATS"]
if file_entries:
# Verify file has allowed extension and select the 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,
)
return file_key


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

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


class ThumbnailLinks(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 = {}
vars["file_key"] = get_record_thumbnail_file(record=obj)
for size in self._sizes:
vars["size"] = size
thumbnail_links[str(size)] = self._uritemplate.expand(**vars)
return thumbnail_links


# Helper link definitions
record_doi_link = ConditionalLink(
cond=is_datacite_test,
Expand Down Expand Up @@ -455,6 +525,11 @@ class RDMRecordServiceConfig(RecordServiceConfig, ConfiguratorMixin):
if_=RecordLink("{+api}/records/{id}/media-files"),
else_=RecordLink("{+api}/records/{id}/draft/media-files"),
),
"thumbnails": ThumbnailLinks(
"{+api}/iiif/record:{id}:{file_key}/full/^{size},/0/default.jpg",
sizes=LocalProxy(record_thumbnail_sizes),
when=has_image_files,
),
"archive": ConditionalLink(
cond=is_record,
if_=RecordLink(
Expand Down
2 changes: 2 additions & 0 deletions tests/resources/test_serialized_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def test_draft_links(client, draft_json, minimal_record, headers):
"access_users": f"https://127.0.0.1:5000/api/records/{pid_value}/access/users",
"access_groups": f"https://127.0.0.1:5000/api/records/{pid_value}/access/groups",
}

assert expected_links == created_draft_links == read_draft_links


Expand Down Expand Up @@ -122,6 +123,7 @@ def test_record_links(client, published_json, headers):
"access_users": f"https://127.0.0.1:5000/api/records/{pid_value}/access/users",
"access_groups": f"https://127.0.0.1:5000/api/records/{pid_value}/access/groups",
}

assert expected_links == published_record_links == read_record_links


Expand Down

0 comments on commit b717fb7

Please sign in to comment.