Fix code input clearing behavior on download

Validate download code before starting download to ensure code input is only cleared on successful downloads. Invalid or expired codes now show an error message while preserving the entered code for correction.
This commit is contained in:
2025-12-27 14:40:46 -05:00
parent 164e2162a2
commit f8943ab363

View File

@@ -292,8 +292,26 @@
downloadButton.textContent = 'Downloading...'; downloadButton.textContent = 'Downloading...';
try { try {
window.location.assign(`/download/${code}`); // First check if the code is valid by making a HEAD request
showMessage('Download started!', 'success'); const checkResponse = await fetch(`/download/${code}`, {
method: 'HEAD'
});
if (checkResponse.ok) {
// Code is valid, start download and clear the input
window.location.assign(`/download/${code}`);
showMessage('Download started!', 'success');
// Clear the code inputs after successful download
setTimeout(() => {
form.reset();
downloadButton.disabled = true;
deleteButton.disabled = true;
}, 500);
} else {
// Code is invalid, show error and don't clear
showMessage('Invalid or expired code. Please check and try again.', 'error');
}
} catch (error) { } catch (error) {
showMessage('Error starting download. Please try again.', 'error'); showMessage('Error starting download. Please try again.', 'error');
} finally { } finally {