From 787f799ab1b0820d3bdc08316248235f42b4a11d Mon Sep 17 00:00:00 2001 From: lelo Date: Sat, 19 Apr 2025 08:32:43 +0200 Subject: [PATCH] robust downloads --- static/app.js | 2 +- static/audioplayer.js | 43 ++++++++++++++++++++++++++++--------------- static/sw.js | 2 +- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/static/app.js b/static/app.js index 20bc1d6..71ddab7 100644 --- a/static/app.js +++ b/static/app.js @@ -292,7 +292,7 @@ document.querySelectorAll('.play-file').forEach(link => { title: currentMusicFiles[currentMusicIndex].title, artist: folderName, artwork: [ - { src: '/static/icons/logo-192x192.png', sizes: '192x192', type: 'image/png' } + { src: '/icons/logo-192x192.png', sizes: '192x192', type: 'image/png' } ] }); } diff --git a/static/audioplayer.js b/static/audioplayer.js index 2e258db..aec10af 100644 --- a/static/audioplayer.js +++ b/static/audioplayer.js @@ -98,20 +98,33 @@ audio.onended = function() { playerButton.innerHTML = playIcon; }; -function downloadAudio() { - // Get the current audio source URL - const audioSrc = audio.currentSrc || audio.src; - if (audioSrc) { - // Create a temporary link element - const a = document.createElement('a'); - a.href = audioSrc; - // Extract the file name from the URL, decode it, and set as download attribute - let fileName = audioSrc.split('/').pop() || 'audio'; - fileName = decodeURIComponent(fileName); - a.download = fileName; - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); - } + + +async function downloadAudio() { + const src = audio.currentSrc; + if (!src) return; + + // Fetch with credentials / correct mode + const response = await fetch(src, { credentials: 'same-origin' }); + if (!response.ok) throw new Error(`Download failed: ${response.status}`); + + // Grab the data as a Blob + const blob = await response.blob(); + + // Create your own blob: URL and anchor + const url = URL.createObjectURL(blob); + const a = document.createElement('a'); + a.href = url; + + // Retain original filename if possible + let filename = decodeURIComponent(src.split('/').pop() || 'audio'); + a.download = filename; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + + // Cleanup + URL.revokeObjectURL(url); } + diff --git a/static/sw.js b/static/sw.js index 43407e0..2e3e9c2 100644 --- a/static/sw.js +++ b/static/sw.js @@ -1,4 +1,4 @@ -const cacheName = 'gottesdienste-v1.2'; +const cacheName = 'gottesdienste-v1.3'; const assets = [ '/', '/static/app.css',