feat: a one-off job makes the artwork pictures for orders from before them
The customer's browser makes each upload's picture, so older orders show "sem prévia" on the Kanban. ops.backfill_thumbnails, run once from the worker's console, reads each PNG, JPEG or WebP an order or live quote still refers to and stores a WebP the Site's size. PDFs and CorelDRAW files need a renderer the server does not have and keep the layout drawing; images over 100 million pixels are skipped so the worker's memory stays bounded. Running it again only looks at uploads that still have no picture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
71
tests/thumbnail_backfill_test.py
Normal file
71
tests/thumbnail_backfill_test.py
Normal file
@@ -0,0 +1,71 @@
|
||||
"""Old orders get their artwork picture from the stored image, once.
|
||||
|
||||
Run inside the API container: python -m tests.thumbnail_backfill_test
|
||||
"""
|
||||
import io
|
||||
from uuid import uuid4
|
||||
|
||||
from PIL import Image
|
||||
from psycopg.types.json import Jsonb
|
||||
|
||||
from app.core import db
|
||||
from ops import backfill_thumbnails
|
||||
|
||||
|
||||
def png(width, height):
|
||||
out = io.BytesIO()
|
||||
Image.new('RGBA', (width, height), (255, 0, 0, 128)).save(out, 'PNG')
|
||||
return out.getvalue()
|
||||
|
||||
|
||||
class Storage:
|
||||
"""Objects by key, written where the worker asks."""
|
||||
def __init__(self, objects):
|
||||
self.objects, self.fetched = objects, []
|
||||
|
||||
def fetch(self, key, path):
|
||||
"""Other uploads in a shared database are not this test's: they fail like unreadable files."""
|
||||
self.fetched.append(key)
|
||||
with open(path, 'wb') as handle:
|
||||
handle.write(self.objects[key])
|
||||
|
||||
|
||||
def run():
|
||||
owner, quote, order = uuid4(), uuid4(), uuid4()
|
||||
image, drawing = uuid4(), uuid4()
|
||||
files = {image: ('folha.png', png(1200, 3000)), drawing: ('arte.cdr', b'CDR')}
|
||||
storage = Storage({f'backfill/{uid}': content for uid, (_, content) in files.items()})
|
||||
try:
|
||||
with db.connect() as c:
|
||||
for uid, (name, content) in files.items():
|
||||
c.execute('''INSERT INTO dtf_local.uploads(id,owner,name,size,object_key,multipart_id,complete,
|
||||
expires_at,scan_state) VALUES(%s,%s,%s,%s,%s,'m',true,now()+interval '1 day','clean')''',
|
||||
(uid, owner, name, len(content), f'backfill/{uid}'))
|
||||
items = [{'mode': 'file', 'uploads': [str(image), str(drawing)]}]
|
||||
c.execute('''INSERT INTO dtf_local.quotes(id,owner,request_key,request_hash,draft)
|
||||
VALUES(%s,%s,%s,'backfill-fixture',%s)''', (quote, owner, uuid4(), Jsonb({'items': items})))
|
||||
c.execute('INSERT INTO dtf_local.orders(id,quote_id,owner,snapshot,payment) VALUES(%s,%s,%s,%s,%s)',
|
||||
(order, quote, owner, Jsonb({'items': items, 'total_cents': 1490}), Jsonb({'provider': 'teste'})))
|
||||
lines = []
|
||||
assert backfill_thumbnails.main([], out=lines.append, storage=storage) == 0
|
||||
with db.connect() as c:
|
||||
made = c.execute('SELECT mime,data FROM dtf_local.upload_thumbnails WHERE upload_id=%s', (image,)).fetchone()
|
||||
assert made and made['mime'] == 'image/webp'
|
||||
with Image.open(io.BytesIO(bytes(made['data']))) as picture:
|
||||
assert picture.format == 'WEBP' and picture.size[0] <= 480 and picture.size[1] <= 720, picture.size
|
||||
assert not c.execute('SELECT 1 FROM dtf_local.upload_thumbnails WHERE upload_id=%s', (drawing,)).fetchone()
|
||||
# The CDR is never fetched; a second run does not fetch the image again.
|
||||
assert storage.fetched.count(f'backfill/{image}') == 1 and f'backfill/{drawing}' not in storage.fetched
|
||||
backfill_thumbnails.main([], out=lines.append, storage=storage)
|
||||
assert storage.fetched.count(f'backfill/{image}') == 1
|
||||
print('PASS: old orders get a WebP picture from a stored PNG; drawings are skipped; a rerun repeats nothing')
|
||||
finally:
|
||||
with db.connect() as c:
|
||||
c.execute('DELETE FROM dtf_local.upload_thumbnails WHERE upload_id=ANY(%s)', (list(files),))
|
||||
c.execute('DELETE FROM dtf_local.orders WHERE id=%s', (order,))
|
||||
c.execute('DELETE FROM dtf_local.quotes WHERE id=%s', (quote,))
|
||||
c.execute('DELETE FROM dtf_local.uploads WHERE id=ANY(%s)', (list(files),))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run()
|
||||
Reference in New Issue
Block a user