Dateitransfer: Server, Seiten, Tests und Docker-Dateien
Hochladen mit Upload-Code, kurzer Link zum Herunterladen, automatische Löschung nach Ablauf der Frist. Node.js ohne zusätzliche Pakete. - src/: HTTP-Server, Ablage auf der Platte, Zugriffsbegrenzung - pages/, public/: Oberfläche in Deutsch und Englisch - tools/: Transfers auflisten und löschen - test/: 34 automatische Tests - Dockerfile, compose.yaml: Betrieb als Container auf 127.0.0.1:8083 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
51ff6f9d40
commit
278194b707
31 files changed
+5099
No files matched your search
@@ -0,0 +1,100 @@
|
||||
// Download-Seite: zeigt die Dateien eines Transfers.
|
||||
import { ApiError, api, fileNameElement, fill, formatBytes, formatDate, formatDuration, getLang, onLangChange } from './common.js';
|
||||
|
||||
const TEXT = {
|
||||
de: {
|
||||
title: (n) => (n === 1 ? '1 Datei zum Herunterladen' : `${n} Dateien zum Herunterladen`),
|
||||
meta: '{size} · verfügbar bis {date}',
|
||||
metaTotal: 'Zusammen {size} · verfügbar bis {date}',
|
||||
download: 'Herunterladen',
|
||||
downloadName: '„{name}“ herunterladen',
|
||||
network: 'Der Server ist nicht erreichbar. Prüf bitte deine Internetverbindung.',
|
||||
rateLimited: 'Zu viele Anfragen. Bitte warte {time} und versuch es dann noch einmal.',
|
||||
generic: 'Die Dateiliste konnte nicht geladen werden (Fehler {code}).',
|
||||
},
|
||||
en: {
|
||||
title: (n) => (n === 1 ? '1 file to download' : `${n} files to download`),
|
||||
meta: '{size} · available until {date}',
|
||||
metaTotal: '{size} in total · available until {date}',
|
||||
download: 'Download',
|
||||
downloadName: 'Download “{name}”',
|
||||
network: 'The server cannot be reached. Please check your internet connection.',
|
||||
rateLimited: 'Too many requests. Please wait {time} and try again.',
|
||||
generic: 'The file list could not be loaded (error {code}).',
|
||||
},
|
||||
};
|
||||
|
||||
function t(key, params) {
|
||||
const value = TEXT[getLang()][key];
|
||||
return typeof value === 'function' ? value(params) : fill(value, params);
|
||||
}
|
||||
|
||||
const root = document.documentElement;
|
||||
const el = {
|
||||
title: document.getElementById('dl-title'),
|
||||
meta: document.getElementById('dl-meta'),
|
||||
list: document.getElementById('dl-list'),
|
||||
errorText: document.getElementById('dl-error-text'),
|
||||
retry: document.getElementById('dl-retry'),
|
||||
};
|
||||
|
||||
const id = (location.pathname.split('/')[2] ?? '').toLowerCase();
|
||||
let transfer = null;
|
||||
let failure = null;
|
||||
|
||||
function render() {
|
||||
if (root.dataset.state === 'ready' && transfer) {
|
||||
const count = transfer.files.length;
|
||||
el.title.textContent = t('title', count);
|
||||
el.meta.textContent = t(count === 1 ? 'meta' : 'metaTotal', {
|
||||
size: formatBytes(transfer.totalBytes),
|
||||
date: formatDate(transfer.expiresAt),
|
||||
});
|
||||
el.list.replaceChildren(
|
||||
...transfer.files.map((file, index) => {
|
||||
const item = document.createElement('li');
|
||||
const size = document.createElement('span');
|
||||
size.className = 'file-size';
|
||||
size.textContent = formatBytes(file.size);
|
||||
const link = document.createElement('a');
|
||||
link.className = 'button button-secondary button-small';
|
||||
link.href = `/d/${transfer.id}/${index}`;
|
||||
link.download = file.name;
|
||||
link.textContent = t('download');
|
||||
link.setAttribute('aria-label', t('downloadName', { name: file.name }));
|
||||
item.append(fileNameElement(file.name), size, link);
|
||||
return item;
|
||||
}),
|
||||
);
|
||||
} else if (root.dataset.state === 'error' && failure) {
|
||||
if (failure instanceof ApiError && failure.status === 0) {
|
||||
el.errorText.textContent = t('network');
|
||||
} else if (failure instanceof ApiError && failure.status === 429) {
|
||||
el.errorText.textContent = t('rateLimited', { time: formatDuration(Number(failure.body.retryAfter) || 60) });
|
||||
} else {
|
||||
el.errorText.textContent = t('generic', { code: failure?.code ?? failure?.status ?? '?' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function load() {
|
||||
// Der Server hat schon mitgeteilt, dass es den Link nicht gibt.
|
||||
if (root.dataset.state === 'missing') return;
|
||||
root.dataset.state = 'loading';
|
||||
try {
|
||||
transfer = await api('GET', `/api/transfers/${encodeURIComponent(id)}`);
|
||||
root.dataset.state = 'ready';
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && err.status === 404) {
|
||||
root.dataset.state = 'missing';
|
||||
} else {
|
||||
failure = err;
|
||||
root.dataset.state = 'error';
|
||||
}
|
||||
}
|
||||
render();
|
||||
}
|
||||
|
||||
el.retry.addEventListener('click', load);
|
||||
onLangChange(render);
|
||||
await load();
|
||||
Reference in new issue
Block a user