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); } });