mirror of
https://github.com/JonasunderscoreJones/fileshare.jonasjones.dev.git
synced 2025-10-22 23:19:18 +02:00
fixed deletion date not working and added custom deletion timestamp
also cleaned up code
This commit is contained in:
parent
c1c5b81c04
commit
92489f6b7f
1 changed files with 73 additions and 20 deletions
93
src/index.js
93
src/index.js
|
@ -85,7 +85,7 @@ async function handleRequest(event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleUrlUpload(request) {
|
async function handleUrlUpload(request) {
|
||||||
const { url, authKey } = await request.json();
|
const { url, authKey, deletionTimestamp } = await request.json();
|
||||||
if (!url || !authKey) {
|
if (!url || !authKey) {
|
||||||
return new Response('Bad Request', { status: 400, headers: headersCORS });
|
return new Response('Bad Request', { status: 400, headers: headersCORS });
|
||||||
}
|
}
|
||||||
|
@ -104,7 +104,9 @@ async function handleUrlUpload(request) {
|
||||||
const hash = nanoid();
|
const hash = nanoid();
|
||||||
const contentType = fileResponse.headers.get('Content-Type') || 'application/octet-stream';
|
const contentType = fileResponse.headers.get('Content-Type') || 'application/octet-stream';
|
||||||
const originalName = url.split('/').pop();
|
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(), {
|
await CDN_BUCKET.put(`tempupload/content/${hash}`, fileBlob.stream(), {
|
||||||
httpMetadata: { contentType },
|
httpMetadata: { contentType },
|
||||||
|
@ -117,14 +119,14 @@ async function handleUrlUpload(request) {
|
||||||
hash,
|
hash,
|
||||||
contentType,
|
contentType,
|
||||||
originalName,
|
originalName,
|
||||||
deletionTimestamp,
|
deletionTimestamp: expiryTimestamp,
|
||||||
});
|
});
|
||||||
|
|
||||||
await CDN_BUCKET.put('tempupload/index.json', JSON.stringify(index), {
|
await CDN_BUCKET.put('tempupload/index.json', JSON.stringify(index), {
|
||||||
httpMetadata: { contentType: 'application/json' },
|
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 },
|
headers: { 'Content-Type': 'application/json', ...headersCORS },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -133,25 +135,24 @@ async function handleUrlUpload(request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function cleanUpExpiredFiles() {
|
async function cleanUpExpiredFiles() {
|
||||||
const indexData = await CDN_BUCKET.get('/tempupload/index.json');
|
const indexData = await CDN_BUCKET.get('tempupload/index.json');
|
||||||
if (!indexData) {
|
if (!indexData) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let index = await indexData.json();
|
let index = await indexData.json();
|
||||||
const now = Date.now();
|
|
||||||
const updatedIndex = [];
|
const updatedIndex = [];
|
||||||
|
|
||||||
for (const fileRecord of index) {
|
for (fileRecord of index) {
|
||||||
if (fileRecord.deletionTimestamp <= now) {
|
if (fileRecord.deletionTimestamp <= Date.now()) {
|
||||||
await CDN_BUCKET.delete(`/tempupload/content/${fileRecord.hash}`);
|
await CDN_BUCKET.delete(`tempupload/content/${fileRecord.hash}`);
|
||||||
} else {
|
} else {
|
||||||
updatedIndex.push(fileRecord);
|
updatedIndex.push(fileRecord);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (updatedIndex.length !== index.length) {
|
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' },
|
httpMetadata: { contentType: 'application/json' },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -165,6 +166,8 @@ async function handleUpload(request) {
|
||||||
|
|
||||||
const formData = await request.formData();
|
const formData = await request.formData();
|
||||||
const file = formData.get('file');
|
const file = formData.get('file');
|
||||||
|
const deletionTimestamp = formData.get('deletionTimestamp');
|
||||||
|
console.log('deletionTimestamp:', deletionTimestamp);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
return new Response('Bad Request', { status: 400, headers: headersCORS });
|
return new Response('Bad Request', { status: 400, headers: headersCORS });
|
||||||
}
|
}
|
||||||
|
@ -172,7 +175,8 @@ async function handleUpload(request) {
|
||||||
const hash = nanoid();
|
const hash = nanoid();
|
||||||
const contentType = file.type;
|
const contentType = file.type;
|
||||||
const originalName = file.name;
|
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(), {
|
await CDN_BUCKET.put(`tempupload/content/${hash}`, file.stream(), {
|
||||||
httpMetadata: { contentType },
|
httpMetadata: { contentType },
|
||||||
|
@ -185,7 +189,7 @@ async function handleUpload(request) {
|
||||||
hash,
|
hash,
|
||||||
contentType,
|
contentType,
|
||||||
originalName,
|
originalName,
|
||||||
deletionTimestamp,
|
deletionTimestamp: expiryTimestamp,
|
||||||
});
|
});
|
||||||
|
|
||||||
await CDN_BUCKET.put('tempupload/index.json', JSON.stringify(index), {
|
await CDN_BUCKET.put('tempupload/index.json', JSON.stringify(index), {
|
||||||
|
@ -289,6 +293,9 @@ const htmlForm = `
|
||||||
<form id="uploadForm">
|
<form id="uploadForm">
|
||||||
<input type="file" id="fileInput" name="file" required>
|
<input type="file" id="fileInput" name="file" required>
|
||||||
<input type="password" id="authKey" placeholder="Enter password" required>
|
<input type="password" id="authKey" placeholder="Enter password" required>
|
||||||
|
<label for="deletionDate">Deletion Date (select date or leave blank for 7 days from now):</label>
|
||||||
|
<input type="date" id="deletionDate">
|
||||||
|
<input type="checkbox" id="defaultSevenDays"> Set to delete 7 days from now
|
||||||
<button type="submit">Upload</button>
|
<button type="submit">Upload</button>
|
||||||
</form>
|
</form>
|
||||||
<div id="uploadResponse"></div>
|
<div id="uploadResponse"></div>
|
||||||
|
@ -297,6 +304,9 @@ const htmlForm = `
|
||||||
<form id="urlUploadForm">
|
<form id="urlUploadForm">
|
||||||
<input type="url" id="fileUrl" placeholder="Enter file URL" required>
|
<input type="url" id="fileUrl" placeholder="Enter file URL" required>
|
||||||
<input type="password" id="urlAuthKey" placeholder="Enter password" required>
|
<input type="password" id="urlAuthKey" placeholder="Enter password" required>
|
||||||
|
<label for="urlDeletionDate">Deletion Date (select date or leave blank for 7 days from now):</label>
|
||||||
|
<input type="date" id="urlDeletionDate">
|
||||||
|
<input type="checkbox" id="urlDefaultSevenDays"> Set to delete 7 days from now
|
||||||
<button type="submit">Upload</button>
|
<button type="submit">Upload</button>
|
||||||
</form>
|
</form>
|
||||||
<div id="urlUploadResponse"></div>
|
<div id="urlUploadResponse"></div>
|
||||||
|
@ -316,7 +326,7 @@ const htmlForm = `
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Original Name</th>
|
<th>Original Name</th>
|
||||||
<th>Deletion Timestamp</th>
|
<th>Deletion Date</th>
|
||||||
<th>Download Link (Hash)</th>
|
<th>Download Link (Hash)</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
@ -324,6 +334,20 @@ const htmlForm = `
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
// Calculate 7 days from now in Unix milliseconds
|
||||||
|
function getSevenDaysFromNow() {
|
||||||
|
const now = new Date();
|
||||||
|
return roundToNextDay(now.getTime() + 8 * 24 * 60 * 60 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Round to start of next day
|
||||||
|
function roundToNextDay(timestamp) {
|
||||||
|
const date = new Date(timestamp);
|
||||||
|
date.setHours(24, 0, 0, 0); // Set to 00:00:00 the following day
|
||||||
|
return date.getTime() - 24 * 60 * 60 * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle form submission for file upload
|
||||||
document.getElementById('uploadForm').addEventListener('submit', async (e) => {
|
document.getElementById('uploadForm').addEventListener('submit', async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
|
@ -332,11 +356,24 @@ const htmlForm = `
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', fileInput.files[0]);
|
formData.append('file', fileInput.files[0]);
|
||||||
|
|
||||||
|
let deletionTimestamp;
|
||||||
|
const deletionDate = document.getElementById('deletionDate').value;
|
||||||
|
const useSevenDays = document.getElementById('defaultSevenDays').checked;
|
||||||
|
|
||||||
|
if (useSevenDays) {
|
||||||
|
deletionTimestamp = getSevenDaysFromNow();
|
||||||
|
} else if (deletionDate) {
|
||||||
|
deletionTimestamp = new Date(deletionDate).getTime();
|
||||||
|
deletionTimestamp = roundToNextDay(deletionTimestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
formData.append('deletionTimestamp', deletionTimestamp);
|
||||||
|
|
||||||
const responseElement = document.getElementById('uploadResponse');
|
const responseElement = document.getElementById('uploadResponse');
|
||||||
responseElement.textContent = 'Uploading...';
|
responseElement.textContent = 'Uploading...';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('https://fileshare.jonasjones.dev/upload', {
|
const response = await fetch('/upload', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'X-Custom-Auth-Key': \`Bearer \${authKey}\`
|
'X-Custom-Auth-Key': \`Bearer \${authKey}\`
|
||||||
|
@ -353,22 +390,35 @@ const htmlForm = `
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Handle form submission for URL upload
|
||||||
document.getElementById('urlUploadForm').addEventListener('submit', async (e) => {
|
document.getElementById('urlUploadForm').addEventListener('submit', async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const fileUrl = document.getElementById('fileUrl').value;
|
const fileUrl = document.getElementById('fileUrl').value;
|
||||||
const authKey = document.getElementById('urlAuthKey').value;
|
const authKey = document.getElementById('urlAuthKey').value;
|
||||||
|
|
||||||
|
let deletionTimestamp;
|
||||||
|
const deletionDate = document.getElementById('urlDeletionDate').value;
|
||||||
|
const useSevenDays = document.getElementById('urlDefaultSevenDays').checked;
|
||||||
|
|
||||||
|
if (useSevenDays) {
|
||||||
|
deletionTimestamp = getSevenDaysFromNow();
|
||||||
|
} else if (deletionDate) {
|
||||||
|
deletionTimestamp = new Date(deletionDate).getTime();
|
||||||
|
deletionTimestamp = roundToNextDay(deletionTimestamp);
|
||||||
|
}
|
||||||
|
|
||||||
const responseElement = document.getElementById('urlUploadResponse');
|
const responseElement = document.getElementById('urlUploadResponse');
|
||||||
responseElement.textContent = 'Downloading and uploading...';
|
responseElement.textContent = 'Downloading and uploading...';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch('https://fileshare.jonasjones.dev/upload-url', {
|
const response = await fetch('\/upload-url', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'X-Custom-Auth-Key': \`Bearer \${authKey}\`,
|
'X-Custom-Auth-Key': \`Bearer \${authKey}\`,
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ url: fileUrl, authKey: \`Bearer \${authKey}\` })
|
body: JSON.stringify({ url: fileUrl, authKey: \`Bearer \${authKey}\`, deletionTimestamp })
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
@ -380,6 +430,7 @@ const htmlForm = `
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Handle file deletion
|
||||||
document.getElementById('deleteForm').addEventListener('submit', async (e) => {
|
document.getElementById('deleteForm').addEventListener('submit', async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
|
@ -391,7 +442,7 @@ const htmlForm = `
|
||||||
responseElement.textContent = 'Deleting...';
|
responseElement.textContent = 'Deleting...';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(\`https://fileshare.jonasjones.dev/delete/\${fileHash}\`, {
|
const response = await fetch(\`\/delete/\${fileHash}\`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: {
|
headers: {
|
||||||
'X-Custom-Auth-Key': \`Bearer \${authKey}\`
|
'X-Custom-Auth-Key': \`Bearer \${authKey}\`
|
||||||
|
@ -407,6 +458,7 @@ const htmlForm = `
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Fetch file list for deletion dropdown
|
||||||
async function fetchFileList() {
|
async function fetchFileList() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('https://cdn.jonasjones.dev/tempupload/index.json');
|
const response = await fetch('https://cdn.jonasjones.dev/tempupload/index.json');
|
||||||
|
@ -415,7 +467,7 @@ const htmlForm = `
|
||||||
fileSelect.innerHTML = '<option value="">Select a file to delete</option>';
|
fileSelect.innerHTML = '<option value="">Select a file to delete</option>';
|
||||||
files.forEach(file => {
|
files.forEach(file => {
|
||||||
const option = document.createElement('option');
|
const option = document.createElement('option');
|
||||||
const deletionDate = new Date(file.deletionTimestamp).toLocaleString();
|
const deletionDate = new Date(file.deletionTimestamp).toLocaleDateString();
|
||||||
option.value = file.hash;
|
option.value = file.hash;
|
||||||
option.textContent = \`\${file.originalName} (deletes on \${deletionDate}) - \${file.hash}\`;
|
option.textContent = \`\${file.originalName} (deletes on \${deletionDate}) - \${file.hash}\`;
|
||||||
fileSelect.appendChild(option);
|
fileSelect.appendChild(option);
|
||||||
|
@ -425,6 +477,7 @@ const htmlForm = `
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetch and display file table
|
||||||
async function fetchFileTable() {
|
async function fetchFileTable() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('https://cdn.jonasjones.dev/tempupload/index.json');
|
const response = await fetch('https://cdn.jonasjones.dev/tempupload/index.json');
|
||||||
|
@ -433,11 +486,11 @@ const htmlForm = `
|
||||||
fileTableBody.innerHTML = '';
|
fileTableBody.innerHTML = '';
|
||||||
files.forEach(file => {
|
files.forEach(file => {
|
||||||
const row = document.createElement('tr');
|
const row = document.createElement('tr');
|
||||||
const deletionDate = new Date(file.deletionTimestamp).toLocaleString();
|
const deletionDate = new Date(file.deletionTimestamp).toLocaleDateString();
|
||||||
row.innerHTML = \`
|
row.innerHTML = \`
|
||||||
<td>\${file.originalName}</td>
|
<td>\${file.originalName}</td>
|
||||||
<td>\${deletionDate}</td>
|
<td>\${deletionDate}</td>
|
||||||
<td><a href="https://fileshare.jonasjones.dev/download/\${file.hash}">\${file.hash}</a></td>
|
<td><a href="\/download/\${file.hash}">\${file.hash}</a></td>
|
||||||
\`;
|
\`;
|
||||||
fileTableBody.appendChild(row);
|
fileTableBody.appendChild(row);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue