fix custom images cache
This commit is contained in:
@@ -81,7 +81,6 @@ python app.py
|
|||||||
|
|
||||||
## 📋 TODOs?
|
## 📋 TODOs?
|
||||||
- Better admin registration(?)
|
- Better admin registration(?)
|
||||||
- Cache user edited pictures
|
|
||||||
- clear edited fields after scanning of new product
|
- clear edited fields after scanning of new product
|
||||||
|
|
||||||
## 🥼 Food Datasets
|
## 🥼 Food Datasets
|
||||||
|
|||||||
40
app.py
40
app.py
@@ -5,6 +5,7 @@ from flask import Flask, render_template, request, jsonify, redirect, url_for, f
|
|||||||
from flask_socketio import SocketIO, emit
|
from flask_socketio import SocketIO, emit
|
||||||
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
|
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
|
||||||
from werkzeug.security import generate_password_hash, check_password_hash
|
from werkzeug.security import generate_password_hash, check_password_hash
|
||||||
|
import mimetypes
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['SECRET_KEY'] = 'seki_super_secret_key_99' # Change this if you have actual friends
|
app.config['SECRET_KEY'] = 'seki_super_secret_key_99' # Change this if you have actual friends
|
||||||
@@ -45,20 +46,31 @@ def load_user(user_id):
|
|||||||
user = conn.execute('SELECT id, username FROM users WHERE id = ?', (user_id,)).fetchone()
|
user = conn.execute('SELECT id, username FROM users WHERE id = ?', (user_id,)).fetchone()
|
||||||
return User(user[0], user[1]) if user else None
|
return User(user[0], user[1]) if user else None
|
||||||
|
|
||||||
# --- HELPERS ---
|
|
||||||
def download_image(url, barcode):
|
def download_image(url, barcode):
|
||||||
if not url or not url.startswith('http'): return url
|
if not url or not url.startswith('http'):
|
||||||
local_filename = f"{barcode}.jpg"
|
return url
|
||||||
local_path = os.path.join(CACHE_DIR, local_filename)
|
|
||||||
if os.path.exists(local_path): return f"/static/cache/{local_filename}"
|
|
||||||
try:
|
try:
|
||||||
headers = {'User-Agent': 'SekiPOS/1.0'}
|
headers = {'User-Agent': 'SekiPOS/1.2'}
|
||||||
r = requests.get(url, headers=headers, stream=True, timeout=5)
|
# Use stream=True to check headers before downloading the whole file
|
||||||
if r.status_code == 200:
|
with requests.get(url, headers=headers, stream=True, timeout=5) as r:
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
|
# Detect extension from Content-Type header
|
||||||
|
content_type = r.headers.get('content-type')
|
||||||
|
ext = mimetypes.guess_extension(content_type) or '.jpg'
|
||||||
|
|
||||||
|
local_filename = f"{barcode}{ext}"
|
||||||
|
local_path = os.path.join(CACHE_DIR, local_filename)
|
||||||
|
|
||||||
|
# Save the file
|
||||||
with open(local_path, 'wb') as f:
|
with open(local_path, 'wb') as f:
|
||||||
for chunk in r.iter_content(1024): f.write(chunk)
|
for chunk in r.iter_content(chunk_size=8192):
|
||||||
|
f.write(chunk)
|
||||||
|
|
||||||
return f"/static/cache/{local_filename}"
|
return f"/static/cache/{local_filename}"
|
||||||
except: pass
|
except Exception as e:
|
||||||
|
print(f"Download failed: {e}")
|
||||||
return url
|
return url
|
||||||
|
|
||||||
def fetch_from_openfoodfacts(barcode):
|
def fetch_from_openfoodfacts(barcode):
|
||||||
@@ -119,11 +131,17 @@ def index():
|
|||||||
@login_required
|
@login_required
|
||||||
def upsert():
|
def upsert():
|
||||||
d = request.form
|
d = request.form
|
||||||
|
barcode = d['barcode']
|
||||||
|
original_url = d['image_url']
|
||||||
|
|
||||||
|
# Process the URL: Download if external, or keep if already local/empty
|
||||||
|
final_image_path = download_image(original_url, barcode)
|
||||||
|
|
||||||
with sqlite3.connect(DB_FILE) as conn:
|
with sqlite3.connect(DB_FILE) as conn:
|
||||||
conn.execute('''INSERT INTO products (barcode, name, price, image_url) VALUES (?,?,?,?)
|
conn.execute('''INSERT INTO products (barcode, name, price, image_url) VALUES (?,?,?,?)
|
||||||
ON CONFLICT(barcode) DO UPDATE SET name=excluded.name,
|
ON CONFLICT(barcode) DO UPDATE SET name=excluded.name,
|
||||||
price=excluded.price, image_url=excluded.image_url''',
|
price=excluded.price, image_url=excluded.image_url''',
|
||||||
(d['barcode'], d['name'], d['price'], d['image_url']))
|
(barcode, d['name'], d['price'], final_image_path))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user