try to fix cached variable

This commit is contained in:
lelo 2025-04-01 15:51:50 +00:00
parent 49311a0f91
commit cb16ead6cf
2 changed files with 7 additions and 1 deletions

View File

@ -71,6 +71,7 @@ def log_file_access(rel_path, filesize, mime, ip_address, user_agent, device_id,
VALUES (?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (iso_ts, rel_path, filesize, mime, ip_address, user_agent, device_id, cached)) ''', (iso_ts, rel_path, filesize, mime, ip_address, user_agent, device_id, cached))
file_access_temp.insert(0, [iso_ts, rel_path, filesize, mime, ip_address, user_agent, device_id, cached]) file_access_temp.insert(0, [iso_ts, rel_path, filesize, mime, ip_address, user_agent, device_id, cached])
print("Log: ", iso_ts, rel_path, filesize, mime, ip_address, user_agent, device_id, cached)
return iso_ts return iso_ts

7
app.py
View File

@ -224,11 +224,14 @@ def serve_file(subpath):
cache = cache_other cache = cache_other
# Check if the file is already cached # Check if the file is already cached
was_cached = None
cached = cache.get(subpath) cached = cache.get(subpath)
if cached: if cached:
cached_file_bytes, mime = cached cached_file_bytes, mime = cached
cached_file = io.BytesIO(cached_file_bytes) cached_file = io.BytesIO(cached_file_bytes)
filesize = len(cached_file.getbuffer()) filesize = len(cached_file.getbuffer())
was_cached = True
response = send_file(cached_file, mimetype=mime) response = send_file(cached_file, mimetype=mime)
else: else:
if mime and mime.startswith('image/'): if mime and mime.startswith('image/'):
@ -247,6 +250,7 @@ def serve_file(subpath):
img.save(img_bytes_io, format=output_format, **save_kwargs) img.save(img_bytes_io, format=output_format, **save_kwargs)
thumb_bytes = img_bytes_io.getvalue() thumb_bytes = img_bytes_io.getvalue()
cache.set(subpath, (thumb_bytes, output_mime)) cache.set(subpath, (thumb_bytes, output_mime))
was_cached = False
response = send_file(io.BytesIO(thumb_bytes), mimetype=output_mime, conditional=True) response = send_file(io.BytesIO(thumb_bytes), mimetype=output_mime, conditional=True)
except Exception as e: except Exception as e:
app.logger.error(f"Image processing failed for {subpath}: {e}") app.logger.error(f"Image processing failed for {subpath}: {e}")
@ -259,6 +263,7 @@ def serve_file(subpath):
cache.set(subpath, (file_bytes, mime)) cache.set(subpath, (file_bytes, mime))
file_bytes_io = io.BytesIO(file_bytes) file_bytes_io = io.BytesIO(file_bytes)
filesize = len(file_bytes_io.getbuffer()) filesize = len(file_bytes_io.getbuffer())
was_cached = False
response = send_file(file_bytes_io, mimetype=mime, conditional=True) response = send_file(file_bytes_io, mimetype=mime, conditional=True)
except Exception as e: except Exception as e:
app.logger.error(f"Failed to read file {subpath}: {e}") app.logger.error(f"Failed to read file {subpath}: {e}")
@ -278,7 +283,7 @@ def serve_file(subpath):
logging = True logging = True
if logging: if logging:
a.log_file_access(subpath, filesize, mime, ip_address, user_agent, session['device_id'], bool(cached)) a.log_file_access(subpath, filesize, mime, ip_address, user_agent, session['device_id'], was_cached)
return response return response