adacam-odc: purge uploaded images after 7 days (purge_uploaded_images)

This commit is contained in:
kayos 2026-03-24 13:34:11 -07:00
parent 4949d793b5
commit 45e491ce94

View file

@ -787,6 +787,56 @@ def purge_forwarded_detections():
return 0
def purge_uploaded_images():
"""
Delete image files from IMAGES_DIR that have already been uploaded to ADAMaps.
Only deletes files:
1. Whose detection id <= last_image_id cursor (already uploaded)
2. Older than DETECTION_KEEP_DAYS
Gets the list of uploaded image filenames from SQLite, then removes the files.
"""
keep_ms = DETECTION_KEEP_DAYS * 86400 * 1000
cutoff_ms = int(time.time() * 1000) - keep_ms
with forwarder_state_lock:
cursor_img_id = forwarder_state.get("last_image_id", 0)
if cursor_img_id == 0:
return 0
deleted = 0
try:
db_path = ODC_DB_PATH
if not db_path.exists():
return 0
conn = sqlite3.connect(str(db_path))
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute(
"SELECT image_name FROM landmarks WHERE id <= ? AND ts < ? AND image_name IS NOT NULL",
(cursor_img_id, cutoff_ms)
)
rows = cur.fetchall()
conn.close()
for row in rows:
img_path = IMAGES_DIR / row["image_name"]
try:
if img_path.exists():
img_path.unlink()
deleted += 1
except Exception as e:
fwd_logger.debug(f"[purge] Could not delete image {img_path}: {e}")
if deleted > 0:
fwd_logger.info(f"[purge] Deleted {deleted} uploaded images older than {DETECTION_KEEP_DAYS}d from {IMAGES_DIR}")
return deleted
except Exception as e:
fwd_logger.warning(f"[purge] Image purge failed: {e}")
return 0
def forwarder_process_cycle() -> Tuple[int, int]:
"""
One processing cycle: forward new detections + upload images.
@ -934,9 +984,10 @@ def forwarder_process_cycle() -> Tuple[int, int]:
save_forwarder_state()
# ── 3. Purge old forwarded detections ────────────────────────────────────
# ── 3. Purge old forwarded detections + images ───────────────────────────
if det_sent > 0 or img_uploaded > 0:
purge_forwarded_detections()
purge_uploaded_images()
return det_sent, img_uploaded