feat: add background download manager with progress reporting

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-21 11:00:46 -04:00
parent 3e469a931f
commit 088def2ee4
7 changed files with 292 additions and 5 deletions

View File

@@ -1,7 +1,9 @@
import type { VideoMetadata } from '$lib/bindings/video';
import { startDownload } from '$lib/bindings/video';
import type { ThumbnailSpritesheet } from '$lib/bindings/mediaAnalysis';
export type SessionStatus = 'idle' | 'resolving' | 'ready' | 'error';
export type DownloadStatus = 'idle' | 'downloading' | 'complete' | 'failed';
export const session = $state({
status: 'idle' as SessionStatus,
@@ -16,6 +18,7 @@ export const session = $state({
isPlaying: false,
localFilePath: null as string | null,
downloadProgress: 0,
downloadStatus: 'idle' as DownloadStatus,
keyframePositions: [] as number[],
waveformPeaks: [] as number[],
thumbnailSpritesheets: [] as ThumbnailSpritesheet[],
@@ -33,6 +36,7 @@ export function setMetadata(meta: VideoMetadata) {
session.localFilePath = null;
session.downloadProgress = 0;
session.downloadStatus = 'idle';
session.keyframePositions = [];
session.waveformPeaks = [];
session.thumbnailSpritesheets = [];
@@ -49,6 +53,7 @@ function clearMediaFields() {
session.isPlaying = false;
session.localFilePath = null;
session.downloadProgress = 0;
session.downloadStatus = 'idle';
session.keyframePositions = [];
session.waveformPeaks = [];
session.thumbnailSpritesheets = [];
@@ -71,3 +76,39 @@ export function reset() {
session.status = 'idle';
session.error = null;
}
export async function beginDownload() {
if (!session.url) return;
session.downloadStatus = 'downloading';
session.downloadProgress = 0;
session.localFilePath = null;
try {
await startDownload(
session.url,
(percent) => {
session.downloadProgress = percent;
},
(path) => {
session.localFilePath = path;
},
(success, path) => {
if (success) {
session.downloadProgress = 1.0;
session.localFilePath = path;
session.downloadStatus = 'complete';
} else {
session.downloadStatus = 'failed';
}
},
(message) => {
session.downloadStatus = 'failed';
console.error('Download error:', message);
}
);
} catch (e) {
session.downloadStatus = 'failed';
console.error('Download failed:', e);
}
}