image cache fix
This commit is contained in:
15
app.py
15
app.py
@@ -165,15 +165,17 @@ def scan():
|
|||||||
if p:
|
if p:
|
||||||
barcode_val, name, price, image_path = p
|
barcode_val, name, price, image_path = p
|
||||||
|
|
||||||
# Check if local cache image actually exists on disk
|
# FIX: Check if the file exists relative to the application root
|
||||||
if image_path and image_path.startswith('/static/cache/'):
|
# We lstrip('/') to convert '/static/cache/xxx.jpg' to 'static/cache/xxx.jpg'
|
||||||
relative_path = image_path.lstrip('/')
|
if image_path and image_path.startswith('/static/'):
|
||||||
if not os.path.exists(relative_path):
|
# Strip query parameters like ?t=12345 before checking disk
|
||||||
# Image lost! Attempting recovery
|
clean_path = image_path.split('?')[0].lstrip('/')
|
||||||
|
|
||||||
|
if not os.path.exists(clean_path):
|
||||||
|
# ONLY if the file is truly gone from the disk, try to recover
|
||||||
ext_data = fetch_from_openfoodfacts(barcode_val)
|
ext_data = fetch_from_openfoodfacts(barcode_val)
|
||||||
if ext_data and ext_data.get('image'):
|
if ext_data and ext_data.get('image'):
|
||||||
image_path = ext_data['image']
|
image_path = ext_data['image']
|
||||||
# Update DB with the new path (might have a different extension now)
|
|
||||||
with sqlite3.connect(DB_FILE) as conn:
|
with sqlite3.connect(DB_FILE) as conn:
|
||||||
conn.execute('UPDATE products SET image_url = ? WHERE barcode = ?', (image_path, barcode_val))
|
conn.execute('UPDATE products SET image_url = ? WHERE barcode = ?', (image_path, barcode_val))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
@@ -183,6 +185,7 @@ def scan():
|
|||||||
})
|
})
|
||||||
return jsonify({"status": "ok", "recovered": True})
|
return jsonify({"status": "ok", "recovered": True})
|
||||||
|
|
||||||
|
# If image exists OR it's a raw URL, just send what we have in the DB
|
||||||
socketio.emit('new_scan', {"barcode": barcode_val, "name": name, "price": int(price), "image": image_path})
|
socketio.emit('new_scan', {"barcode": barcode_val, "name": name, "price": int(price), "image": image_path})
|
||||||
return jsonify({"status": "ok"})
|
return jsonify({"status": "ok"})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user