Image creation and checks refactoring

Erwan Rouchet
2025-10-27T20:23:56Z

https://redmine.teklia.com/issues/10642

Requires #1949

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 i...

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:

1. calls get_image_information;

2. calls ImageServer.check_options with the results;

3. updates the image's width, height and path;

4. resizes the polygons if necessary;

5. updates the status.

The order of operations in CreateIIIFURL now changes to prepare for IIIF 2/3 autodetection:

1. Attempt to find an existing ImageServer, without creating one automatically;

2. If an ImageServer already exists:

If the width and height were set, validate that it allows external validation;

Build the image's path and check that it does not already exist;

3. If an ImageServer does not exist and width and height were set, creating a server will have it not allow external validation, so an error can returned immediately.

4. If an ImageServer does not exist, or if width and height were not set:

1. Call get_image_information.

2. If an ImageServer already exists:

1. if the path returned by get_image_information is different than the URL, check for duplicates again;

2. call ImageServer.check_options with the data returned by get_image_information.

3. Otherwise, try to create an ImageServer only now, using the data from get_image_information and the URL autodetection;

5. Create a new image on the server, with its status set to checked if we called get_image_information;

6. If we did not call get_image_information, schedule an async check to run after the transaction completes.