-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
processors: move tiles processor to records
- Loading branch information
Showing
4 changed files
with
72 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
invenio_records_resources/services/records/processors/base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class RecordFilesProcessor: | ||
"""Base class for record files processors.""" | ||
|
||
def _can_process(self, file_record, draft, record): | ||
"""Determine if this processor can process a given record file.""" | ||
return False | ||
|
||
def _process(self, file_record, draft, record, uow=None): | ||
"""Process a record file.""" | ||
pass | ||
|
||
def __call__(self, file_record, draft, record, uow=None): | ||
if self._can_process(file_record, draft, record): | ||
self._process(file_record, draft, record, uow=uow) |
51 changes: 51 additions & 0 deletions
51
invenio_records_resources/services/records/processors/tiles.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import datetime | ||
from io import BytesIO | ||
|
||
from invenio_rdm_records.services.iiif.tasks import generate_zoomable_image | ||
|
||
from invenio_records_resources.services.records.processors.base import ( | ||
RecordFilesProcessor, | ||
) | ||
from invenio_records_resources.services.uow import TaskOp | ||
|
||
|
||
class TilesProcessor(RecordFilesProcessor): | ||
|
||
def __init__(self, valid_exts=["tif", "jpeg", "png", "jpg"]): | ||
self.valid_exts = valid_exts | ||
|
||
def _can_process(self, file_record, record, draft): | ||
if file_record.file.ext in self.valid_exts: | ||
return True | ||
|
||
def _process(self, file_record, draft, record, uow): | ||
ref = record if record.is_published else draft | ||
ref.media_files.enabled = True | ||
ref["media_files"]["enabled"] = True | ||
|
||
mf = ref.media_files.create( | ||
f"{file_record.key}.ptif", | ||
data={ | ||
"metadata": { | ||
"processor": { | ||
"type": "image-tiles", | ||
"status": "init", | ||
# TODO: could also be just `mf.updated` | ||
"updated_at": datetime.datetime.now().isoformat(), | ||
"props": {}, | ||
}, | ||
}, | ||
"access": {"hidden": False}, | ||
}, | ||
) | ||
|
||
ref.media_files.create_obj(f"{file_record.key}.ptif", BytesIO()) | ||
ref.media_files.commit(f"{file_record.key}.ptif") | ||
|
||
uow.register( | ||
TaskOp( | ||
generate_zoomable_image, | ||
record_id=ref["id"], | ||
file_key=file_record.key, | ||
) | ||
) |