final solution for cached column
This commit is contained in:
parent
cb16ead6cf
commit
43fafaf6bf
@ -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
|
||||
|
||||
|
||||
27
app.py
27
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
|
||||
if is_cache_request:
|
||||
logging = False
|
||||
else:
|
||||
logging = True
|
||||
|
||||
was_cached = None
|
||||
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
|
||||
|
||||
|
||||
@ -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'
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user