Image creation and checks refactoring
https://redmine.teklia.com/issues/10642
Requires #1949 (closed)
A new arkindex.image.iiif module should be created, with functions that handle everything related to IIIF without using Django models directly. This will allow to have a single implementation of the IIIF checks that can be used for ImageServer auto-detection, image creation and status checks on existing images.
A get_image_information(url) method should make an HTTP request to the info.json for a given image URL, as Image.perform_check does, then parse the result as Image.check_from_payload does. It must return a tuple of (version, url, width, height, max_width, max_height), or raise a rest_framework.serializers.ValidationError if it fails. The version will always be set to 2 for now. The max_width and max_height are nullable.
A new ImageServer.check_options(version: int, max_width: int or None, max_height: int or None) verifies that the max_width and max_height of the server are valid against those received from an Image Information request, using the logic currently found in Image.check_from_payload. The function also currently ensures that version is equal to 2.
Image.check_from_payload is merged into Image.perform_check. The method now:
- calls
get_image_information; - calls
ImageServer.check_optionswith the results; - updates the image's
width,heightandpath; - resizes the polygons if necessary;
- updates the
status.
The order of operations in CreateIIIFURL now changes to prepare for IIIF 2/3 autodetection:
-
Attempt to find an existing
ImageServer, without creating one automatically; -
If an
ImageServeralready exists:- If the
widthandheightwere set, validate that it allows external validation; - Build the image's
pathand check that it does not already exist;
- If the
-
If an
ImageServerdoes not exist andwidthandheightwere set, creating a server will have it not allow external validation, so an error can returned immediately. -
If an
ImageServerdoes not exist, or ifwidthandheightwere not set:-
Call
get_image_information. -
If an
ImageServeralready exists:- if the
pathreturned byget_image_informationis different than the URL, check for duplicates again; - call
ImageServer.check_optionswith the data returned byget_image_information.
- if the
-
Otherwise, try to create an
ImageServeronly now, using the data fromget_image_informationand the URL autodetection;
-
-
Create a new image on the server, with its
statusset tocheckedif we calledget_image_information; -
If we did not call
get_image_information, schedule an async check to run after the transaction completes.