adding data to scan request + Go ARMv7
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
pos_database.db
|
||||
ScannerGO/ScannerGO-*
|
||||
ScannerGO/config.json
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
# Define binary names
|
||||
LINUX_BIN="ScannerGO-linux"
|
||||
LINUX_ARM_BIN="ScannerGO-linuxARMv7"
|
||||
WINDOWS_BIN="ScannerGO-windows.exe"
|
||||
|
||||
echo "Starting build process..."
|
||||
@@ -28,4 +29,14 @@ else
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Build for Linux ARM (ARMv7)
|
||||
echo "Building for Linux ARMv7..."
|
||||
GOOS=linux GOARCH=arm GOARM=7 go build -o "$LINUX_ARM_BIN" main.go
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "Successfully built: $LINUX_ARM_BIN"
|
||||
else
|
||||
echo "Failed to build Linux ARMv7 binary"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Build complete."
|
||||
|
||||
@@ -118,6 +118,7 @@ func sendToEndpoint(baseURL, content string) {
|
||||
client := &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
fullURL := fmt.Sprintf("%s?content=%s", baseURL, url.QueryEscape(content))
|
||||
resp, err := client.Get(fullURL)
|
||||
if err != nil {
|
||||
@@ -125,5 +126,17 @@ func sendToEndpoint(baseURL, content string) {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Read the response body
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading response: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Data: [%s] | Status: %s\n", content, resp.Status)
|
||||
if len(body) > 0 {
|
||||
fmt.Printf("Response: %s\n", string(body))
|
||||
}
|
||||
fmt.Println(strings.Repeat("-", 30))
|
||||
}
|
||||
|
||||
44
app.py
44
app.py
@@ -157,45 +157,53 @@ def delete(barcode):
|
||||
@app.route('/scan', methods=['GET'])
|
||||
def scan():
|
||||
barcode = request.args.get('content', '').replace('{content}', '')
|
||||
if not barcode: return jsonify({"err": "empty"}), 400
|
||||
if not barcode:
|
||||
return jsonify({"status": "error", "message": "empty barcode"}), 400
|
||||
|
||||
with sqlite3.connect(DB_FILE) as conn:
|
||||
p = conn.execute('SELECT * FROM products WHERE barcode = ?', (barcode,)).fetchone()
|
||||
|
||||
# 1. Product exists in local Database
|
||||
if p:
|
||||
barcode_val, name, price, image_path = p
|
||||
|
||||
# FIX: Check if the file exists relative to the application root
|
||||
# We lstrip('/') to convert '/static/cache/xxx.jpg' to 'static/cache/xxx.jpg'
|
||||
# Image recovery logic for missing local files
|
||||
if image_path and image_path.startswith('/static/'):
|
||||
# Strip query parameters like ?t=12345 before checking disk
|
||||
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)
|
||||
if ext_data and ext_data.get('image'):
|
||||
image_path = ext_data['image']
|
||||
with sqlite3.connect(DB_FILE) as conn:
|
||||
conn.execute('UPDATE products SET image_url = ? WHERE barcode = ?', (image_path, barcode_val))
|
||||
conn.commit()
|
||||
socketio.emit('new_scan', {
|
||||
"barcode": barcode_val, "name": name, "price": int(price),
|
||||
"image": image_path, "note": "Imagen recuperada"
|
||||
})
|
||||
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})
|
||||
return jsonify({"status": "ok"})
|
||||
product_data = {
|
||||
"barcode": barcode_val,
|
||||
"name": name,
|
||||
"price": int(price),
|
||||
"image": image_path
|
||||
}
|
||||
|
||||
socketio.emit('new_scan', product_data)
|
||||
return jsonify({"status": "ok", "data": product_data}), 200
|
||||
|
||||
# 2. Product not in DB, try external API
|
||||
ext = fetch_from_openfoodfacts(barcode)
|
||||
if ext:
|
||||
socketio.emit('scan_error', {"barcode": barcode, "name": ext['name'], "image": ext['image']})
|
||||
else:
|
||||
socketio.emit('scan_error', {"barcode": barcode})
|
||||
# We found it externally, but it's still a 404 relative to our local DB
|
||||
external_data = {
|
||||
"barcode": barcode,
|
||||
"name": ext['name'],
|
||||
"image": ext['image'],
|
||||
"source": "openfoodfacts"
|
||||
}
|
||||
socketio.emit('scan_error', external_data)
|
||||
return jsonify({"status": "not_found", "data": external_data}), 404
|
||||
|
||||
return jsonify({"status": "not_found"}), 404
|
||||
# 3. Truly not found anywhere
|
||||
socketio.emit('scan_error', {"barcode": barcode})
|
||||
return jsonify({"status": "not_found", "data": {"barcode": barcode}}), 404
|
||||
|
||||
@app.route('/static/cache/<path:filename>')
|
||||
def serve_cache(filename):
|
||||
|
||||
Reference in New Issue
Block a user