From 43fafaf6bf76680eb76753382c9b868451f24afe Mon Sep 17 00:00:00 2001 From: lelo Date: Tue, 1 Apr 2025 19:26:59 +0200 Subject: [PATCH] final solution for cached column --- analytics.py | 1 - app.py | 29 +++++++++++++---------------- static/app.js | 11 ++++++----- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/analytics.py b/analytics.py index 65e78d7..6c2a468 100644 --- a/analytics.py +++ b/analytics.py @@ -71,7 +71,6 @@ def log_file_access(rel_path, filesize, mime, ip_address, user_agent, device_id, VALUES (?, ?, ?, ?, ?, ?, ?, ?) ''', (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 diff --git a/app.py b/app.py index 7ad9749..4fbd3e8 100755 --- a/app.py +++ b/app.py @@ -209,6 +209,7 @@ def serve_file(subpath): range_header = request.headers.get('Range') ip_address = request.remote_addr user_agent = request.headers.get('User-Agent') + is_cache_request = request.headers.get('X-Cache-Request') == 'true' # Check cache first (using diskcache) response = None @@ -224,14 +225,16 @@ def serve_file(subpath): cache = cache_other # Check if the file is already cached - - was_cached = None + if is_cache_request: + logging = False + else: + logging = True + cached = cache.get(subpath) if cached: cached_file_bytes, mime = cached cached_file = io.BytesIO(cached_file_bytes) filesize = len(cached_file.getbuffer()) - was_cached = True response = send_file(cached_file, mimetype=mime) else: if mime and mime.startswith('image/'): @@ -246,11 +249,10 @@ def serve_file(subpath): save_kwargs = {'quality': 85} img_bytes_io = io.BytesIO() - filesize = len(img_bytes_io.getbuffer()) img.save(img_bytes_io, format=output_format, **save_kwargs) thumb_bytes = img_bytes_io.getvalue() + filesize = len(thumb_bytes) cache.set(subpath, (thumb_bytes, output_mime)) - was_cached = False response = send_file(io.BytesIO(thumb_bytes), mimetype=output_mime, conditional=True) except Exception as e: app.logger.error(f"Image processing failed for {subpath}: {e}") @@ -263,7 +265,6 @@ def serve_file(subpath): cache.set(subpath, (file_bytes, mime)) file_bytes_io = io.BytesIO(file_bytes) filesize = len(file_bytes_io.getbuffer()) - was_cached = False response = send_file(file_bytes_io, mimetype=mime, conditional=True) except Exception as e: app.logger.error(f"Failed to read file {subpath}: {e}") @@ -272,18 +273,14 @@ def serve_file(subpath): # Set Cache-Control header (browser caching for 1 day) response.headers['Cache-Control'] = 'public, max-age=86400' - if mime and mime.startswith('audio/mpeg'): # special rules for mp3 files - # HEAD request are coming in to initiate server caching. Ignore HEAD Request. Only log GET request. - # log access if there is no range header. # log access if range request starts from 0 but is larger then only from 0 to 1 (bytes=0-1) - if request.method == 'GET' and (not range_header or (range_header.startswith("bytes=0-") and range_header != "bytes=0-1")): - logging = True - else: - logging = False - else: - logging = True + # special rules for audio files. + # HEAD request checks if the audio file is available. GET requests coming from the audi player itself and can be made multiple times. + # a HEAD request only for logging will be ignored because of rules before + if mime and mime.startswith('audio/mpeg') and request.method != 'HEAD': + logging = False if logging: - a.log_file_access(subpath, filesize, mime, ip_address, user_agent, session['device_id'], was_cached) + a.log_file_access(subpath, filesize, mime, ip_address, user_agent, session['device_id'], bool(cached)) return response diff --git a/static/app.js b/static/app.js index 503b273..4bba0a8 100644 --- a/static/app.js +++ b/static/app.js @@ -161,11 +161,12 @@ function preload_audio() { const nextFile = currentMusicFiles[currentMusicIndex + 1]; const nextMediaUrl = '/media/' + nextFile.path; // Use a HEAD request so that the backend reads and caches the file without returning the full content. - fetch(nextMediaUrl, { method: 'HEAD' }) - // .then(response => { - // console.log('Backend diskcache initiated for next file:', nextFile.path); - // }) - // .catch(error => console.error('Error initiating backend diskcache for next file:', error)); + fetch(nextMediaUrl, { + method: 'HEAD', + headers: { + 'X-Cache-Request': 'true' + } + }); } }