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 (?, ?, ?, ?, ?, ?, ?, ?)
|
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
|
||||||
|
|
||||||
|
|||||||
27
app.py
27
app.py
@ -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,14 +225,16 @@ 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
|
||||||
|
|
||||||
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/'):
|
||||||
@ -246,11 +249,10 @@ 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))
|
||||||
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}")
|
||||||
@ -263,7 +265,6 @@ 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}")
|
||||||
@ -272,18 +273,14 @@ 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
|
logging = False
|
||||||
else:
|
|
||||||
logging = False
|
|
||||||
else:
|
|
||||||
logging = True
|
|
||||||
|
|
||||||
if logging:
|
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
|
return response
|
||||||
|
|
||||||
|
|||||||
@ -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));
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user