|
4 | 4 |
|
5 | 5 | import csv |
6 | 6 | import logging |
| 7 | +import os |
7 | 8 |
|
8 | 9 | from collections import namedtuple |
| 10 | +from math import ceil, trunc |
9 | 11 | from pathlib import Path |
10 | 12 | from typing import List |
11 | 13 |
|
|
20 | 22 | RunStatus = namedtuple('RunStatus', ('tind_id', 'status', 'path')) |
21 | 23 |
|
22 | 24 | SUPPORTED_IMAGE_TYPES = {"image/jpeg", "image/png", "image/gif", "image/webp"} |
| 25 | +"""The supported image MIME types we will fetch.""" |
| 26 | + |
| 27 | +SIZE_LIMIT: int = 3750000 |
| 28 | +"""The upper bound for a size of image in bytes that we will fetch.""" |
| 29 | + |
| 30 | + |
| 31 | +def base64_size(s: int | float) -> int: |
| 32 | + """Determine the base64-encoded size of an object given its original size. |
| 33 | +
|
| 34 | + :param s: The original size of the object. |
| 35 | + :returns: The base64-encoded size of the object. |
| 36 | + :note: The result of this function has the same unit as its parameter. |
| 37 | + For example, passing 4.5 MiB will result in 8 (MiB). |
| 38 | + """ |
| 39 | + return int(4 * (ceil(s * 4 / 3) / 4)) |
| 40 | + |
23 | 41 |
|
24 | 42 | @dag(schedule=[to_process_csv], catchup=False, tags=["tind", "fetch", "batch-image"]) |
25 | 43 | def fetch_images(): |
@@ -59,17 +77,57 @@ def fetch_image_to_record_directory(orig_run_id: str, tind_id: str) -> RunStatus |
59 | 77 | """Fetch an image from TIND to the target record's storage directory.""" |
60 | 78 | try: |
61 | 79 | client = FetchTind(orig_run_id) |
62 | | - filemd = client.client.fetch_file_metadata(tind_id) |
63 | | - if filemd[0].get("mime") in SUPPORTED_IMAGE_TYPES: |
64 | | - path = client.download_image_file(tind_id) |
65 | | - status = "fetched" |
| 80 | + filemd = client.get_first_file_metadata(tind_id) |
| 81 | + if not filemd.get("mime") in SUPPORTED_IMAGE_TYPES: |
| 82 | + return RunStatus(tind_id=tind_id, path="", |
| 83 | + status=f"skipped: Unsupported file type {filemd.get('mime')}") |
| 84 | + |
| 85 | + target_width = width = filemd.get("width", 0) |
| 86 | + target_height = height = filemd.get("height", 0) |
| 87 | + size = filemd.get("size", 0) |
| 88 | + b64_size = base64_size(size) |
| 89 | + logger.warning("b64_size = %d, size = %d", b64_size, size) |
| 90 | + if b64_size > SIZE_LIMIT: |
| 91 | + factor = float(SIZE_LIMIT) / b64_size |
| 92 | + target_width *= factor |
| 93 | + target_height *= factor |
| 94 | + |
| 95 | + if target_width > 8000: |
| 96 | + factor = 8000.0 / width |
| 97 | + target_width *= factor |
| 98 | + target_height *= factor |
| 99 | + |
| 100 | + if target_height > 8000: |
| 101 | + factor = 8000.0 / height |
| 102 | + target_width *= factor |
| 103 | + target_height *= factor |
| 104 | + |
| 105 | + target_width = int(trunc(target_width)) |
| 106 | + target_height = int(trunc(target_height)) |
| 107 | + |
| 108 | + if (width != target_width) or (height != target_height): |
| 109 | + # downsample using IIIF |
| 110 | + path = client.download_image_from_record_sized(tind_id, target_width, target_height) |
| 111 | + |
| 112 | + # TIND resampling may cause the image to be larger than original. recalculate. |
| 113 | + # in my testing, 290827 resampled to 5998x8000 went from 2.5 MB to 4.9 MB(!) |
| 114 | + new_size = os.stat(path).st_size |
| 115 | + b64_size = base64_size(new_size) |
| 116 | + if b64_size > SIZE_LIMIT: |
| 117 | + factor = float(SIZE_LIMIT) / b64_size |
| 118 | + target_width *= factor |
| 119 | + target_height *= factor |
| 120 | + target_width = int(trunc(target_width)) |
| 121 | + target_height = int(trunc(target_height)) |
| 122 | + # Only re-download if it actually does exceed the limit. |
| 123 | + path = client.download_image_from_record_sized(tind_id, target_width, target_height) |
66 | 124 | else: |
67 | | - path = "" |
68 | | - status = f"skipped: Unsupported file type {filemd[0].get('mime')}" |
| 125 | + path = client.download_image_file(tind_id) |
69 | 126 | except Exception as ex: # pylint: disable=broad-exception-caught |
| 127 | + logger.warning("Fetcher encountered exception", exc_info=ex) |
70 | 128 | return RunStatus(tind_id=tind_id, status=f'failed: {str(ex)}', path='') |
71 | 129 |
|
72 | | - return RunStatus(tind_id=tind_id, status=status, path=path) |
| 130 | + return RunStatus(tind_id=tind_id, status="fetched", path=path) |
73 | 131 |
|
74 | 132 | @task(outlets=[fetched_csv]) |
75 | 133 | def write_status_to_fetched_csv(orig_run_id: str, records: dict[str, list[str]], |
|
0 commit comments