From 770ff38b64f3791d85dea35bda219c4516990ca4 Mon Sep 17 00:00:00 2001 From: lelo Date: Sat, 22 Mar 2025 19:58:58 +0000 Subject: [PATCH] fix download delays --- analytics.py | 1 + app.py | 7 +++++-- static/app.js | 15 ++++++++------- templates/network.html | 1 - 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/analytics.py b/analytics.py index d5af7e1..b4c412d 100644 --- a/analytics.py +++ b/analytics.py @@ -89,6 +89,7 @@ def return_file_access(): else: return [] +@require_secret def network(): return render_template('network.html') diff --git a/app.py b/app.py index bea22ba..da5e3a7 100755 --- a/app.py +++ b/app.py @@ -140,10 +140,12 @@ def generate_breadcrumbs(subpath=None): @app.route('/static/icons/.png') def serve_resized_icon(size): cached_image_bytes = get_cached_image(size) - return send_file( + response = send_file( io.BytesIO(cached_image_bytes), mimetype='image/png' ) + response.headers['Cache-Control'] = 'public, max-age=86400' + return response @app.route('/sw.js') def serve_sw(): @@ -202,8 +204,9 @@ def serve_file(subpath): # HEAD request are coming in to initiate server caching. # only log initial hits and not the reload of further file parts range_header = request.headers.get('Range') + # only request with starting from the beginning of the file will be tracked if request.method != 'HEAD' and (not range_header or range_header.startswith("bytes=0-")): - a.log_file_access(full_path) + threading.Thread(target=a.log_file_access, args=(full_path,)).start() # Check cache first (using diskcache) response = None diff --git a/static/app.js b/static/app.js index 12b3550..6ab1c16 100644 --- a/static/app.js +++ b/static/app.js @@ -238,19 +238,20 @@ document.querySelectorAll('.play-file').forEach(link => { const folderName = pathParts[pathParts.length - 2]; const fileName = pathParts.pop(); const pathStr = pathParts.join('/'); - nowPlayingInfo.innerHTML = pathStr.replace(/\//g, ' > ') + '
' + fileName.replace('.mp3', '') + ''; // write into hardware player if ('mediaSession' in navigator) { navigator.mediaSession.metadata = new MediaMetadata({ title: currentMusicFiles[currentMusicIndex].title, artist: folderName, artwork: [ - { src: '/static/icons/logo-512x512.png', sizes: '512x512', type: 'image/png' } + { src: '/static/icons/logo-192x192.png', sizes: '192x192', type: 'image/png' } ] }); }; - preload_audio(); - } + nowPlayingInfo.innerHTML = pathStr.replace(/\//g, ' > ') + '
' + fileName.replace('.mp3', '') + ''; + // Delay preloading to avoid blocking playback + setTimeout(preload_audio, 1000); + }; } catch (error) { // If the fetch was aborted, error.name will be 'AbortError'. if (error.name === 'AbortError') { @@ -258,12 +259,12 @@ document.querySelectorAll('.play-file').forEach(link => { } else { console.error('Error fetching media:', error); nowPlayingInfo.textContent = "Fehler: Netzwerkproblem oder ungültige URL."; - } - } + }; + }; } else if (fileType === 'image') { // Open the gallery modal for image files. openGalleryModal(relUrl); - } + }; }); }); diff --git a/templates/network.html b/templates/network.html index 6ce6d71..dde6c98 100644 --- a/templates/network.html +++ b/templates/network.html @@ -78,7 +78,6 @@ }); }); -