Files
dtf-system/app/printpng_validate.py
Cauê Faleiros d69e326125 feat: the proof card, machine pull and print file corrections (Dropstar, 05/11)
The proof card owns its final PNG, keeps its parent locked until the seller
answers, enters the queue first, and shares one machine with the parent.
The board no longer assigns a machine at payment; the puller records it.
A stalled print file times out with a card warning and a retry, finished
orders sort by completion, and finals are stored as PNG. The original
download carries the order number (items 0.1, 0.4 to 0.7 and 0.9).
2026-11-06 10:41:20 -03:00

59 lines
2.4 KiB
Python

"""Small, bounded header check for the final file FlexiPRINT receives.
The original upload may be PNG or PDF. A *final* file is a transparent,
8-bit RGBA PNG tagged 300 DPI. Read only header chunks before IDAT, so even
a multi-gigabyte sheet is checked without decoding or downloading the film.
"""
import struct
PNG = b'\x89PNG\r\n\x1a\n'
HEADER_BYTES = 1 << 20
def final_png_issue(data):
if not data.startswith(PNG):
return 'O arquivo final precisa ser PNG transparente a 300 DPI. Converta PDF/TIFF antes de enviar.'
at = len(PNG)
rgba = False
dpi = None
image_data = False
while at + 12 <= len(data):
size = struct.unpack('>I', data[at:at + 4])[0]
kind = data[at + 4:at + 8]
end = at + 12 + size
if kind == b'IDAT':
image_data = True
break
if end > len(data):
return 'O cabeçalho do PNG final está incompleto ou inválido.'
body = data[at + 8:at + 8 + size]
if kind == b'IHDR':
if at != 8 or size != 13:
return 'O cabeçalho do PNG final está inválido.'
width, height, depth, color, compression, filtering, interlace = struct.unpack('>IIBBBBB', body)
rgba = bool(width and height and depth == 8 and color == 6 and
compression == 0 and filtering == 0 and interlace == 0)
elif kind == b'pHYs' and size == 9:
x, y, unit = struct.unpack('>IIB', body)
if unit == 1 and x and y:
dpi = (x * .0254, y * .0254)
at = end
if not rgba:
return 'O PNG final precisa ter transparência (canal alfa RGBA de 8 bits).'
if not dpi or any(abs(axis - 300) > 1 for axis in dpi):
return 'O PNG final precisa estar marcado como 300 DPI nos dois eixos.'
if not image_data:
return 'O PNG final está incompleto: faltam os dados da imagem.'
return None
def require_final_png(storage, upload):
from fastapi import HTTPException
if not upload['name'].lower().endswith('.png'):
raise HTTPException(422, 'O arquivo final precisa ser PNG transparente a 300 DPI. Converta PDF/TIFF antes de enviar.')
response = storage.client.get_object(Bucket=storage.bucket, Key=upload['object_key'],
Range=f'bytes=0-{HEADER_BYTES - 1}')
issue = final_png_issue(response['Body'].read())
if issue:
raise HTTPException(422, issue)