Files
OpenFrame/components/video-page/hooks/use-download-actions.ts
T
yusufipk 5821f73d38 feat: show live progress while downloading named files
Bunny/cross-origin downloads are fetched into a blob before saving, which
on large files or slow connections looked stuck (spinner only). Stream the
body through a counting transform and show real byte progress in a toast:
per-file percent for single downloads and file N/M + percent for bulk.

- Progress is measured from Content-Length + received bytes (not estimated).
- The blob is assembled by the browser from the stream (can be disk-backed),
  so we don't accumulate chunks in the JS heap.
- Only the blob path shows a toast; same-origin (R2/S3/MinIO) and the >10 GB
  fallback use the browser's native download UI.
2026-07-10 22:27:53 +07:00

195 lines
6.9 KiB
TypeScript

'use client';
import { useCallback, useState } from 'react';
import { toast } from 'sonner';
import type {
BunnyDownloadPreference,
Comment,
DownloadTarget,
Version,
VideoData,
} from '@/components/video-page/types';
import { resolvePublicBunnyCdnHostname } from '@/lib/bunny-cdn';
import {
downloadNamedFile,
downloadProgressLabel,
extensionFromUrl,
navigateDownload,
} from '@/lib/client/download-file';
function sanitizeDownloadFileName(value: string): string {
return value
.replace(/[<>:"/\\|?*\u0000-\u001F]/g, '-')
.replace(/\s+/g, ' ')
.trim();
}
function getAllowedHosts() {
const bunnyCdnHostname = resolvePublicBunnyCdnHostname();
return [
...(bunnyCdnHostname ? [bunnyCdnHostname] : []),
...(process.env.NEXT_PUBLIC_DIRECT_DOWNLOAD_ALLOWED_HOSTS ?? '').split(','),
]
.map((host) => host.trim().toLowerCase())
.filter(Boolean);
}
function getSafeDirectDownloadUrl(rawUrl: string): string | null {
try {
const parsed = new URL(rawUrl);
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
return null;
}
const allowedHosts = getAllowedHosts();
if (allowedHosts.length === 0) {
return null;
}
const normalizedHost = parsed.hostname.toLowerCase();
if (!allowedHosts.includes(normalizedHost)) {
return null;
}
return parsed.toString();
} catch {
return null;
}
}
interface UseDownloadActionsParams {
activeVersion: (Version & { comments: Comment[] }) | undefined;
video: VideoData | null;
}
export function useDownloadActions({ activeVersion, video }: UseDownloadActionsParams) {
const [activeDownloadTarget, setActiveDownloadTarget] = useState<DownloadTarget | null>(null);
const isDownloadingVideo = activeDownloadTarget !== null;
const startDownload = useCallback(
async (preference: BunnyDownloadPreference = 'compressed') => {
if (!activeVersion || !video || isDownloadingVideo) return;
if (!video.canDownload) {
toast.error('Download is disabled for this shared link');
return;
}
if (
activeVersion.providerId !== 'bunny' &&
activeVersion.providerId !== 'direct' &&
activeVersion.providerId !== 'r2'
) {
toast.error('This video source does not support direct download');
return;
}
const target: DownloadTarget = activeVersion.providerId === 'bunny' ? preference : 'direct';
setActiveDownloadTarget(target);
try {
let downloadUrl: string | null = null;
if (activeVersion.providerId === 'bunny') {
const prepareRes = await fetch(
`/api/versions/${activeVersion.id}/download?source=${preference}&prepare=1`,
{
cache: 'no-store',
}
);
if (!prepareRes.ok) {
const prepareBody = await prepareRes.json().catch(() => null);
const fallbackError =
preference === 'original'
? 'Original file is not available for this video'
: 'Compressed file is not available for this video';
const errorMessage =
typeof prepareBody?.error === 'string' ? prepareBody.error : fallbackError;
throw new Error(errorMessage);
}
downloadUrl = `/api/versions/${activeVersion.id}/download?source=${preference}`;
} else if (activeVersion.providerId === 'r2') {
if (!activeVersion.originalUrl.startsWith('/api/upload/video/')) {
throw new Error('Direct download URL is not allowed');
}
downloadUrl = activeVersion.originalUrl;
} else {
downloadUrl = getSafeDirectDownloadUrl(activeVersion.originalUrl);
if (!downloadUrl) {
throw new Error('Direct download URL is not allowed');
}
}
if (!downloadUrl) {
throw new Error('Missing download URL');
}
// File name: "<video title> <version label>" if the editor set a label
// for this version, otherwise "<video title> v<number>".
const versionLabel = activeVersion.versionLabel?.trim();
const baseName =
sanitizeDownloadFileName(
versionLabel
? `${video.title} ${versionLabel}`
: `${video.title} v${activeVersion.versionNumber}`
) || 'video';
if (activeVersion.providerId === 'r2') {
// Same-origin proxy: the download attribute applies and streams
// without buffering the whole file in memory (any size).
const ext = extensionFromUrl(activeVersion.originalUrl) || 'mp4';
navigateDownload(downloadUrl, `${baseName}.${ext}`);
} else {
// Bunny (CDN redirect) and direct hosts are cross-origin, so the
// download attribute is ignored on a plain navigation. Fetch the bytes
// (CORS is open) and save them with our filename — unless the file is
// over 10 GB, in which case downloadNamedFile returns false and we fall
// back to a plain navigation (streams to disk with the CDN's name).
const fallbackExt =
(activeVersion.providerId === 'direct'
? extensionFromUrl(activeVersion.originalUrl)
: '') || 'mp4';
// The file is pulled into the browser before it can be saved, which on
// a big file / slow connection takes a while with no native download UI
// — show live progress so it doesn't look stuck.
const toastId = `download-${activeVersion.id}`;
toast.loading(`Downloading “${baseName}”…`, { id: toastId, duration: Infinity });
const saved = await downloadNamedFile(downloadUrl, `${baseName}.${fallbackExt}`, (p) => {
toast.loading(`Downloading “${baseName}”`, {
id: toastId,
description: downloadProgressLabel(p),
duration: Infinity,
});
});
if (saved) {
toast.success(`“${baseName}” downloaded`, { id: toastId, duration: 4000 });
} else {
// Too large to buffer (or fetch blocked): let the browser download it
// directly (its own progress UI, CDN filename).
toast.dismiss(toastId);
navigateDownload(downloadUrl);
}
}
} catch (error) {
console.error('Failed to start video download:', error);
if (error instanceof Error && error.message === 'Direct download URL is not allowed') {
toast.error('This direct download host is not allowed');
} else if (error instanceof Error && error.message) {
toast.error(error.message);
} else {
toast.error('Failed to start download');
}
} finally {
setActiveDownloadTarget(null);
}
},
[activeVersion, video, isDownloadingVideo]
);
return {
activeDownloadTarget,
isDownloadingVideo,
startDownload,
};
}