fix download delays

This commit is contained in:
lelo 2025-03-22 19:58:58 +00:00
parent f2e5e2f314
commit 770ff38b64
4 changed files with 14 additions and 10 deletions

View File

@ -89,6 +89,7 @@ def return_file_access():
else:
return []
@require_secret
def network():
return render_template('network.html')

7
app.py
View File

@ -140,10 +140,12 @@ def generate_breadcrumbs(subpath=None):
@app.route('/static/icons/<string:size>.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

View File

@ -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, ' > ') + '<br><span style="font-size: larger; font-weight: bold;">' + fileName.replace('.mp3', '') + '</span>';
// 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, ' > ') + '<br><span style="font-size: larger; font-weight: bold;">' + fileName.replace('.mp3', '') + '</span>';
// 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);
}
};
});
});

View File

@ -78,7 +78,6 @@
});
});
</script>
<!-- Bootstrap 5 JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>