fix: a PDF's resolution is worked out at the scan, not at checkout
create_quote downloaded every PDF sheet (up to 150 MB) and parsed it inside the customer's request, behind nginx's 30 s. The worker's scan already reads every upload before the Site lets the customer continue, so it now stores the PDF's DPI on the upload (uploads.pdf_checked, pdf_dpi) and the checkout reads the number. An upload scanned before this, or whose work-out failed, is read at checkout as before (Critical Corrections 2, step 2). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -15,8 +15,10 @@ again, by the Site's own rules:
|
||||
is only vectors (web/site-pdf.js, dpiDasImagens and resumoDpi).
|
||||
|
||||
Images are measured from their first bytes, never decoded. A PDF is opened
|
||||
only up to PDF_MAX_BYTES, the size the Site itself analyses. A grade that
|
||||
cannot be recomputed is None; the caller decides what that means.
|
||||
only up to PDF_MAX_BYTES, the size the Site itself analyses. Its DPI is worked
|
||||
out when the scan finishes and stored on the upload row (pdf_checked), so
|
||||
checkout uses the stored number and opens the file only when it is absent. A
|
||||
grade that cannot be recomputed is None; the caller decides what that means.
|
||||
"""
|
||||
import math
|
||||
import os
|
||||
@@ -130,6 +132,16 @@ def pdf_dpi(path):
|
||||
return js_round(sum(dpi * area for dpi, area in found) / total)
|
||||
|
||||
|
||||
def stored_pdf_dpi(storage, row):
|
||||
"""A stored PDF's DPI, read once; None when it is above PDF_MAX_BYTES."""
|
||||
if row['size'] > PDF_MAX_BYTES:
|
||||
return None
|
||||
with tempfile.NamedTemporaryFile(suffix='.pdf') as handle:
|
||||
storage.client.download_fileobj(storage.bucket, row['object_key'], handle)
|
||||
handle.flush()
|
||||
return pdf_dpi(handle.name)
|
||||
|
||||
|
||||
class Files:
|
||||
"""The uploaded files, read from storage as little as needed."""
|
||||
|
||||
@@ -150,13 +162,10 @@ class Files:
|
||||
def pdf_dpi(self, row):
|
||||
key = row['object_key']
|
||||
if key not in self._dpis:
|
||||
if row['size'] > PDF_MAX_BYTES:
|
||||
self._dpis[key] = None
|
||||
if row.get('pdf_checked'):
|
||||
self._dpis[key] = row['pdf_dpi']
|
||||
else:
|
||||
with tempfile.NamedTemporaryFile(suffix='.pdf') as handle:
|
||||
self.storage.client.download_fileobj(self.storage.bucket, key, handle)
|
||||
handle.flush()
|
||||
self._dpis[key] = pdf_dpi(handle.name)
|
||||
self._dpis[key] = stored_pdf_dpi(self.storage, row)
|
||||
return self._dpis[key]
|
||||
|
||||
|
||||
|
||||
4
app/migrations/202611101200_upload-pdf-dpi.sql
Normal file
4
app/migrations/202611101200_upload-pdf-dpi.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
-- The PDF's resolution, worked out at the scan (Critical Corrections 2): pdf_checked
|
||||
-- false = not worked out yet (checkout reads as before), NULL pdf_dpi with it true = unreadable.
|
||||
ALTER TABLE dtf_local.uploads ADD COLUMN IF NOT EXISTS pdf_checked boolean NOT NULL DEFAULT false;
|
||||
ALTER TABLE dtf_local.uploads ADD COLUMN IF NOT EXISTS pdf_dpi integer;
|
||||
@@ -8,6 +8,7 @@ are those of the format its name claims (a PNG that really is a PNG, not a
|
||||
program renamed .png). That is the check the client chose for large files; it
|
||||
does not look for malware inside a valid file.
|
||||
"""
|
||||
import logging
|
||||
import re
|
||||
import socket
|
||||
import struct
|
||||
@@ -16,6 +17,7 @@ from fastapi import HTTPException
|
||||
from .core.auth import audit
|
||||
from .core.db import connect
|
||||
from .core.limits import scan_limit_bytes
|
||||
from .grade_check import stored_pdf_dpi
|
||||
|
||||
def require_clean(row):
|
||||
if not row['complete'] or row['scan_state'] != 'clean':
|
||||
@@ -144,10 +146,21 @@ def scan_one(storage, scanner=None):
|
||||
finally:stream.close()
|
||||
except Exception:
|
||||
state,reason='error','Malware scanner unavailable; file remains blocked'
|
||||
# A PDF's resolution is worked out here, once, so checkout never reads
|
||||
# it (Critical Corrections 2). A failure leaves it for the fallback.
|
||||
checked,pdf_dpi_value=False,None
|
||||
if state=='clean' and name.lower().endswith('.pdf'):
|
||||
try:
|
||||
pdf_dpi_value=stored_pdf_dpi(storage,row)
|
||||
checked=True
|
||||
except Exception:
|
||||
logging.exception('Could not work out the PDF DPI of upload %s',row['id'])
|
||||
c.execute("""UPDATE dtf_local.uploads SET name=%s,scan_state=%s,scan_reason=%s,scanned_at=now(),
|
||||
scan_after=now()+interval '1 minute',
|
||||
pdf_checked=CASE WHEN %s THEN true ELSE pdf_checked END,
|
||||
pdf_dpi=CASE WHEN %s THEN %s ELSE pdf_dpi END,
|
||||
expires_at=CASE WHEN %s IN ('rejected','error') THEN LEAST(expires_at,now()+interval '3 days') ELSE expires_at END
|
||||
WHERE id=%s""",(name,state,reason,state,row['id']))
|
||||
WHERE id=%s""",(name,state,reason,checked,checked,pdf_dpi_value,state,row['id']))
|
||||
audit('artwork_scan', upload=str(row['id']), result=state, c=c)
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user