-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
415 lines (333 loc) · 12.8 KB
/
app.py
File metadata and controls
415 lines (333 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# -*- coding: utf-8 -*-
"""
app.py - Flask Web Server
=========================
Main server for the Receipt Processing System.
Routes:
GET / -> Main page (index.html)
POST /upload -> Upload & process a receipt image
GET /download -> Download generated Excel file
POST /reset -> Clear current batch
GET /health -> Health check
Security:
- Filename sanitization (UUID prefix + secure_filename)
- File extension + magic byte verification
- Upload size limit (15MB)
- Rate limiting (per-IP, memory-based)
- localhost-only binding
- No debug mode
- Error messages hide internal details
"""
from flask import Flask, render_template, request, jsonify, send_file
import os
import sys
import json
import time
import uuid
import socket
import signal
import atexit
import logging
from collections import defaultdict
from functools import wraps
from werkzeug.utils import secure_filename
from processor import ReceiptProcessor
from excel_generator import ExcelGenerator
# ---------------------------------------------------------------------------
# Logging (force UTF-8 on all platforms)
# ---------------------------------------------------------------------------
os.makedirs("logs", exist_ok=True)
_log_fh = logging.FileHandler("logs/server.log", encoding="utf-8")
_log_sh = logging.StreamHandler(sys.stdout)
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
handlers=[_log_fh, _log_sh],
)
logger = logging.getLogger("FlaskServer")
# ---------------------------------------------------------------------------
# Flask app
# ---------------------------------------------------------------------------
app = Flask(__name__)
# Paths (cross-platform)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(BASE_DIR, "uploads")
OUTPUT_FOLDER = os.path.join(BASE_DIR, "output")
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
app.config["OUTPUT_FOLDER"] = OUTPUT_FOLDER
app.config["MAX_CONTENT_LENGTH"] = 15 * 1024 * 1024 # 15MB
# Random secret key per session
app.secret_key = uuid.uuid4().hex
# ---------------------------------------------------------------------------
# Load config (with .env fallback for API key)
# ---------------------------------------------------------------------------
CONFIG_PATH = os.path.join(BASE_DIR, "config.json")
try:
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
config = json.load(f)
logger.info("Config loaded: %s", CONFIG_PATH)
except FileNotFoundError:
logger.critical("config.json not found!")
raise SystemExit("config.json is required. See README.md.")
except json.JSONDecodeError as e:
logger.critical("config.json parse error: %s", str(e))
raise SystemExit("config.json is malformed.")
# .env fallback: if api_key is still placeholder, check environment
try:
from dotenv import load_dotenv
load_dotenv(os.path.join(BASE_DIR, ".env"))
except ImportError:
pass # python-dotenv not required
env_key = os.environ.get("ANTHROPIC_API_KEY", "")
if config.get("api_key") == "YOUR_API_KEY_HERE" and env_key:
config["api_key"] = env_key
logger.info("API key loaded from environment variable")
# ---------------------------------------------------------------------------
# Initialize processor & Excel generator
# ---------------------------------------------------------------------------
try:
processor = ReceiptProcessor(config)
excel_gen = ExcelGenerator(config)
logger.info("Processor and Excel generator initialized")
except ValueError as e:
logger.critical("Initialization failed: %s", str(e))
raise SystemExit(str(e))
# ---------------------------------------------------------------------------
# Global state
# ---------------------------------------------------------------------------
current_batch_results = []
last_excel_path = None
# ---------------------------------------------------------------------------
# Cleanup on exit
# ---------------------------------------------------------------------------
def cleanup_uploads():
"""Remove temp files on server shutdown."""
try:
for f in os.listdir(UPLOAD_FOLDER):
fpath = os.path.join(UPLOAD_FOLDER, f)
if os.path.isfile(fpath):
os.remove(fpath)
logger.info("Upload folder cleaned")
except Exception:
pass
atexit.register(cleanup_uploads)
# Graceful shutdown on Ctrl+C
def _signal_handler(sig, frame):
cleanup_uploads()
sys.exit(0)
signal.signal(signal.SIGINT, _signal_handler)
signal.signal(signal.SIGTERM, _signal_handler)
# ---------------------------------------------------------------------------
# Rate limiter (memory-based, per IP)
# ---------------------------------------------------------------------------
_rate_limit_store = defaultdict(list)
RATE_LIMIT = config.get("security", {}).get("rate_limit_per_minute", 30)
def rate_limit(f):
"""Decorator to limit requests per minute per IP."""
@wraps(f)
def decorated(*args, **kwargs):
ip = request.remote_addr or "127.0.0.1"
now = time.time()
_rate_limit_store[ip] = [t for t in _rate_limit_store[ip] if now - t < 60]
if len(_rate_limit_store[ip]) >= RATE_LIMIT:
logger.warning("Rate limit exceeded: %s", ip)
return jsonify({
"success": False,
"error": "Too many requests. Please wait and try again.",
}), 429
_rate_limit_store[ip].append(now)
return f(*args, **kwargs)
return decorated
# ---------------------------------------------------------------------------
# File validation
# ---------------------------------------------------------------------------
ALLOWED_EXTENSIONS = set(
config.get("upload", {}).get("allowed_extensions", ["jpg", "jpeg", "png", "pdf"])
)
# Magic bytes for file type verification
MAGIC_BYTES = {
b"\xff\xd8\xff": "jpg", # JPEG
b"\x89PNG": "png", # PNG
b"%PDF": "pdf", # PDF
b"RIFF": "webp", # WEBP (RIFF....WEBP)
}
def is_allowed_file(filename: str) -> bool:
"""Check file extension against whitelist."""
if "." not in filename:
return False
return filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
def verify_file_magic(file_data: bytes, filename: str) -> bool:
"""
Verify file's magic bytes match its extension.
Prevents disguised executables (e.g. .exe renamed to .jpg).
"""
ext = filename.rsplit(".", 1)[1].lower() if "." in filename else ""
for magic, ftype in MAGIC_BYTES.items():
if file_data[: len(magic)] == magic:
if ext in ("jpg", "jpeg") and ftype == "jpg":
return True
if ext == ftype:
return True
# Skip magic check for formats with complex headers
if ext in ("heic", "webp"):
return True
logger.warning("Magic byte mismatch: %s (ext: %s)", filename, ext)
return False
def sanitize_filename(filename: str) -> str:
"""Generate safe filename with UUID prefix."""
safe = secure_filename(filename)
if not safe:
safe = "receipt.jpg"
unique = uuid.uuid4().hex[:8]
name, ext = os.path.splitext(safe)
return f"{unique}_{name}{ext}"
# ---------------------------------------------------------------------------
# Routes
# ---------------------------------------------------------------------------
@app.route("/")
def index():
"""Render main page."""
return render_template("index.html")
@app.route("/upload", methods=["POST"])
@rate_limit
def upload_receipt():
"""
Upload and process a receipt image.
Request: multipart/form-data with 'file' field.
Response: JSON result.
"""
global current_batch_results
try:
if "file" not in request.files:
return jsonify({"success": False, "error": "No file provided"}), 400
file = request.files["file"]
if not file.filename or file.filename.strip() == "":
return jsonify({"success": False, "error": "Empty filename"}), 400
if not is_allowed_file(file.filename):
return jsonify({
"success": False,
"error": f"Unsupported file type. Allowed: {', '.join(sorted(ALLOWED_EXTENSIONS))}",
}), 400
file_data = file.read()
if not verify_file_magic(file_data, file.filename):
return jsonify({
"success": False,
"error": "File content does not match its extension",
}), 400
max_size = config.get("upload", {}).get("max_file_size_mb", 15) * 1024 * 1024
if len(file_data) > max_size:
return jsonify({
"success": False,
"error": f"File too large (max {max_size // (1024 * 1024)}MB)",
}), 400
safe_name = sanitize_filename(file.filename)
filepath = os.path.join(app.config["UPLOAD_FOLDER"], safe_name)
with open(filepath, "wb") as f:
f.write(file_data)
logger.info("File saved: %s -> %s", file.filename, safe_name)
result = processor.process_receipt(filepath)
current_batch_results.append(result)
# Cleanup temp file
try:
os.remove(filepath)
except OSError:
pass
return jsonify(result)
except Exception as e:
logger.error("Upload error: %s", str(e), exc_info=True)
return jsonify({
"success": False,
"error": "Server error. Please try again.",
}), 500
@app.route("/download")
def download_excel():
"""Generate and download Excel file from current batch."""
global last_excel_path
try:
if not current_batch_results:
return jsonify({
"success": False,
"error": "No receipts processed yet.",
}), 400
filepath = excel_gen.generate_excel(
current_batch_results, app.config["OUTPUT_FOLDER"]
)
last_excel_path = filepath
return send_file(
filepath,
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
as_attachment=True,
download_name=os.path.basename(filepath),
)
except Exception as e:
logger.error("Download error: %s", str(e), exc_info=True)
return jsonify({
"success": False,
"error": "Failed to generate Excel file.",
}), 500
@app.route("/reset", methods=["POST"])
def reset_batch():
"""Clear current batch results."""
global current_batch_results, last_excel_path
current_batch_results = []
last_excel_path = None
cleanup_uploads()
logger.info("Batch reset")
return jsonify({"success": True})
@app.route("/health")
def health_check():
"""Server health check."""
return jsonify({
"status": "ok",
"batch_count": len(current_batch_results),
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
})
# ---------------------------------------------------------------------------
# Error handlers
# ---------------------------------------------------------------------------
@app.errorhandler(413)
def too_large(e):
return jsonify({"success": False, "error": "File too large."}), 413
@app.errorhandler(404)
def not_found(e):
return jsonify({"success": False, "error": "Not found."}), 404
@app.errorhandler(500)
def server_error(e):
return jsonify({"success": False, "error": "Internal server error."}), 500
# ---------------------------------------------------------------------------
# Port finder (macOS uses 5000 for AirPlay)
# ---------------------------------------------------------------------------
def find_available_port(config: dict) -> int:
"""Find an available port, trying primary then fallbacks."""
server_cfg = config.get("server", {})
ports = [server_cfg.get("port", 5000)] + server_cfg.get("fallback_ports", [])
for port in ports:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", port))
return port
except OSError:
logger.warning("Port %d is in use, trying next...", port)
# Last resort: let OS pick
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
# ---------------------------------------------------------------------------
# Server start
# ---------------------------------------------------------------------------
if __name__ == "__main__":
host = config.get("server", {}).get("host", "127.0.0.1")
port = find_available_port(config)
print()
print("=" * 52)
print(" Receipt Processor")
print("=" * 52)
print()
print(f" URL: http://{host}:{port}")
print(" Stop: Ctrl + C")
print()
print("=" * 52)
print()
app.run(host=host, port=port, debug=False)