diff --git a/app/fileManager.js b/app/fileManager.js index 0dd46d21..acbe9161 100644 --- a/app/fileManager.js +++ b/app/fileManager.js @@ -36,22 +36,16 @@ function openLinksInNewTab(links, should = true) { return links; } -function exists(id) { - return new Promise((resolve, reject) => { - const xhr = new XMLHttpRequest(); - xhr.onreadystatechange = () => { - if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) { - resolve(xhr.status === 200); - } - }; - xhr.onerror = () => resolve(false); - xhr.ontimeout = () => resolve(false); - xhr.open('get', '/api/exists/' + id); - xhr.timeout = 2000; - xhr.send(); - }); +async function getDLCounts(file) { + const url = `/api/metadata/${file.id}`; + const receiver = new FileReceiver(url, file); + try { + await receiver.getMetadata(file.nonce); + return receiver.file; + } catch (e) { + if (e.message === '404') return false; + } } - export default function(state, emitter) { let lastRender = 0; let updateTitle = false; @@ -64,10 +58,17 @@ export default function(state, emitter) { const files = state.storage.files; let rerender = false; for (const file of files) { - const ok = await exists(file.id); - if (!ok) { + const oldLimit = file.dlimit; + const oldTotal = file.dtotal; + const receivedFile = await getDLCounts(file); + if (!receivedFile) { state.storage.remove(file.id); rerender = true; + } else if ( + oldLimit !== receivedFile.dlimit || + oldTotal !== receivedFile.dtotal + ) { + rerender = true; } } if (rerender) { diff --git a/app/templates/file.js b/app/templates/file.js index 881ef098..8d658ad7 100644 --- a/app/templates/file.js +++ b/app/templates/file.js @@ -15,7 +15,9 @@ function timeLeft(milliseconds) { module.exports = function(file, state, emit) { const ttl = file.expiresAt - Date.now(); - const remaining = timeLeft(ttl) || state.translate('linkExpiredAlt'); + const remainingTime = timeLeft(ttl) || state.translate('linkExpiredAlt'); + const downloadLimit = file.dlimit || 1; + const totalDownloads = file.dtotal || 0; const row = html`