fix caching

This commit is contained in:
lelo 2025-03-16 22:38:46 +01:00
parent 5dd26db2be
commit 9913b96eb9
4 changed files with 17 additions and 5 deletions

Binary file not shown.

Binary file not shown.

22
app.py
View File

@ -13,6 +13,7 @@ app = Flask(__name__)
# Use a raw string for the UNC path. # Use a raw string for the UNC path.
app.config['MP3_ROOT'] = r'/mp3_root' app.config['MP3_ROOT'] = r'/mp3_root'
# app.config['MP3_ROOT'] = r'\\192.168.10.10\docker2\sync-bethaus\syncfiles\folders'
app.config['SECRET_KEY'] = os.urandom(24) app.config['SECRET_KEY'] = os.urandom(24)
app.config['ALLOWED_SECRETS'] = { app.config['ALLOWED_SECRETS'] = {
'test': datetime(2026, 3, 31, 23, 59, 59), 'test': datetime(2026, 3, 31, 23, 59, 59),
@ -177,6 +178,7 @@ def serve_file(filename):
full_path = os.path.normpath(os.path.join(app.config['MP3_ROOT'], decoded_filename)) full_path = os.path.normpath(os.path.join(app.config['MP3_ROOT'], decoded_filename))
if not os.path.isfile(full_path): if not os.path.isfile(full_path):
app.logger.error(f"File not found: {full_path}")
return "File not found", 404 return "File not found", 404
mime, _ = mimetypes.guess_type(full_path) mime, _ = mimetypes.guess_type(full_path)
@ -187,12 +189,14 @@ def serve_file(filename):
# Check cache first (using diskcache) # Check cache first (using diskcache)
cached = cache.get(filename) cached = cache.get(filename)
if cached: if cached:
app.logger.info(f"Cache hit for {filename}")
cached_file_bytes, mime = cached cached_file_bytes, mime = cached
cached_file = io.BytesIO(cached_file_bytes) cached_file = io.BytesIO(cached_file_bytes)
response = send_file(cached_file, mimetype=mime) response = send_file(cached_file, mimetype=mime)
else: else:
app.logger.info(f"Cache miss for {filename}")
if mime and mime.startswith('image/'): if mime and mime.startswith('image/'):
# Image processing # Image processing branch (with caching)
try: try:
with Image.open(full_path) as img: with Image.open(full_path) as img:
img.thumbnail((1200, 1200)) img.thumbnail((1200, 1200))
@ -202,16 +206,24 @@ def serve_file(filename):
cache.set(filename, (img_bytes, mime)) cache.set(filename, (img_bytes, mime))
response = send_file(io.BytesIO(img_bytes), mimetype=mime) response = send_file(io.BytesIO(img_bytes), mimetype=mime)
except Exception as e: except Exception as e:
app.logger.error(f"Image processing failed: {e}") app.logger.error(f"Image processing failed for {filename}: {e}")
abort(500) abort(500)
else: else:
# Other file types # Cache non-image files: read bytes and cache
response = send_file(full_path, mimetype=mime) try:
with open(full_path, 'rb') as f:
file_bytes = f.read()
cache.set(filename, (file_bytes, mime))
response = send_file(io.BytesIO(file_bytes), mimetype=mime)
except Exception as e:
app.logger.error(f"Failed to read file {filename}: {e}")
abort(500)
# Set Cache-Control header (browser caching for 1 day) # Set Cache-Control header (browser caching for 1 day)
response.headers['Cache-Control'] = 'public, max-age=86400' # 1 day response.headers['Cache-Control'] = 'public, max-age=86400'
return response return response
@app.route("/transcript/<path:filename>") @app.route("/transcript/<path:filename>")
@require_secret @require_secret
def get_transcript(filename): def get_transcript(filename):