Files

30 lines
2.4 KiB
Python

from pathlib import Path
import hashlib,json,subprocess,zipfile,collections
from PIL import Image
P=Path(__file__).resolve().parents[2];D=P/'docs/rebuild-v2';public=P/'web-front/public';digest=lambda f:hashlib.sha256(f.read_bytes()).hexdigest()
public_by_hash={digest(f):'/'+f.relative_to(public).as_posix() for f in public.rglob('*') if f.is_file()}
rows=json.loads((D/'media-inventory.json').read_text())
for row in rows:
if row['path']=='/download/svk-002-mn.pdf':row.update(rawFile='docs/rebuild-v2/raw/svk002-review-retry-0.bin',reason=None)
f=P/row['rawFile'] if row.get('rawFile') else None
row['publicDestination']=None
if row.get('reason') and row.get('fetchStatus')=='excluded':row['validation']='excluded-original-commercial-or-runtime';continue
if not f or not f.is_file() or not f.stat().st_size:row['validation']='unresolved-no-complete-bytes';continue
row.update(bytes=f.stat().st_size,sha256=digest(f));valid=False
try:
suffix=Path(row['path']).suffix.lower()
if suffix in ['.jpg','.jpeg','.gif','.png','.ico']:
with Image.open(f) as im:im.load();row['imageDimensions']=list(im.size);valid=True
elif suffix=='.pdf':
r=subprocess.run(['pdfinfo',str(f)],capture_output=True,text=True);valid=r.returncode==0 and 'Pages:' in r.stdout;row['pdfinfoExit']=r.returncode
elif suffix=='.zip':
with zipfile.ZipFile(f) as z:valid=z.testzip() is None;row['zipMembers']=z.namelist()
else:row['validation']='downloaded-needs-format-specific-review';continue
except Exception as e:row['validationError']=str(e)
if valid:
row['publicDestination']=public_by_hash.get(row['sha256']);row['validation']='validated-published-exact-bytes' if row['publicDestination'] else 'validated-unpublished'
else:row['validation']='unresolved-invalid-or-truncated';row['fetchStatus']='unresolved-invalid'
(D/'media-inventory.json').write_text(json.dumps(rows,ensure_ascii=False,indent=2)+'\n')
(D/'evidence/media-validation.json').write_text(json.dumps(dict(resources=len(rows),counts=dict(collections.Counter(x['validation'] for x in rows)),publicFiles=len(public_by_hash),scope='Primary domain136resourceCDXkeys; external sample host separately recorded in sample-gallery-inventory.json',note='No software, firmware or script executed; original archive members inspected only. GPSsampleZIP is truncated; four complete JPEGs salvaged by memberCRC, not fullZIPrecovery.'),ensure_ascii=False,indent=2)+'\n')
print(collections.Counter(x['validation'] for x in rows))