mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
Add a "Download project" / "Download selected" flow that builds a server-side manifest of downloadable media, plus a selection mode with bulk delete for project videos. Gate viewer downloads behind a new project allowDownloads setting (default off, opt-in). Admins can always download; enabling on a public project allows anonymous visitors to download. Enforce the setting on every download surface (manifest, version, asset, watch, video routes) via canDownloadProjectMedia. Add rate limits for the manifest endpoint, host allowlisting for direct download URLs, and configurable file/byte caps. Closes #16 Closes #19
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
'use client';
|
|
|
|
const DOWNLOAD_STAGGER_MS = 500;
|
|
|
|
export type ProjectDownloadManifestFile = {
|
|
fileName: string;
|
|
url: string;
|
|
sizeBytes: number | null;
|
|
};
|
|
|
|
export type ProjectDownloadManifest = {
|
|
projectName: string;
|
|
files: ProjectDownloadManifestFile[];
|
|
totalFiles: number;
|
|
totalBytes: string | null;
|
|
};
|
|
|
|
function sleep(ms: number): Promise<void> {
|
|
return new Promise((resolve) => window.setTimeout(resolve, ms));
|
|
}
|
|
|
|
function triggerBrowserDownload(file: ProjectDownloadManifestFile): void {
|
|
const anchor = document.createElement('a');
|
|
anchor.href = file.url;
|
|
anchor.rel = 'noopener';
|
|
if (file.url.startsWith('/')) {
|
|
anchor.download = file.fileName;
|
|
}
|
|
document.body.appendChild(anchor);
|
|
anchor.click();
|
|
anchor.remove();
|
|
}
|
|
|
|
export async function runProjectDownloadManifest(manifest: ProjectDownloadManifest): Promise<void> {
|
|
for (let index = 0; index < manifest.files.length; index += 1) {
|
|
triggerBrowserDownload(manifest.files[index]!);
|
|
if (index < manifest.files.length - 1) {
|
|
await sleep(DOWNLOAD_STAGGER_MS);
|
|
}
|
|
}
|
|
}
|