π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Refactor save downloads
Anand Baburajan530553e1ed63
diff --git a/web/apps/albums/src/public-album/download/services/save.ts b/web/apps/albums/src/public-album/download/services/save.tsindex 3ff0f4e963..4beb8bdb83 100644--- a/web/apps/albums/src/public-album/download/services/save.ts+++ b/web/apps/albums/src/public-album/download/services/save.ts@@ -1,87 +1,7 @@import { downloadManager } from "@/public-album/download/services/download-manager";-import { assertionFailed } from "ente-base/assert";-import { nameAndExtension } from "ente-base/file-name";-import log from "ente-base/log";-import { saveAsFileAndRevokeObjectURL } from "ente-base/utils/web";-import type {- AddSaveGroup,- UpdateSaveGroup,-} from "ente-gallery/components/utils/save-groups";+import type { AddSaveGroup } from "ente-gallery/components/utils/save-groups";+import { downloadAndSaveFilesWeb } from "ente-gallery/services/browser-save";import type { EnteFile } from "ente-media/file";-import { fileFileName } from "ente-media/file-metadata";-import { FileType } from "ente-media/file-type";-import { decodeLivePhoto } from "ente-media/live-photo";-import type JSZip from "jszip";--/**- * Download limits optimized for different devices and browsers.- */-interface DownloadLimits {- /** Number of concurrent file downloads. */- concurrency: number;- /** Maximum size of a ZIP batch in bytes. */- maxZipSize: number;-}--let cachedLimits: DownloadLimits | undefined;-type JSZipConstructor = new () => JSZip;--let jsZipConstructorPromise: Promise<JSZipConstructor> | undefined;--const createJSZip = async (): Promise<JSZip> => {- const JSZipConstructor = await (jsZipConstructorPromise ??=- import("jszip").then((module) => {- const candidate = (- module as unknown as { default?: JSZipConstructor }- ).default;- return candidate ?? (module as unknown as JSZipConstructor);- }));- return new JSZipConstructor();-};--/**- * Get download limits for the current device.- *- * - Mobile devices: 4 concurrent, 100MB max- * - Desktop: 8 concurrent, 250MB max- */-const getDownloadLimits = (): DownloadLimits => {- if (cachedLimits) return cachedLimits;-- const ua = navigator.userAgent.toLowerCase();- const isMobile =- ua.includes("iphone") ||- ua.includes("ipad") ||- ua.includes("ipod") ||- ua.includes("android") ||- ua.includes("mobile") ||- ua.includes("tablet") ||- (ua.includes("macintosh") && navigator.maxTouchPoints > 1);-- cachedLimits = isMobile- ? { concurrency: 4, maxZipSize: 100 * 1024 * 1024 } // 100MB- : { concurrency: 8, maxZipSize: 250 * 1024 * 1024 }; // 250MB-- return cachedLimits;-};--const shouldIncludeZipNumber = (- files: EnteFile[],- maxZipSize: number,-): boolean => {- let totalSize = 0;- for (const file of files) {- const size = file.info?.fileSize;- if (size == null) {- return true;- }- totalSize += size;- if (totalSize > maxZipSize) {- return true;- }- }- return false;-};/*** Save the given {@link files} to the user's device.@@ -103,7 +23,13 @@ export const downloadAndSaveFiles = (files: EnteFile[],title: string,onAddSaveGroup: AddSaveGroup,-) => downloadAndSave(files, title, onAddSaveGroup);+) =>+ downloadAndSaveFilesWeb({+ downloader: downloadManager,+ files,+ title,+ onAddSaveGroup,+ });/*** Save all the files of a collection to the user's device.@@ -124,518 +50,11 @@ export const downloadAndSaveCollectionFiles = async (isHiddenCollectionSummary: boolean | undefined,onAddSaveGroup: AddSaveGroup,) =>- downloadAndSave(+ downloadAndSaveFilesWeb({+ downloader: downloadManager,files,- collectionSummaryName,+ title: collectionSummaryName,onAddSaveGroup,- collectionSummaryName,- collectionSummaryID,- isHiddenCollectionSummary,- );--/**- * The lower level primitive that the public API of this module delegates to.- */-const downloadAndSave = async (- files: EnteFile[],- title: string,- onAddSaveGroup: AddSaveGroup,- _collectionSummaryName?: string,- collectionSummaryID?: number,- isHiddenCollectionSummary?: boolean,-) => {- const total = files.length;- if (!files.length) {- // Nothing to download.- assertionFailed();- return;- }-- const downloadDirPath: string | undefined = undefined;- const shouldZipOnWeb =- files.length > 1 ||- (files.length === 1 &&- files[0]?.metadata.fileType === FileType.livePhoto);- const includeZipNumber =- shouldZipOnWeb &&- shouldIncludeZipNumber(files, getDownloadLimits().maxZipSize);-- const canceller = new AbortController();- const failedFiles: EnteFile[] = [];- let isDownloading = false;- let updateSaveGroup: UpdateSaveGroup = () => undefined;- // Track the next ZIP batch index across retries so part numbers continue- // sequentially (e.g., if initial download creates Parts 1-5 and fails,- // retry creates Part 6+ instead of starting over at Part 1).- let nextZipBatchIndex = 1;-- const downloadFilesWeb = async (- filesToDownload: EnteFile[],- resetFailedCount = false,- ) => {- if (!filesToDownload.length || isDownloading) return;-- // Reset counts first if this is a retry, before any other logic- if (resetFailedCount) {- updateSaveGroup((g) => ({- ...g,- failed: 0,- failureReason: undefined,- }));- failedFiles.length = 0;- }-- // If already offline, mark all files as failed so retry is available- if (!navigator.onLine) {- log.info("Download skipped - network is offline");- for (const file of filesToDownload) {- failedFiles.push(file);- }- updateSaveGroup((g) => ({- ...g,- failed: filesToDownload.length,- failureReason: "network_offline",- }));- return;- }-- isDownloading = true;- // Only clear on first download, not retry (already cleared above)- if (!resetFailedCount) {- failedFiles.length = 0;- }-- try {- // Single non-live-photo file: download directly without zipping- const singleFile = filesToDownload[0];- if (- filesToDownload.length === 1 &&- singleFile &&- singleFile.metadata.fileType !== FileType.livePhoto- ) {- try {- const fileBlob = await downloadManager.fileBlob(singleFile);- const fileName = fileFileName(singleFile);- const url = URL.createObjectURL(fileBlob);- saveAsFileAndRevokeObjectURL(url, fileName);- updateSaveGroup((g) => ({ ...g, success: g.success + 1 }));- } catch (e) {- log.error("File download failed", e);- failedFiles.push(singleFile);- updateSaveGroup((g) => ({ ...g, failed: g.failed + 1 }));- }- } else {- // Multiple files or live photo: use ZIP- nextZipBatchIndex = await saveAsZip(- filesToDownload,- title,- () =>- updateSaveGroup((g) => ({- ...g,- success: g.success + 1,- })),- (file) => {- failedFiles.push(file);- updateSaveGroup((g) => ({- ...g,- failed: g.failed + 1,- }));- },- canceller,- updateSaveGroup,- nextZipBatchIndex,- includeZipNumber,- );- }-- if (!failedFiles.length) {- updateSaveGroup((g) => ({ ...g, retry: undefined }));- }- } finally {- isDownloading = false;- }- };-- const retry = () => {- if (!failedFiles.length || isDownloading || canceller.signal.aborted)- return;- void downloadFilesWeb([...failedFiles], true);- };-- updateSaveGroup = onAddSaveGroup({- title,collectionSummaryID,isHiddenCollectionSummary,- downloadDirPath,- total,- includeZipNumber,- canceller,- retry,});-- await downloadFilesWeb(files);-};--/**- * A helper class to accumulate files into ZIP batches and download them when- * the batch size limit is reached.- */-class ZipBatcher {- private zipPromise = createJSZip();- private currentBatchSize = 0;- private currentFileCount = 0;- private batchIndex: number;- private usedNames = new Set<string>();- private baseName: string;- private maxZipSize: number;- private includePartNumber: boolean;- private onStateChange?: (- isDownloading: boolean,- partNumber: number,- ) => void;-- constructor(- baseName: string,- maxZipSize: number,- startingBatchIndex = 1,- onStateChange?: (isDownloading: boolean, partNumber: number) => void,- includePartNumber = false,- ) {- this.baseName = baseName;- this.maxZipSize = maxZipSize;- this.batchIndex = startingBatchIndex;- this.onStateChange = onStateChange;- this.includePartNumber = includePartNumber || startingBatchIndex > 1;- }-- /**- * Get the next batch index that would be used for the next ZIP file.- * This is useful for tracking progress across retries.- */- getNextBatchIndex(): number {- return this.batchIndex;- }-- /**- * Get the current batch index being processed.- */- getCurrentBatchIndex(): number {- return this.batchIndex;- }-- /**- * Add file data to the current ZIP batch. If adding this file would exceed- * the batch size limit, the current batch is downloaded first.- */- async addFile(data: Uint8Array | Blob, fileName: string): Promise<void> {- const size = data instanceof Blob ? data.size : data.byteLength;-- // If adding this file would exceed the limit and we have files in the- // batch, download the current batch first.- if (- this.currentBatchSize > 0 &&- this.currentBatchSize + size > this.maxZipSize- ) {- this.includePartNumber = true;- await this.downloadCurrentBatch();- // Notify that we're now preparing a new part- this.onStateChange?.(false, this.batchIndex);- }-- // Ensure unique file names within the ZIP- const uniqueName = this.getUniqueName(fileName);- this.usedNames.add(uniqueName);- const zip = await this.zipPromise;- zip.file(uniqueName, data);- this.currentBatchSize += size;- this.currentFileCount++;- }-- /**- * Download any remaining files in the current batch.- */- async flush(): Promise<void> {- if (this.currentBatchSize > 0) {- await this.downloadCurrentBatch();- }- }-- private async downloadCurrentBatch(): Promise<void> {- this.onStateChange?.(true, this.batchIndex);- try {- const zip = await this.zipPromise;- const zipBlob = await zip.generateAsync({ type: "blob" });- const fileLabel =- this.currentFileCount === 1- ? "1 file"- : `${this.currentFileCount} files`;- const baseName = this.baseName.trim();- const nameBase = this.includePartNumber- ? `${baseName} Part ${this.batchIndex}`- : baseName;- const zipName =- baseName.toLowerCase() === fileLabel.toLowerCase()- ? `${nameBase}.zip`- : `${nameBase} - ${fileLabel}.zip`;-- const url = URL.createObjectURL(zipBlob);- saveAsFileAndRevokeObjectURL(url, zipName);- } finally {- this.onStateChange?.(false, this.batchIndex);- }-- // Reset for next batch- this.zipPromise = createJSZip();- this.currentBatchSize = 0;- this.currentFileCount = 0;- this.usedNames.clear();- this.batchIndex++;- }-- /**- * Generate a unique file name within the ZIP by appending a suffix if the- * name already exists.- */- private getUniqueName(fileName: string): string {- if (!this.usedNames.has(fileName)) {- return fileName;- }-- const [name, ext] = nameAndExtension(fileName);- let counter = 1;- let uniqueName: string;- do {- uniqueName = ext- ? `${name}(${counter}).${ext}`- : `${name}(${counter})`;- counter++;- } while (this.usedNames.has(uniqueName));-- return uniqueName;- }-}--/** Result of downloading and processing a single file for ZIP inclusion. */-type DownloadedFileData =- | { type: "regular"; fileName: string; data: Uint8Array }- | {- type: "livePhoto";- imageFileName: string;- imageData: Uint8Array;- videoFileName: string;- videoData: Uint8Array;- };--/**- * Download and process a single file, returning the data ready for ZIP.- */-const downloadFileForZip = async (- file: EnteFile,-): Promise<DownloadedFileData> => {- const fileBlob = await downloadManager.fileBlob(file);- const fileName = fileFileName(file);-- if (file.metadata.fileType == FileType.livePhoto) {- const { imageFileName, imageData, videoFileName, videoData } =- await decodeLivePhoto(fileName, fileBlob);- return {- type: "livePhoto",- imageFileName,- imageData,- videoFileName,- videoData,- };- } else {- const data = new Uint8Array(await fileBlob.arrayBuffer());- return { type: "regular", fileName, data };- }-};--/**- * Save multiple files as ZIP archives to the user's download folder.- *- * Files are batched into ZIPs of up to 100MB each. If the total exceeds 100MB,- * multiple ZIP files will be downloaded. Downloads are performed concurrently- * (up to {@link CONCURRENT_DOWNLOADS} at a time) for better performance.- *- * @param files The files to download and add to the ZIP.- * @param baseName The base name for the ZIP file(s).- * @param onSuccess Callback invoked after each file is successfully added.- * @param onError Callback invoked when a file fails to download.- * @param canceller An AbortController to check for cancellation.- * @param startingBatchIndex The batch index to start from (for retries).- * @returns The next batch index to use for subsequent ZIPs (useful for retries).- */-const saveAsZip = async (- files: EnteFile[],- baseName: string,- onSuccess: () => void,- onError: (file: EnteFile, error: unknown) => void,- canceller: AbortController,- updateSaveGroup: UpdateSaveGroup,- startingBatchIndex = 1,- includePartNumber = false,-): Promise<number> => {- const { concurrency, maxZipSize } = getDownloadLimits();- const batcher = new ZipBatcher(- baseName,- maxZipSize,- startingBatchIndex,- (isDownloading, partNumber) =>- updateSaveGroup((g) => ({- ...g,- isDownloadingZip: isDownloading,- currentPart: partNumber,- })),- includePartNumber,- );-- // Set initial part number- updateSaveGroup((g) => ({ ...g, currentPart: startingBatchIndex }));-- // Queue of files to process- let fileIndex = 0;-- // Track if we've gone offline to stop processing immediately.- // Using an object so the value can be mutated by event handlers and- // checked synchronously by the async workers.- const networkState = { isOffline: !navigator.onLine };- const handleOffline = () => {- networkState.isOffline = true;- };- const handleOnline = () => {- networkState.isOffline = false;- };- window.addEventListener("offline", handleOffline);- window.addEventListener("online", handleOnline);-- // Mutex for serializing ZIP additions (download is concurrent, but adding- // to the ZIP must be serialized to avoid race conditions with batching)- let zipMutex: Promise<void> = Promise.resolve();- const withZipLock = async <T>(fn: () => Promise<T>): Promise<T> => {- const prev = zipMutex;- let resolve: () => void;- zipMutex = new Promise((r) => (resolve = r));- await prev;- try {- return await fn();- } finally {- resolve!();- }- };-- // Process a single file: download, then add to ZIP- const processFile = async (): Promise<boolean> => {- // Stop immediately if offline or cancelled- if (networkState.isOffline || canceller.signal.aborted) {- return false;- }-- // Get next file to process- const currentIndex = fileIndex++;- if (currentIndex >= files.length) {- return false;- }-- const file = files[currentIndex]!;- try {- // Check again before starting download (value can change via event handler)- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition- if (networkState.isOffline) {- // Put this file back for retry- onError(file, new Error("Network offline"));- return false;- }-- // Download happens concurrently- const downloadedData = await downloadFileForZip(file);-- // Adding to ZIP is serialized via mutex- await withZipLock(async () => {- if (downloadedData.type === "livePhoto") {- await batcher.addFile(- downloadedData.imageData,- downloadedData.imageFileName,- );- await batcher.addFile(- downloadedData.videoData,- downloadedData.videoFileName,- );- } else {- await batcher.addFile(- downloadedData.data,- downloadedData.fileName,- );- }- });- onSuccess();- } catch (e) {- // Individual file failed - mark it for retry but continue with others- // Only log non-network errors to avoid log spam when offline- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition- if (!networkState.isOffline) {- log.error(`Failed to download file ${file.id}, skipping`, e);- }- onError(file, e);-- // Only stop all processing if we went offline (not for individual failures)- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition- if (networkState.isOffline) {- updateSaveGroup((g) => ({- ...g,- failureReason: "network_offline",- }));- return false;- }- // Mark as file error for individual failures- updateSaveGroup((g) => ({- ...g,- failureReason: g.failureReason ?? "file_error",- }));- // Continue processing remaining files even if this one failed- }-- return true;- };-- // Worker that continuously processes files until done- const worker = async (): Promise<void> => {- while (await processFile()) {- // Continue processing- }- };-- try {- // Start concurrent workers- const workers = Array.from(- { length: Math.min(concurrency, files.length) },- () => worker(),- );- await Promise.all(workers);-- // If we went offline, mark remaining files as failed- if (networkState.isOffline) {- updateSaveGroup((g) => ({- ...g,- failureReason: "network_offline",- }));- while (fileIndex < files.length) {- const file = files[fileIndex++];- if (file) {- onError(file, new Error("Network offline"));- }- }- }-- // Flush whatever we have (even partial) unless cancelled- if (!canceller.signal.aborted) {- await batcher.flush();- }-- return batcher.getNextBatchIndex();- } finally {- // Clean up event listeners- window.removeEventListener("offline", handleOffline);- window.removeEventListener("online", handleOnline);- }-};diff --git a/web/packages/gallery/services/browser-save.ts b/web/packages/gallery/services/browser-save.tsnew file mode 100644index 0000000000..b40db07b11--- /dev/null+++ b/web/packages/gallery/services/browser-save.ts@@ -0,0 +1,594 @@+import { assertionFailed } from "ente-base/assert";+import { nameAndExtension } from "ente-base/file-name";+import log from "ente-base/log";+import { saveAsFileAndRevokeObjectURL } from "ente-base/utils/web";+import type { EnteFile } from "ente-media/file";+import { fileFileName } from "ente-media/file-metadata";+import { FileType } from "ente-media/file-type";+import { decodeLivePhoto } from "ente-media/live-photo";+import type JSZip from "jszip";+import type {+ AddSaveGroup,+ UpdateSaveGroup,+} from "../components/utils/save-groups";++export interface BrowserSaveDownloader {+ fileBlob(file: EnteFile): Promise<Blob>;+}++export interface DownloadAndSaveFilesWebOpts {+ downloader: BrowserSaveDownloader;+ files: EnteFile[];+ title: string;+ onAddSaveGroup: AddSaveGroup;+ collectionSummaryID?: number;+ isHiddenCollectionSummary?: boolean;+}++/**+ * Download limits optimized for different devices and browsers.+ */+interface DownloadLimits {+ /** Number of concurrent file downloads. */+ concurrency: number;+ /** Maximum size of a ZIP batch in bytes. */+ maxZipSize: number;+}++let cachedLimits: DownloadLimits | undefined;+type JSZipConstructor = new () => JSZip;++let jsZipConstructorPromise: Promise<JSZipConstructor> | undefined;++const createJSZip = async (): Promise<JSZip> => {+ const JSZipConstructor = await (jsZipConstructorPromise ??=+ import("jszip").then((module) => {+ const candidate = (+ module as unknown as { default?: JSZipConstructor }+ ).default;+ return candidate ?? (module as unknown as JSZipConstructor);+ }));+ return new JSZipConstructor();+};++/**+ * Get download limits for the current device.+ *+ * - Mobile devices: 4 concurrent, 100MB max+ * - Desktop: 8 concurrent, 250MB max+ */+const getDownloadLimits = (): DownloadLimits => {+ if (cachedLimits) return cachedLimits;++ const ua = navigator.userAgent.toLowerCase();+ const isMobile =+ ua.includes("iphone") ||+ ua.includes("ipad") ||+ ua.includes("ipod") ||+ ua.includes("android") ||+ ua.includes("mobile") ||+ ua.includes("tablet") ||+ (ua.includes("macintosh") && navigator.maxTouchPoints > 1);++ cachedLimits = isMobile+ ? { concurrency: 4, maxZipSize: 100 * 1024 * 1024 } // 100MB+ : { concurrency: 8, maxZipSize: 250 * 1024 * 1024 }; // 250MB++ return cachedLimits;+};++const shouldIncludeZipNumber = (+ files: EnteFile[],+ maxZipSize: number,+): boolean => {+ let totalSize = 0;+ for (const file of files) {+ const size = file.info?.fileSize;+ if (size == null) {+ return true;+ }+ totalSize += size;+ if (totalSize > maxZipSize) {+ return true;+ }+ }+ return false;+};++export const downloadAndSaveFilesWeb = async ({+ downloader,+ files,+ title,+ onAddSaveGroup,+ collectionSummaryID,+ isHiddenCollectionSummary,+}: DownloadAndSaveFilesWebOpts) => {+ const total = files.length;+ if (!files.length) {+ // Nothing to download.+ assertionFailed();+ return;+ }++ const shouldZipOnWeb =+ files.length > 1 ||+ (files.length === 1 &&+ files[0]?.metadata.fileType === FileType.livePhoto);+ const includeZipNumber =+ shouldZipOnWeb &&+ shouldIncludeZipNumber(files, getDownloadLimits().maxZipSize);++ const canceller = new AbortController();+ const failedFiles: EnteFile[] = [];+ let isDownloading = false;+ let updateSaveGroup: UpdateSaveGroup = () => undefined;+ // Track the next ZIP batch index across retries so part numbers continue+ // sequentially (e.g., if initial download creates Parts 1-5 and fails,+ // retry creates Part 6+ instead of starting over at Part 1).+ let nextZipBatchIndex = 1;++ const downloadFilesWeb = async (+ filesToDownload: EnteFile[],+ resetFailedCount = false,+ ) => {+ if (!filesToDownload.length || isDownloading) return;++ // Reset counts first if this is a retry, before any other logic+ if (resetFailedCount) {+ updateSaveGroup((g) => ({+ ...g,+ failed: 0,+ failureReason: undefined,+ }));+ failedFiles.length = 0;+ }++ // If already offline, mark all files as failed so retry is available+ if (!navigator.onLine) {+ log.info("Download skipped - network is offline");+ for (const file of filesToDownload) {+ failedFiles.push(file);+ }+ updateSaveGroup((g) => ({+ ...g,+ failed: filesToDownload.length,+ failureReason: "network_offline",+ }));+ return;+ }++ isDownloading = true;+ // Only clear on first download, not retry (already cleared above)+ if (!resetFailedCount) {+ failedFiles.length = 0;+ }++ try {+ // Single non-live-photo file: download directly without zipping+ const singleFile = filesToDownload[0];+ if (+ filesToDownload.length === 1 &&+ singleFile &&+ singleFile.metadata.fileType !== FileType.livePhoto+ ) {+ try {+ const fileBlob = await downloader.fileBlob(singleFile);+ const fileName = fileFileName(singleFile);+ const url = URL.createObjectURL(fileBlob);+ saveAsFileAndRevokeObjectURL(url, fileName);+ updateSaveGroup((g) => ({ ...g, success: g.success + 1 }));+ } catch (e) {+ log.error("File download failed", e);+ failedFiles.push(singleFile);+ updateSaveGroup((g) => ({ ...g, failed: g.failed + 1 }));+ }+ } else {+ // Multiple files or live photo: use ZIP+ nextZipBatchIndex = await saveAsZip(+ downloader,+ filesToDownload,+ title,+ () =>+ updateSaveGroup((g) => ({+ ...g,+ success: g.success + 1,+ })),+ (file) => {+ failedFiles.push(file);+ updateSaveGroup((g) => ({+ ...g,+ failed: g.failed + 1,+ }));+ },+ canceller,+ updateSaveGroup,+ nextZipBatchIndex,+ includeZipNumber,+ );+ }++ if (!failedFiles.length) {+ updateSaveGroup((g) => ({ ...g, retry: undefined }));+ }+ } finally {+ isDownloading = false;+ }+ };++ const retry = () => {+ if (!failedFiles.length || isDownloading || canceller.signal.aborted)+ return;+ void downloadFilesWeb([...failedFiles], true);+ };++ updateSaveGroup = onAddSaveGroup({+ title,+ collectionSummaryID,+ isHiddenCollectionSummary,+ total,+ includeZipNumber,+ canceller,+ retry,+ });++ await downloadFilesWeb(files);+};++/**+ * A helper class to accumulate files into ZIP batches and download them when+ * the batch size limit is reached.+ */+class ZipBatcher {+ private zipPromise = createJSZip();+ private currentBatchSize = 0;+ private currentFileCount = 0;+ private batchIndex: number;+ private usedNames = new Set<string>();+ private baseName: string;+ private maxZipSize: number;+ private includePartNumber: boolean;+ private onStateChange?: (+ isDownloading: boolean,+ partNumber: number,+ ) => void;++ constructor(+ baseName: string,+ maxZipSize: number,+ startingBatchIndex = 1,+ onStateChange?: (isDownloading: boolean, partNumber: number) => void,+ includePartNumber = false,+ ) {+ this.baseName = baseName;+ this.maxZipSize = maxZipSize;+ this.batchIndex = startingBatchIndex;+ this.onStateChange = onStateChange;+ this.includePartNumber = includePartNumber || startingBatchIndex > 1;+ }++ /**+ * Get the next batch index that would be used for the next ZIP file.+ * This is useful for tracking progress across retries.+ */+ getNextBatchIndex(): number {+ return this.batchIndex;+ }++ /**+ * Add file data to the current ZIP batch. If adding this file would exceed+ * the batch size limit, the current batch is downloaded first.+ */+ async addFile(data: Uint8Array | Blob, fileName: string): Promise<void> {+ const size = data instanceof Blob ? data.size : data.byteLength;++ // If adding this file would exceed the limit and we have files in the+ // batch, download the current batch first.+ if (+ this.currentBatchSize > 0 &&+ this.currentBatchSize + size > this.maxZipSize+ ) {+ this.includePartNumber = true;+ await this.downloadCurrentBatch();+ // Notify that we're now preparing a new part+ this.onStateChange?.(false, this.batchIndex);+ }++ // Ensure unique file names within the ZIP+ const uniqueName = this.getUniqueName(fileName);+ this.usedNames.add(uniqueName);+ const zip = await this.zipPromise;+ zip.file(uniqueName, data);+ this.currentBatchSize += size;+ this.currentFileCount++;+ }++ /**+ * Download any remaining files in the current batch.+ */+ async flush(): Promise<void> {+ if (this.currentBatchSize > 0) {+ await this.downloadCurrentBatch();+ }+ }++ private async downloadCurrentBatch(): Promise<void> {+ this.onStateChange?.(true, this.batchIndex);+ try {+ const zip = await this.zipPromise;+ const zipBlob = await zip.generateAsync({ type: "blob" });+ const fileLabel =+ this.currentFileCount === 1+ ? "1 file"+ : `${this.currentFileCount} files`;+ const baseName = this.baseName.trim();+ const nameBase = this.includePartNumber+ ? `${baseName} Part ${this.batchIndex}`+ : baseName;+ const zipName =+ baseName.toLowerCase() === fileLabel.toLowerCase()+ ? `${nameBase}.zip`+ : `${nameBase} - ${fileLabel}.zip`;++ const url = URL.createObjectURL(zipBlob);+ saveAsFileAndRevokeObjectURL(url, zipName);+ } finally {+ this.onStateChange?.(false, this.batchIndex);+ }++ // Reset for next batch+ this.zipPromise = createJSZip();+ this.currentBatchSize = 0;+ this.currentFileCount = 0;+ this.usedNames.clear();+ this.batchIndex++;+ }++ /**+ * Generate a unique file name within the ZIP by appending a suffix if the+ * name already exists.+ */+ private getUniqueName(fileName: string): string {+ if (!this.usedNames.has(fileName)) {+ return fileName;+ }++ const [name, ext] = nameAndExtension(fileName);+ let counter = 1;+ let uniqueName: string;+ do {+ uniqueName = ext+ ? `${name}(${counter}).${ext}`+ : `${name}(${counter})`;+ counter++;+ } while (this.usedNames.has(uniqueName));++ return uniqueName;+ }+}++/** Result of downloading and processing a single file for ZIP inclusion. */+type DownloadedFileData =+ | { type: "regular"; fileName: string; data: Uint8Array }+ | {+ type: "livePhoto";+ imageFileName: string;+ imageData: Uint8Array;+ videoFileName: string;+ videoData: Uint8Array;+ };++/**+ * Download and process a single file, returning the data ready for ZIP.+ */+const downloadFileForZip = async (+ downloader: BrowserSaveDownloader,+ file: EnteFile,+): Promise<DownloadedFileData> => {+ const fileBlob = await downloader.fileBlob(file);+ const fileName = fileFileName(file);++ if (file.metadata.fileType == FileType.livePhoto) {+ const { imageFileName, imageData, videoFileName, videoData } =+ await decodeLivePhoto(fileName, fileBlob);+ return {+ type: "livePhoto",+ imageFileName,+ imageData,+ videoFileName,+ videoData,+ };+ } else {+ const data = new Uint8Array(await fileBlob.arrayBuffer());+ return { type: "regular", fileName, data };+ }+};++/**+ * Save multiple files as ZIP archives to the user's download folder.+ *+ * Files are batched into ZIPs using device-specific size limits. If the total+ * exceeds that limit, multiple ZIP files will be downloaded. Downloads are+ * performed concurrently for better performance.+ *+ * @param files The files to download and add to the ZIP.+ * @param baseName The base name for the ZIP file(s).+ * @param onSuccess Callback invoked after each file is successfully added.+ * @param onError Callback invoked when a file fails to download.+ * @param canceller An AbortController to check for cancellation.+ * @param startingBatchIndex The batch index to start from (for retries).+ * @returns The next batch index to use for subsequent ZIPs (useful for retries).+ */+const saveAsZip = async (+ downloader: BrowserSaveDownloader,+ files: EnteFile[],+ baseName: string,+ onSuccess: () => void,+ onError: (file: EnteFile, error: unknown) => void,+ canceller: AbortController,+ updateSaveGroup: UpdateSaveGroup,+ startingBatchIndex = 1,+ includePartNumber = false,+): Promise<number> => {+ const { concurrency, maxZipSize } = getDownloadLimits();+ const batcher = new ZipBatcher(+ baseName,+ maxZipSize,+ startingBatchIndex,+ (isDownloading, partNumber) =>+ updateSaveGroup((g) => ({+ ...g,+ isDownloadingZip: isDownloading,+ currentPart: partNumber,+ })),+ includePartNumber,+ );++ // Set initial part number+ updateSaveGroup((g) => ({ ...g, currentPart: startingBatchIndex }));++ // Queue of files to process+ let fileIndex = 0;++ // Track if we've gone offline to stop processing immediately.+ // Using an object so the value can be mutated by event handlers and+ // checked synchronously by the async workers.+ const networkState = { isOffline: !navigator.onLine };+ const handleOffline = () => {+ networkState.isOffline = true;+ };+ const handleOnline = () => {+ networkState.isOffline = false;+ };+ window.addEventListener("offline", handleOffline);+ window.addEventListener("online", handleOnline);++ // Mutex for serializing ZIP additions (download is concurrent, but adding+ // to the ZIP must be serialized to avoid race conditions with batching)+ let zipMutex: Promise<void> = Promise.resolve();+ const withZipLock = async <T>(fn: () => Promise<T>): Promise<T> => {+ const prev = zipMutex;+ let resolve: () => void;+ zipMutex = new Promise((r) => (resolve = r));+ await prev;+ try {+ return await fn();+ } finally {+ resolve!();+ }+ };++ // Process a single file: download, then add to ZIP+ const processFile = async (): Promise<boolean> => {+ // Stop immediately if offline or cancelled+ if (networkState.isOffline || canceller.signal.aborted) {+ return false;+ }++ // Get next file to process+ const currentIndex = fileIndex++;+ if (currentIndex >= files.length) {+ return false;+ }++ const file = files[currentIndex]!;+ try {+ // Check again before starting download (value can change via event handler)+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition+ if (networkState.isOffline) {+ // Put this file back for retry+ onError(file, new Error("Network offline"));+ return false;+ }++ // Download happens concurrently+ const downloadedData = await downloadFileForZip(downloader, file);++ // Adding to ZIP is serialized via mutex+ await withZipLock(async () => {+ if (downloadedData.type === "livePhoto") {+ await batcher.addFile(+ downloadedData.imageData,+ downloadedData.imageFileName,+ );+ await batcher.addFile(+ downloadedData.videoData,+ downloadedData.videoFileName,+ );+ } else {+ await batcher.addFile(+ downloadedData.data,+ downloadedData.fileName,+ );+ }+ });+ onSuccess();+ } catch (e) {+ // Individual file failed - mark it for retry but continue with others+ // Only log non-network errors to avoid log spam when offline+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition+ if (!networkState.isOffline) {+ log.error(`Failed to download file ${file.id}, skipping`, e);+ }+ onError(file, e);++ // Only stop all processing if we went offline (not for individual failures)+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition+ if (networkState.isOffline) {+ updateSaveGroup((g) => ({+ ...g,+ failureReason: "network_offline",+ }));+ return false;+ }+ // Mark as file error for individual failures+ updateSaveGroup((g) => ({+ ...g,+ failureReason: g.failureReason ?? "file_error",+ }));+ // Continue processing remaining files even if this one failed+ }++ return true;+ };++ // Worker that continuously processes files until done+ const worker = async (): Promise<void> => {+ while (await processFile()) {+ // Continue processing+ }+ };++ try {+ // Start concurrent workers+ const workers = Array.from(+ { length: Math.min(concurrency, files.length) },+ () => worker(),+ );+ await Promise.all(workers);++ // If we went offline, mark remaining files as failed+ if (networkState.isOffline) {+ updateSaveGroup((g) => ({+ ...g,+ failureReason: "network_offline",+ }));+ while (fileIndex < files.length) {+ const file = files[fileIndex++];+ if (file) {+ onError(file, new Error("Network offline"));+ }+ }+ }++ // Flush whatever we have (even partial) unless cancelled+ if (!canceller.signal.aborted) {+ await batcher.flush();+ }++ return batcher.getNextBatchIndex();+ } finally {+ // Clean up event listeners+ window.removeEventListener("offline", handleOffline);+ window.removeEventListener("online", handleOnline);+ }+};diff --git a/web/packages/gallery/services/save.ts b/web/packages/gallery/services/save.tsindex 51f42ed3c0..bf5bed585b 100644--- a/web/packages/gallery/services/save.ts+++ b/web/packages/gallery/services/save.ts@@ -1,9 +1,8 @@import { assertionFailed } from "ente-base/assert";import { suppressMainWindowBlurForTrustedPrompt } from "ente-base/electron";-import { joinPath, nameAndExtension } from "ente-base/file-name";+import { joinPath } from "ente-base/file-name";import log from "ente-base/log";import { type Electron } from "ente-base/types/ipc";-import { saveAsFileAndRevokeObjectURL } from "ente-base/utils/web";import { downloadManager } from "ente-gallery/services/download";import { writeStream } from "ente-gallery/utils/native-stream";import type { EnteFile } from "ente-media/file";@@ -14,67 +13,11 @@ import {safeDirectoryName,safeFileName,} from "ente-new/photos/utils/native-fs";-import JSZip from "jszip";import type {AddSaveGroup,UpdateSaveGroup,} from "../components/utils/save-groups";--/**- * Download limits optimized for different devices and browsers.- */-interface DownloadLimits {- /** Number of concurrent file downloads. */- concurrency: number;- /** Maximum size of a ZIP batch in bytes. */- maxZipSize: number;-}--let cachedLimits: DownloadLimits | undefined;--/**- * Get download limits for the current device.- *- * - Mobile devices: 4 concurrent, 100MB max- * - Desktop: 8 concurrent, 250MB max- */-const getDownloadLimits = (): DownloadLimits => {- if (cachedLimits) return cachedLimits;-- const ua = navigator.userAgent.toLowerCase();- const isMobile =- ua.includes("iphone") ||- ua.includes("ipad") ||- ua.includes("ipod") ||- ua.includes("android") ||- ua.includes("mobile") ||- ua.includes("tablet") ||- (ua.includes("macintosh") && navigator.maxTouchPoints > 1);-- cachedLimits = isMobile- ? { concurrency: 4, maxZipSize: 100 * 1024 * 1024 } // 100MB- : { concurrency: 8, maxZipSize: 250 * 1024 * 1024 }; // 250MB-- return cachedLimits;-};--const shouldIncludeZipNumber = (- files: EnteFile[],- maxZipSize: number,-): boolean => {- let totalSize = 0;- for (const file of files) {- const size = file.info?.fileSize;- if (size == null) {- return true;- }- totalSize += size;- if (totalSize > maxZipSize) {- return true;- }- }- return false;-};+import { downloadAndSaveFilesWeb } from "./browser-save";/*** Save the given {@link files} to the user's device.@@ -145,6 +88,16 @@ const downloadAndSave = async (isHiddenCollectionSummary?: boolean,) => {const electron = globalThis.electron;+ if (!electron) {+ return downloadAndSaveFilesWeb({+ downloader: downloadManager,+ files,+ title,+ onAddSaveGroup,+ collectionSummaryID,+ isHiddenCollectionSummary,+ });+ }const total = files.length;if (!files.length) {@@ -153,47 +106,30 @@ const downloadAndSave = async (return;}- let downloadDirPath: string | undefined;- if (electron) {- suppressMainWindowBlurForTrustedPrompt();- downloadDirPath = await electron.selectDirectory();- if (!downloadDirPath) {- // The user cancelled on the directory selection dialog.- return;- }- if (collectionSummaryName) {- downloadDirPath = await mkdirCollectionDownloadFolder(- electron,- downloadDirPath,- collectionSummaryName,- );- }+ suppressMainWindowBlurForTrustedPrompt();+ const selectedDirPath = await electron.selectDirectory();+ if (!selectedDirPath) {+ // The user cancelled on the directory selection dialog.+ return;}-- const shouldZipOnWeb =- !electron &&- (files.length > 1 ||- (files.length === 1 &&- files[0]?.metadata.fileType === FileType.livePhoto));- const includeZipNumber =- shouldZipOnWeb &&- shouldIncludeZipNumber(files, getDownloadLimits().maxZipSize);+ const downloadDirPath = collectionSummaryName+ ? await mkdirCollectionDownloadFolder(+ electron,+ selectedDirPath,+ collectionSummaryName,+ )+ : selectedDirPath;const canceller = new AbortController();const failedFiles: EnteFile[] = [];let isDownloading = false;let updateSaveGroup: UpdateSaveGroup = () => undefined;- // Track the next ZIP batch index across retries so part numbers continue- // sequentially (e.g., if initial download creates Parts 1-5 and fails,- // retry creates Part 6+ instead of starting over at Part 1).- let nextZipBatchIndex = 1;const downloadFilesDesktop = async (filesToDownload: EnteFile[],resetFailedCount = false,) => {if (!filesToDownload.length || isDownloading) return;- if (!electron || !downloadDirPath) return;isDownloading = true;if (resetFailedCount) {@@ -222,99 +158,10 @@ const downloadAndSave = async (}};- const downloadFilesWeb = async (- filesToDownload: EnteFile[],- resetFailedCount = false,- ) => {- if (!filesToDownload.length || isDownloading) return;-- // Reset counts first if this is a retry, before any other logic- if (resetFailedCount) {- updateSaveGroup((g) => ({- ...g,- failed: 0,- failureReason: undefined,- }));- failedFiles.length = 0;- }-- // If already offline, mark all files as failed so retry is available- if (!navigator.onLine) {- log.info("Download skipped - network is offline");- for (const file of filesToDownload) {- failedFiles.push(file);- }- updateSaveGroup((g) => ({- ...g,- failed: filesToDownload.length,- failureReason: "network_offline",- }));- return;- }-- isDownloading = true;- // Only clear on first download, not retry (already cleared above)- if (!resetFailedCount) {- failedFiles.length = 0;- }-- try {- // Single non-live-photo file: download directly without zipping- const singleFile = filesToDownload[0];- if (- filesToDownload.length === 1 &&- singleFile &&- singleFile.metadata.fileType !== FileType.livePhoto- ) {- try {- const fileBlob = await downloadManager.fileBlob(singleFile);- const fileName = fileFileName(singleFile);- const url = URL.createObjectURL(fileBlob);- saveAsFileAndRevokeObjectURL(url, fileName);- updateSaveGroup((g) => ({ ...g, success: g.success + 1 }));- } catch (e) {- log.error("File download failed", e);- failedFiles.push(singleFile);- updateSaveGroup((g) => ({ ...g, failed: g.failed + 1 }));- }- } else {- // Multiple files or live photo: use ZIP- nextZipBatchIndex = await saveAsZip(- filesToDownload,- title,- () =>- updateSaveGroup((g) => ({- ...g,- success: g.success + 1,- })),- (file) => {- failedFiles.push(file);- updateSaveGroup((g) => ({- ...g,- failed: g.failed + 1,- }));- },- canceller,- updateSaveGroup,- nextZipBatchIndex,- includeZipNumber,- );- }-- if (!failedFiles.length) {- updateSaveGroup((g) => ({ ...g, retry: undefined }));- }- } finally {- isDownloading = false;- }- };-- const downloadFiles = electron ? downloadFilesDesktop : downloadFilesWeb;-const retry = () => {if (!failedFiles.length || isDownloading || canceller.signal.aborted)return;- void downloadFiles([...failedFiles], true);+ void downloadFilesDesktop([...failedFiles], true);};updateSaveGroup = onAddSaveGroup({@@ -323,374 +170,12 @@ const downloadAndSave = async (isHiddenCollectionSummary,downloadDirPath,total,- includeZipNumber,+ includeZipNumber: false,canceller,retry,});- await downloadFiles(files);-};--/**- * A helper class to accumulate files into ZIP batches and download them when- * the batch size limit is reached.- */-class ZipBatcher {- private zip = new JSZip();- private currentBatchSize = 0;- private currentFileCount = 0;- private batchIndex: number;- private usedNames = new Set<string>();- private baseName: string;- private maxZipSize: number;- private includePartNumber: boolean;- private onStateChange?: (- isDownloading: boolean,- partNumber: number,- ) => void;-- constructor(- baseName: string,- maxZipSize: number,- startingBatchIndex = 1,- onStateChange?: (isDownloading: boolean, partNumber: number) => void,- includePartNumber = false,- ) {- this.baseName = baseName;- this.maxZipSize = maxZipSize;- this.batchIndex = startingBatchIndex;- this.onStateChange = onStateChange;- this.includePartNumber = includePartNumber || startingBatchIndex > 1;- }-- /**- * Get the next batch index that would be used for the next ZIP file.- * This is useful for tracking progress across retries.- */- getNextBatchIndex(): number {- return this.batchIndex;- }-- /**- * Get the current batch index being processed.- */- getCurrentBatchIndex(): number {- return this.batchIndex;- }-- /**- * Add file data to the current ZIP batch. If adding this file would exceed- * the batch size limit, the current batch is downloaded first.- */- async addFile(data: Uint8Array | Blob, fileName: string): Promise<void> {- const size = data instanceof Blob ? data.size : data.byteLength;-- // If adding this file would exceed the limit and we have files in the- // batch, download the current batch first.- if (- this.currentBatchSize > 0 &&- this.currentBatchSize + size > this.maxZipSize- ) {- this.includePartNumber = true;- await this.downloadCurrentBatch();- // Notify that we're now preparing a new part- this.onStateChange?.(false, this.batchIndex);- }-- // Ensure unique file names within the ZIP- const uniqueName = this.getUniqueName(fileName);- this.usedNames.add(uniqueName);- this.zip.file(uniqueName, data);- this.currentBatchSize += size;- this.currentFileCount++;- }-- /**- * Download any remaining files in the current batch.- */- async flush(): Promise<void> {- if (this.currentBatchSize > 0) {- await this.downloadCurrentBatch();- }- }-- private async downloadCurrentBatch(): Promise<void> {- this.onStateChange?.(true, this.batchIndex);- try {- const zipBlob = await this.zip.generateAsync({ type: "blob" });- const fileLabel =- this.currentFileCount === 1- ? "1 file"- : `${this.currentFileCount} files`;- const baseName = this.baseName.trim();- const nameBase = this.includePartNumber- ? `${baseName} Part ${this.batchIndex}`- : baseName;- const zipName =- baseName.toLowerCase() === fileLabel.toLowerCase()- ? `${nameBase}.zip`- : `${nameBase} - ${fileLabel}.zip`;-- const url = URL.createObjectURL(zipBlob);- saveAsFileAndRevokeObjectURL(url, zipName);- } finally {- this.onStateChange?.(false, this.batchIndex);- }-- // Reset for next batch- this.zip = new JSZip();- this.currentBatchSize = 0;- this.currentFileCount = 0;- this.usedNames.clear();- this.batchIndex++;- }-- /**- * Generate a unique file name within the ZIP by appending a suffix if the- * name already exists.- */- private getUniqueName(fileName: string): string {- if (!this.usedNames.has(fileName)) {- return fileName;- }-- const [name, ext] = nameAndExtension(fileName);- let counter = 1;- let uniqueName: string;- do {- uniqueName = ext- ? `${name}(${counter}).${ext}`- : `${name}(${counter})`;- counter++;- } while (this.usedNames.has(uniqueName));-- return uniqueName;- }-}--/** Result of downloading and processing a single file for ZIP inclusion. */-type DownloadedFileData =- | { type: "regular"; fileName: string; data: Uint8Array }- | {- type: "livePhoto";- imageFileName: string;- imageData: Uint8Array;- videoFileName: string;- videoData: Uint8Array;- };--/**- * Download and process a single file, returning the data ready for ZIP.- */-const downloadFileForZip = async (- file: EnteFile,-): Promise<DownloadedFileData> => {- const fileBlob = await downloadManager.fileBlob(file);- const fileName = fileFileName(file);-- if (file.metadata.fileType == FileType.livePhoto) {- const { imageFileName, imageData, videoFileName, videoData } =- await decodeLivePhoto(fileName, fileBlob);- return {- type: "livePhoto",- imageFileName,- imageData,- videoFileName,- videoData,- };- } else {- const data = new Uint8Array(await fileBlob.arrayBuffer());- return { type: "regular", fileName, data };- }-};--/**- * Save multiple files as ZIP archives to the user's download folder.- *- * Files are batched into ZIPs of up to 100MB each. If the total exceeds 100MB,- * multiple ZIP files will be downloaded. Downloads are performed concurrently- * (up to {@link CONCURRENT_DOWNLOADS} at a time) for better performance.- *- * @param files The files to download and add to the ZIP.- * @param baseName The base name for the ZIP file(s).- * @param onSuccess Callback invoked after each file is successfully added.- * @param onError Callback invoked when a file fails to download.- * @param canceller An AbortController to check for cancellation.- * @param startingBatchIndex The batch index to start from (for retries).- * @returns The next batch index to use for subsequent ZIPs (useful for retries).- */-const saveAsZip = async (- files: EnteFile[],- baseName: string,- onSuccess: () => void,- onError: (file: EnteFile, error: unknown) => void,- canceller: AbortController,- updateSaveGroup: UpdateSaveGroup,- startingBatchIndex = 1,- includePartNumber = false,-): Promise<number> => {- const { concurrency, maxZipSize } = getDownloadLimits();- const batcher = new ZipBatcher(- baseName,- maxZipSize,- startingBatchIndex,- (isDownloading, partNumber) =>- updateSaveGroup((g) => ({- ...g,- isDownloadingZip: isDownloading,- currentPart: partNumber,- })),- includePartNumber,- );-- // Set initial part number- updateSaveGroup((g) => ({ ...g, currentPart: startingBatchIndex }));-- // Queue of files to process- let fileIndex = 0;-- // Track if we've gone offline to stop processing immediately.- // Using an object so the value can be mutated by event handlers and- // checked synchronously by the async workers.- const networkState = { isOffline: !navigator.onLine };- const handleOffline = () => {- networkState.isOffline = true;- };- const handleOnline = () => {- networkState.isOffline = false;- };- window.addEventListener("offline", handleOffline);- window.addEventListener("online", handleOnline);-- // Mutex for serializing ZIP additions (download is concurrent, but adding- // to the ZIP must be serialized to avoid race conditions with batching)- let zipMutex: Promise<void> = Promise.resolve();- const withZipLock = async <T>(fn: () => Promise<T>): Promise<T> => {- const prev = zipMutex;- let resolve: () => void;- zipMutex = new Promise((r) => (resolve = r));- await prev;- try {- return await fn();- } finally {- resolve!();- }- };-- // Process a single file: download, then add to ZIP- const processFile = async (): Promise<boolean> => {- // Stop immediately if offline or cancelled- if (networkState.isOffline || canceller.signal.aborted) {- return false;- }-- // Get next file to process- const currentIndex = fileIndex++;- if (currentIndex >= files.length) {- return false;- }-- const file = files[currentIndex]!;- try {- // Check again before starting download (value can change via event handler)- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition- if (networkState.isOffline) {- // Put this file back for retry- onError(file, new Error("Network offline"));- return false;- }-- // Download happens concurrently- const downloadedData = await downloadFileForZip(file);-- // Adding to ZIP is serialized via mutex- await withZipLock(async () => {- if (downloadedData.type === "livePhoto") {- await batcher.addFile(- downloadedData.imageData,- downloadedData.imageFileName,- );- await batcher.addFile(- downloadedData.videoData,- downloadedData.videoFileName,- );- } else {- await batcher.addFile(- downloadedData.data,- downloadedData.fileName,- );- }- });- onSuccess();- } catch (e) {- // Individual file failed - mark it for retry but continue with others- // Only log non-network errors to avoid log spam when offline- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition- if (!networkState.isOffline) {- log.error(`Failed to download file ${file.id}, skipping`, e);- }- onError(file, e);-- // Only stop all processing if we went offline (not for individual failures)- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition- if (networkState.isOffline) {- updateSaveGroup((g) => ({- ...g,- failureReason: "network_offline",- }));- return false;- }- // Mark as file error for individual failures- updateSaveGroup((g) => ({- ...g,- failureReason: g.failureReason ?? "file_error",- }));- // Continue processing remaining files even if this one failed- }-- return true;- };-- // Worker that continuously processes files until done- const worker = async (): Promise<void> => {- while (await processFile()) {- // Continue processing- }- };-- try {- // Start concurrent workers- const workers = Array.from(- { length: Math.min(concurrency, files.length) },- () => worker(),- );- await Promise.all(workers);-- // If we went offline, mark remaining files as failed- if (networkState.isOffline) {- updateSaveGroup((g) => ({- ...g,- failureReason: "network_offline",- }));- while (fileIndex < files.length) {- const file = files[fileIndex++];- if (file) {- onError(file, new Error("Network offline"));- }- }- }-- // Flush whatever we have (even partial) unless cancelled- if (!canceller.signal.aborted) {- await batcher.flush();- }-- return batcher.getNextBatchIndex();- } finally {- // Clean up event listeners- window.removeEventListener("offline", handleOffline);- window.removeEventListener("online", handleOnline);- }+ await downloadFilesDesktop(files);};/**