mirror of
https://github.com/immich-app/immich
synced 2025-06-07 10:21:03 +00:00

* Fix download panel reactivity * Directly download individual files without buffering in memory * Fix shared link e2e download tests
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { derived, writable } from 'svelte/store';
|
|
|
|
export interface DownloadProgress {
|
|
progress: number;
|
|
total: number;
|
|
percentage: number;
|
|
abort: AbortController | null;
|
|
}
|
|
|
|
export const downloadAssets = writable<Record<string, DownloadProgress>>({});
|
|
|
|
export const isDownloading = derived(downloadAssets, ($downloadAssets) => {
|
|
return Object.keys($downloadAssets).length > 0;
|
|
});
|
|
|
|
const update = (key: string, value: Partial<DownloadProgress> | null) => {
|
|
downloadAssets.update((state) => {
|
|
const newState = { ...state };
|
|
|
|
if (value === null) {
|
|
delete newState[key];
|
|
return newState;
|
|
}
|
|
|
|
if (!newState[key]) {
|
|
newState[key] = { progress: 0, total: 0, percentage: 0, abort: null };
|
|
}
|
|
|
|
const item = newState[key];
|
|
Object.assign(item, value);
|
|
item.percentage = Math.min(Math.floor((item.progress / item.total) * 100), 100);
|
|
newState[key] = { ...item };
|
|
|
|
return newState;
|
|
});
|
|
};
|
|
|
|
export const downloadManager = {
|
|
add: (key: string, total: number, abort?: AbortController) => update(key, { total, abort }),
|
|
clear: (key: string) => update(key, null),
|
|
update: (key: string, progress: number, total?: number) => {
|
|
const download: Partial<DownloadProgress> = { progress };
|
|
if (total !== undefined) {
|
|
download.total = total;
|
|
}
|
|
update(key, download);
|
|
},
|
|
};
|