From 610a108bf8081978ff5f26949326c3fca73c2fe3 Mon Sep 17 00:00:00 2001 From: lelo Date: Sun, 16 Mar 2025 22:51:44 +0100 Subject: [PATCH] jump back to root when run into 403 --- static/app.js | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/static/app.js b/static/app.js index fdee292..2f8cf58 100644 --- a/static/app.js +++ b/static/app.js @@ -118,14 +118,28 @@ function attachEventListeners() { const relUrl = this.getAttribute('data-url'); if (fileType === 'music') { const mediaUrl = '/media/' + relUrl; - const audioPlayer = document.getElementById('globalAudio'); - audioPlayer.src = mediaUrl; - audioPlayer.load(); - audioPlayer.play(); - document.getElementById('nowPlayingInfo').textContent = relUrl; - document.querySelector('footer').style.display = 'flex'; + // Check if the media URL is forbidden + fetch(mediaUrl, { method: 'HEAD' }) + .then(response => { + if (response.status === 403) { + // Redirect to the root if a 403 status is returned + window.location.href = '/'; + } else { + // Otherwise, play the audio + const audioPlayer = document.getElementById('globalAudio'); + audioPlayer.src = mediaUrl; + audioPlayer.load(); + audioPlayer.play(); + document.getElementById('nowPlayingInfo').textContent = relUrl; + document.querySelector('footer').style.display = 'flex'; + } + }) + .catch(error => { + console.error('Error fetching media:', error); + // Optionally handle network errors here + }); } else if (fileType === 'image') { - // Open gallery modal via gallery.js function. + // Open the gallery modal for image files openGalleryModal(relUrl); } });