From f8943ab36360cedb5fd274ec6f04c9b23034f48a Mon Sep 17 00:00:00 2001 From: Matt Hills Date: Sat, 27 Dec 2025 14:40:46 -0500 Subject: [PATCH] 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. --- src/templates/index.html | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/templates/index.html b/src/templates/index.html index d140c62..ff3472b 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -280,7 +280,7 @@ form.addEventListener('submit', async (e) => { e.preventDefault(); const code = Array.from(inputs).map(input => input.value).join(''); - + if (code.length !== 6) { showMessage('Please enter a valid 6-character code.', 'error'); return; @@ -292,8 +292,26 @@ downloadButton.textContent = 'Downloading...'; try { - window.location.assign(`/download/${code}`); - showMessage('Download started!', 'success'); + // First check if the code is valid by making a HEAD request + 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) { showMessage('Error starting download. Please try again.', 'error'); } finally {