from pathlib import Path
import hashlib, os, shutil, sqlite3, time
src = Path('/root/.codex/generated_images/01a04397-54b6-72b3-b87c-ea63493926b2/exec-6051a34e-53f2-4a99-8dc5-477149eb58af.png')
dst = Path('/opt/struktur/social-media-agent/generated_visuals/2d5bf3e8-7970-4f99-9529-92518e8a04e7_codex-image-gen-aa014-korr-02.png')
db = Path('/opt/struktur/social-media-agent/cases.db')
package_id = 'PKG-ca29ff2c-7f23-4a67-b6f0-ac373b9faebb'
if not src.is_file(): raise SystemExit(f'missing source: {src}')
if dst.exists(): raise SystemExit(f'target already exists: {dst}')
old_backup = db.with_name(db.name + '.bak.aa014_korr02_prebind')
shutil.copy2(db, old_backup)
shutil.copy2(src, dst)
data = dst.read_bytes(); sha = hashlib.sha256(data).hexdigest(); now = time.strftime('%Y-%m-%d %H:%M:%S')
con = sqlite3.connect(db); con.row_factory = sqlite3.Row
try:
row = con.execute('SELECT image_path,image_version FROM publication_packages WHERE package_id=?', (package_id,)).fetchone()
if row is None: raise SystemExit('package not found')
old_path, old_version = row['image_path'], int(row['image_version'] or 0)
con.execute('''UPDATE publication_packages SET image_status='present', image_path=?, image_created_at=?, image_generator='Codex built-in image_gen', image_version=?, image_error='', image_prompt_used=?, media_source_type='Codex built-in image_gen', media_source_ref=?, media_quality='visuelle Abnahme offen', updated_at=? WHERE package_id=?''', (str(dst), now, old_version+1, 'AA-014-KORR-02: photorealistische professionelle Feuchtemessung an Innenwand; technischer Gebäudediagnostik-Innenraum; keine touristischen Motive, kein Text, keine Logos.', str(src), now, package_id))
if con.total_changes != 1: raise SystemExit(f'update row count {con.total_changes}')
con.commit()
print('package_id=', package_id)
print('old_path=', old_path)
print('new_path=', dst)
print('version=', old_version+1)
print('bytes=', len(data))
print('sha256=', sha)
print('backup=', old_backup)
finally:
con.close()