Merge remote-tracking branch 'origin/development'

This commit is contained in:
lelo 2025-04-01 19:28:47 +02:00
commit 4f88727321
2 changed files with 18 additions and 15 deletions

20
app.py
View File

@ -209,6 +209,7 @@ def serve_file(subpath):
range_header = request.headers.get('Range') range_header = request.headers.get('Range')
ip_address = request.remote_addr ip_address = request.remote_addr
user_agent = request.headers.get('User-Agent') user_agent = request.headers.get('User-Agent')
is_cache_request = request.headers.get('X-Cache-Request') == 'true'
# Check cache first (using diskcache) # Check cache first (using diskcache)
response = None response = None
@ -224,6 +225,11 @@ def serve_file(subpath):
cache = cache_other cache = cache_other
# Check if the file is already cached # Check if the file is already cached
if is_cache_request:
logging = False
else:
logging = True
cached = cache.get(subpath) cached = cache.get(subpath)
if cached: if cached:
cached_file_bytes, mime = cached cached_file_bytes, mime = cached
@ -243,9 +249,9 @@ def serve_file(subpath):
save_kwargs = {'quality': 85} save_kwargs = {'quality': 85}
img_bytes_io = io.BytesIO() img_bytes_io = io.BytesIO()
filesize = len(img_bytes_io.getbuffer())
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()
filesize = len(thumb_bytes)
cache.set(subpath, (thumb_bytes, output_mime)) cache.set(subpath, (thumb_bytes, output_mime))
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:
@ -267,15 +273,11 @@ def serve_file(subpath):
# 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' response.headers['Cache-Control'] = 'public, max-age=86400'
if mime and mime.startswith('audio/mpeg'): # special rules for mp3 files # special rules for audio files.
# HEAD request are coming in to initiate server caching. Ignore HEAD Request. Only log GET request. # HEAD request checks if the audio file is available. GET requests coming from the audi player itself and can be made multiple times.
# 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) # a HEAD request only for logging will be ignored because of rules before
if request.method == 'GET' and (not range_header or (range_header.startswith("bytes=0-") and range_header != "bytes=0-1")): if mime and mime.startswith('audio/mpeg') and request.method != 'HEAD':
logging = True
else:
logging = False logging = False
else:
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'], bool(cached))

View File

@ -161,11 +161,12 @@ function preload_audio() {
const nextFile = currentMusicFiles[currentMusicIndex + 1]; const nextFile = currentMusicFiles[currentMusicIndex + 1];
const nextMediaUrl = '/media/' + nextFile.path; const nextMediaUrl = '/media/' + nextFile.path;
// Use a HEAD request so that the backend reads and caches the file without returning the full content. // Use a HEAD request so that the backend reads and caches the file without returning the full content.
fetch(nextMediaUrl, { method: 'HEAD' }) fetch(nextMediaUrl, {
// .then(response => { method: 'HEAD',
// console.log('Backend diskcache initiated for next file:', nextFile.path); headers: {
// }) 'X-Cache-Request': 'true'
// .catch(error => console.error('Error initiating backend diskcache for next file:', error)); }
});
} }
} }