diff --git a/src/index.js b/src/index.js index 5b721ee..5a8cfdb 100644 --- a/src/index.js +++ b/src/index.js @@ -85,7 +85,7 @@ async function handleRequest(event) { } async function handleUrlUpload(request) { - const { url, authKey } = await request.json(); + const { url, authKey, deletionTimestamp } = await request.json(); if (!url || !authKey) { return new Response('Bad Request', { status: 400, headers: headersCORS }); } @@ -104,7 +104,9 @@ async function handleUrlUpload(request) { const hash = nanoid(); const contentType = fileResponse.headers.get('Content-Type') || 'application/octet-stream'; const originalName = url.split('/').pop(); - const deletionTimestamp = Date.now() + 24 * 60 * 60 * 1000; // 24 hours + + // Use provided deletionTimestamp or default to 24 hours from now + const expiryTimestamp = deletionTimestamp ? parseInt(deletionTimestamp) : Date.now() + 24 * 60 * 60 * 1000; await CDN_BUCKET.put(`tempupload/content/${hash}`, fileBlob.stream(), { httpMetadata: { contentType }, @@ -117,14 +119,14 @@ async function handleUrlUpload(request) { hash, contentType, originalName, - deletionTimestamp, + deletionTimestamp: expiryTimestamp, }); await CDN_BUCKET.put('tempupload/index.json', JSON.stringify(index), { httpMetadata: { contentType: 'application/json' }, }); - return new Response(JSON.stringify({ downloadLink: `https://fileshare.jonasjones.dev/download/${hash}` }), { + return new Response(JSON.stringify({ downloadLink: `/download/${hash}` }), { headers: { 'Content-Type': 'application/json', ...headersCORS }, }); } catch (error) { @@ -133,25 +135,24 @@ async function handleUrlUpload(request) { } async function cleanUpExpiredFiles() { - const indexData = await CDN_BUCKET.get('/tempupload/index.json'); + const indexData = await CDN_BUCKET.get('tempupload/index.json'); if (!indexData) { return; } let index = await indexData.json(); - const now = Date.now(); const updatedIndex = []; - for (const fileRecord of index) { - if (fileRecord.deletionTimestamp <= now) { - await CDN_BUCKET.delete(`/tempupload/content/${fileRecord.hash}`); + for (fileRecord of index) { + if (fileRecord.deletionTimestamp <= Date.now()) { + await CDN_BUCKET.delete(`tempupload/content/${fileRecord.hash}`); } else { updatedIndex.push(fileRecord); } } if (updatedIndex.length !== index.length) { - await CDN_BUCKET.put('/tempupload/index.json', JSON.stringify(updatedIndex), { + await CDN_BUCKET.put('tempupload/index.json', JSON.stringify(updatedIndex), { httpMetadata: { contentType: 'application/json' }, }); } @@ -165,6 +166,8 @@ async function handleUpload(request) { const formData = await request.formData(); const file = formData.get('file'); + const deletionTimestamp = formData.get('deletionTimestamp'); + console.log('deletionTimestamp:', deletionTimestamp); if (!file) { return new Response('Bad Request', { status: 400, headers: headersCORS }); } @@ -172,7 +175,8 @@ async function handleUpload(request) { const hash = nanoid(); const contentType = file.type; const originalName = file.name; - const deletionTimestamp = Date.now() + 24 * 60 * 60 * 1000; // 24 hours + + const expiryTimestamp = deletionTimestamp ? parseInt(deletionTimestamp) : Date.now() + 7 * 24 * 60 * 60 * 1000; await CDN_BUCKET.put(`tempupload/content/${hash}`, file.stream(), { httpMetadata: { contentType }, @@ -185,7 +189,7 @@ async function handleUpload(request) { hash, contentType, originalName, - deletionTimestamp, + deletionTimestamp: expiryTimestamp, }); await CDN_BUCKET.put('tempupload/index.json', JSON.stringify(index), { @@ -289,6 +293,9 @@ const htmlForm = `
@@ -297,6 +304,9 @@ const htmlForm = ` @@ -316,7 +326,7 @@ const htmlForm = `