π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Move
Manav Rathi275c1d4705f1
diff --git a/web/apps/photos/src/components/Collections/CollectionHeader.tsx b/web/apps/photos/src/components/Collections/CollectionHeader.tsxindex df35d1b372..bbf99742bc 100644--- a/web/apps/photos/src/components/Collections/CollectionHeader.tsx+++ b/web/apps/photos/src/components/Collections/CollectionHeader.tsx@@ -55,7 +55,7 @@ import type { SetFilesDownloadProgressAttributesCreator } from "types/gallery";import {downloadCollectionHelper,downloadDefaultHiddenCollectionHelper,-} from "utils/collection";+} from "ente-gallery/services/save";export interface CollectionHeaderProps {collectionSummary: CollectionSummary;diff --git a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsxindex d97b5ac30c..6ef10876c5 100644--- a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx+++ b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx@@ -5,6 +5,7 @@ import {} from "components/Collections/CollectionShare";import type { TimeStampListItem } from "components/FileList";import { useModalVisibility } from "ente-base/components/utils/modal";+import { type FilesDownloadProgressAttributes } from "ente-gallery/services/save";import type { Collection } from "ente-media/collection";import {GalleryBarImpl,@@ -24,14 +25,12 @@ import { sortCollectionSummaries } from "services/collectionService";import {isFilesDownloadCancelled,isFilesDownloadCompleted,- type FilesDownloadProgressAttributes,} from "../FilesDownloadProgress";import { AlbumCastDialog } from "./AlbumCastDialog";import {CollectionHeader,type CollectionHeaderProps,} from "./CollectionHeader";-type GalleryBarAndListHeaderProps = Omit<GalleryBarImplProps,| "collectionSummaries"diff --git a/web/apps/photos/src/components/FileListWithViewer.tsx b/web/apps/photos/src/components/FileListWithViewer.tsxindex 07a1e42bb4..7881e2d2f2 100644--- a/web/apps/photos/src/components/FileListWithViewer.tsx+++ b/web/apps/photos/src/components/FileListWithViewer.tsx@@ -5,6 +5,10 @@ import {FileViewer,type FileViewerProps,} from "ente-gallery/components/viewer/FileViewer";+import {+ downloadSingleFile,+ type SetFilesDownloadProgressAttributesCreator,+} from "ente-gallery/services/save";import type { Collection } from "ente-media/collection";import type { EnteFile } from "ente-media/file";import { fileCreationTime, fileFileName } from "ente-media/file-metadata";@@ -14,8 +18,6 @@ import { t } from "i18next";import { useCallback, useMemo, useState } from "react";import AutoSizer from "react-virtualized-auto-sizer";import { uploadManager } from "services/upload-manager";-import type { SetFilesDownloadProgressAttributesCreator } from "types/gallery";-import { downloadSingleFile } from "utils/file";import {FileList,type FileListAnnotatedFile,diff --git a/web/apps/photos/src/components/FilesDownloadProgress.tsx b/web/apps/photos/src/components/FilesDownloadProgress.tsxindex da637938e3..87e628287c 100644--- a/web/apps/photos/src/components/FilesDownloadProgress.tsx+++ b/web/apps/photos/src/components/FilesDownloadProgress.tsx@@ -1,21 +1,10 @@// TODO: Audit this file/* eslint-disable react-refresh/only-export-components */import { useBaseContext } from "ente-base/context";+import type { FilesDownloadProgressAttributes } from "ente-gallery/services/save";import { Notification } from "ente-new/photos/components/Notification";import { t } from "i18next";-export interface FilesDownloadProgressAttributes {- id: number;- success: number;- failed: number;- total: number;- folderName: string;- collectionID: number;- isHidden: boolean;- downloadDirPath: string;- canceller: AbortController;-}-interface FilesDownloadProgressProps {attributesList: FilesDownloadProgressAttributes[];setAttributesList: (value: FilesDownloadProgressAttributes[]) => void;diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex 3db254fa7e..565cef4e5f 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -125,9 +125,11 @@ import { Trans } from "react-i18next";import { uploadManager } from "services/upload-manager";import type {SelectedState,+} from "types/gallery";+import type {SetFilesDownloadProgressAttributes,SetFilesDownloadProgressAttributesCreator,-} from "types/gallery";+} from "ente-gallery/services/save";import { getSelectedFiles, performFileOp } from "utils/file";/**diff --git a/web/apps/photos/src/pages/shared-albums.tsx b/web/apps/photos/src/pages/shared-albums.tsxindex 79e85a12c9..88b140a2ed 100644--- a/web/apps/photos/src/pages/shared-albums.tsx+++ b/web/apps/photos/src/pages/shared-albums.tsx@@ -8,10 +8,7 @@ import { Box, Button, IconButton, Stack, styled, Tooltip } from "@mui/material";import Typography from "@mui/material/Typography";import type { TimeStampListItem } from "components/FileList";import { FileListWithViewer } from "components/FileListWithViewer";-import {- FilesDownloadProgress,- type FilesDownloadProgressAttributes,-} from "components/FilesDownloadProgress";+import { FilesDownloadProgress } from "components/FilesDownloadProgress";import { Upload } from "components/Upload";import {AccountsPageContents,@@ -51,6 +48,13 @@ import {import log from "ente-base/log";import { FullScreenDropZone } from "ente-gallery/components/FullScreenDropZone";import { downloadManager } from "ente-gallery/services/download";+import {+ downloadCollectionFiles,+ downloadSelectedFiles,+ type FilesDownloadProgressAttributes,+ type SetFilesDownloadProgressAttributes,+ type SetFilesDownloadProgressAttributesCreator,+} from "ente-gallery/services/save";import { extractCollectionKeyFromShareURL } from "ente-gallery/services/share";import { updateShouldDisableCFUploadProxy } from "ente-gallery/services/upload";import { sortFiles } from "ente-gallery/utils/file";@@ -83,13 +87,8 @@ import { useRouter } from "next/router";import { useCallback, useEffect, useMemo, useRef, useState } from "react";import { type FileWithPath } from "react-dropzone";import { uploadManager } from "services/upload-manager";-import type {- SelectedState,- SetFilesDownloadProgressAttributes,- SetFilesDownloadProgressAttributesCreator,-} from "types/gallery";-import { downloadCollectionFiles } from "utils/collection";-import { downloadSelectedFiles, getSelectedFiles } from "utils/file";+import type { SelectedState } from "types/gallery";+import { getSelectedFiles } from "utils/file";import { PublicCollectionGalleryContext } from "utils/publicCollectionGallery";export default function PublicCollectionGallery() {diff --git a/web/apps/photos/src/types/gallery/index.ts b/web/apps/photos/src/types/gallery/index.tsindex 83500edab5..8a27adc307 100644--- a/web/apps/photos/src/types/gallery/index.ts+++ b/web/apps/photos/src/types/gallery/index.ts@@ -17,19 +17,7 @@ export type SetSelectedState = React.Dispatch<React.SetStateAction<SelectedState>>;export type SetLoading = React.Dispatch<React.SetStateAction<boolean>>;-export type SetFilesDownloadProgressAttributes = (- value:- | Partial<FilesDownloadProgressAttributes>- | ((- prev: FilesDownloadProgressAttributes,- ) => FilesDownloadProgressAttributes),-) => void;-export type SetFilesDownloadProgressAttributesCreator = (- folderName: string,- collectionID?: number,- isHidden?: boolean,-) => SetFilesDownloadProgressAttributes;export interface MergedSourceURL {original: string;diff --git a/web/apps/photos/src/utils/collection.ts b/web/apps/photos/src/utils/collection.tsdeleted file mode 100644index 50c2697da6..0000000000--- a/web/apps/photos/src/utils/collection.ts+++ /dev/null@@ -1,121 +0,0 @@-import { ensureElectron } from "ente-base/electron";-import { joinPath } from "ente-base/file-name";-import log from "ente-base/log";-import { uniqueFilesByID } from "ente-gallery/utils/file";-import type { EnteFile } from "ente-media/file";-import {- defaultHiddenCollectionUserFacingName,- findDefaultHiddenCollectionIDs,-} from "ente-new/photos/services/collection";-import { PseudoCollectionID } from "ente-new/photos/services/collection-summary";-import {- savedCollectionFiles,- savedCollections,-} from "ente-new/photos/services/photos-fdb";-import { safeDirectoryName } from "ente-new/photos/utils/native-fs";-import type {- SetFilesDownloadProgressAttributes,- SetFilesDownloadProgressAttributesCreator,-} from "types/gallery";-import { downloadFilesWithProgress } from "utils/file";--export async function downloadCollectionHelper(- collectionID: number,- setFilesDownloadProgressAttributes: SetFilesDownloadProgressAttributes,-) {- try {- const allFiles = await savedCollectionFiles();- const collectionFiles = allFiles.filter(- (file) => file.collectionID == collectionID,- );- const allCollections = await savedCollections();- const collection = allCollections.find(- (collection) => collection.id == collectionID,- );- if (!collection) {- throw Error("collection not found");- }- await downloadCollectionFiles(- collection.name,- collectionFiles,- setFilesDownloadProgressAttributes,- );- } catch (e) {- log.error("download collection failed ", e);- }-}--export async function downloadDefaultHiddenCollectionHelper(- setFilesDownloadProgressAttributesCreator: SetFilesDownloadProgressAttributesCreator,-) {- try {- const defaultHiddenCollectionsIDs = findDefaultHiddenCollectionIDs(- await savedCollections(),- );- const collectionFiles = await savedCollectionFiles();- const defaultHiddenCollectionFiles = uniqueFilesByID(- collectionFiles.filter((file) =>- defaultHiddenCollectionsIDs.has(file.collectionID),- ),- );- const setFilesDownloadProgressAttributes =- setFilesDownloadProgressAttributesCreator(- defaultHiddenCollectionUserFacingName,- PseudoCollectionID.hiddenItems,- true,- );-- await downloadCollectionFiles(- defaultHiddenCollectionUserFacingName,- defaultHiddenCollectionFiles,- setFilesDownloadProgressAttributes,- );- } catch (e) {- log.error("download hidden files failed ", e);- }-}--export async function downloadCollectionFiles(- collectionName: string,- collectionFiles: EnteFile[],- setFilesDownloadProgressAttributes: SetFilesDownloadProgressAttributes,-) {- if (!collectionFiles.length) {- return;- }- let downloadDirPath: string;- const electron = globalThis.electron;- if (electron) {- const selectedDir = await electron.selectDirectory();- if (!selectedDir) {- return;- }- downloadDirPath = await createCollectionDownloadFolder(- selectedDir,- collectionName,- );- }- await downloadFilesWithProgress(- collectionFiles,- downloadDirPath,- setFilesDownloadProgressAttributes,- );-}--async function createCollectionDownloadFolder(- downloadDirPath: string,- collectionName: string,-) {- const fs = ensureElectron().fs;- const collectionDownloadName = await safeDirectoryName(- downloadDirPath,- collectionName,- fs.exists,- );- const collectionDownloadPath = joinPath(- downloadDirPath,- collectionDownloadName,- );- await fs.mkdirIfNeeded(collectionDownloadPath);- return collectionDownloadPath;-}diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.tsindex df0c381204..d21dadf160 100644--- a/web/apps/photos/src/utils/file/index.ts+++ b/web/apps/photos/src/utils/file/index.ts@@ -1,15 +1,10 @@import type { LocalUser } from "ente-accounts/services/user";-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 { detectFileTypeInfo } from "ente-gallery/utils/detect-type";-import { writeStream } from "ente-gallery/utils/native-stream";+import {+ type SetFilesDownloadProgressAttributesCreator,+ downloadSelectedFiles,+} from "ente-gallery/services/save";import type { EnteFile } from "ente-media/file";-import { ItemVisibility, fileFileName } from "ente-media/file-metadata";-import { FileType } from "ente-media/file-type";-import { decodeLivePhoto } from "ente-media/live-photo";+import { ItemVisibility } from "ente-media/file-metadata";import { type FileOp } from "ente-new/photos/components/SelectedFileOptions";import {addToFavoritesCollection,@@ -18,14 +13,8 @@ import {moveToTrash,} from "ente-new/photos/services/collection";import { updateFilesVisibility } from "ente-new/photos/services/file";-import { safeFileName } from "ente-new/photos/utils/native-fs";-import { wait } from "ente-utils/promise";import { t } from "i18next";-import type {- SelectedState,- SetFilesDownloadProgressAttributes,- SetFilesDownloadProgressAttributesCreator,-} from "types/gallery";+import type { SelectedState } from "types/gallery";export function getSelectedFiles(selected: SelectedState,@@ -41,239 +30,6 @@ export function getSelectedFiles(return files.filter((file) => selectedFilesIDs.has(file.id));}-export async function getFileFromURL(fileURL: string, name: string) {- const fileBlob = await (await fetch(fileURL)).blob();- const fileFile = new File([fileBlob], name);- return fileFile;-}--export async function downloadFilesWithProgress(- files: EnteFile[],- downloadDirPath: string,- setFilesDownloadProgressAttributes: SetFilesDownloadProgressAttributes,-) {- if (!files.length) {- return;- }- const canceller = new AbortController();- const increaseSuccess = () => {- if (canceller.signal.aborted) return;- setFilesDownloadProgressAttributes((prev) => ({- ...prev,- success: prev.success + 1,- }));- };- const increaseFailed = () => {- if (canceller.signal.aborted) return;- setFilesDownloadProgressAttributes((prev) => ({- ...prev,- failed: prev.failed + 1,- }));- };- const isCancelled = () => canceller.signal.aborted;-- setFilesDownloadProgressAttributes({- downloadDirPath,- success: 0,- failed: 0,- total: files.length,- canceller,- });-- const electron = globalThis.electron;- if (electron) {- await downloadFilesDesktop(- electron,- files,- { increaseSuccess, increaseFailed, isCancelled },- downloadDirPath,- );- } else {- await downloadFiles(files, {- increaseSuccess,- increaseFailed,- isCancelled,- });- }-}--export async function downloadSelectedFiles(- files: EnteFile[],- setFilesDownloadProgressAttributes: SetFilesDownloadProgressAttributes,-) {- if (!files.length) {- return;- }- let downloadDirPath: string;- const electron = globalThis.electron;- if (electron) {- downloadDirPath = await electron.selectDirectory();- if (!downloadDirPath) {- return;- }- }- await downloadFilesWithProgress(- files,- downloadDirPath,- setFilesDownloadProgressAttributes,- );-}--export async function downloadSingleFile(- file: EnteFile,- setFilesDownloadProgressAttributes: SetFilesDownloadProgressAttributes,-) {- let downloadDirPath: string;- const electron = globalThis.electron;- if (electron) {- downloadDirPath = await electron.selectDirectory();- if (!downloadDirPath) {- return;- }- }- await downloadFilesWithProgress(- [file],- downloadDirPath,- setFilesDownloadProgressAttributes,- );-}--export async function downloadFiles(- files: EnteFile[],- progressBarUpdater: {- increaseSuccess: () => void;- increaseFailed: () => void;- isCancelled: () => boolean;- },-) {- for (const file of files) {- try {- if (progressBarUpdater?.isCancelled()) {- return;- }- await saveAsFile(file);- progressBarUpdater?.increaseSuccess();- } catch (e) {- log.error("download fail for file", e);- progressBarUpdater?.increaseFailed();- }- }-}--/**- * Save the given {@link EnteFile} as a file in the user's download folder.- */-const saveAsFile = async (file: EnteFile) => {- 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);-- await saveBlobPartAsFile(imageData, imageFileName);-- // Downloading multiple works everywhere except, you guessed it,- // Safari. Make up for their incompetence by adding a setTimeout.- await wait(300) /* arbitrary constant, 300ms */;- await saveBlobPartAsFile(videoData, videoFileName);- } else {- await saveBlobPartAsFile(fileBlob, fileName);- }-};--/**- * Save the given {@link blob} as a file in the user's download folder.- */-const saveBlobPartAsFile = async (blobPart: BlobPart, fileName: string) =>- createTypedObjectURL(blobPart, fileName).then((url) =>- saveAsFileAndRevokeObjectURL(url, fileName),- );--const createTypedObjectURL = async (blobPart: BlobPart, fileName: string) => {- const blob = blobPart instanceof Blob ? blobPart : new Blob([blobPart]);- const { mimeType } = await detectFileTypeInfo(new File([blob], fileName));- return URL.createObjectURL(new Blob([blob], { type: mimeType }));-};--async function downloadFilesDesktop(- electron: Electron,- files: EnteFile[],- progressBarUpdater: {- increaseSuccess: () => void;- increaseFailed: () => void;- isCancelled: () => boolean;- },- downloadPath: string,-) {- for (const file of files) {- try {- if (progressBarUpdater?.isCancelled()) {- return;- }- await downloadFileDesktop(electron, file, downloadPath);- progressBarUpdater?.increaseSuccess();- } catch (e) {- log.error("download fail for file", e);- progressBarUpdater?.increaseFailed();- }- }-}--async function downloadFileDesktop(- electron: Electron,- file: EnteFile,- downloadDir: string,-) {- const fs = electron.fs;-- const stream = await downloadManager.fileStream(file);- const fileName = fileFileName(file);-- if (file.metadata.fileType == FileType.livePhoto) {- const fileBlob = await new Response(stream).blob();- const { imageFileName, imageData, videoFileName, videoData } =- await decodeLivePhoto(fileName, fileBlob);- const imageExportName = await safeFileName(- downloadDir,- imageFileName,- fs.exists,- );- const imageStream = new Response(imageData).body;- await writeStream(- electron,- joinPath(downloadDir, imageExportName),- imageStream,- );- try {- const videoExportName = await safeFileName(- downloadDir,- videoFileName,- fs.exists,- );- const videoStream = new Response(videoData).body;- await writeStream(- electron,- joinPath(downloadDir, videoExportName),- videoStream,- );- } catch (e) {- await fs.rm(joinPath(downloadDir, imageExportName));- throw e;- }- } else {- const fileExportName = await safeFileName(- downloadDir,- fileName,- fs.exists,- );- await writeStream(- electron,- joinPath(downloadDir, fileExportName),- stream,- );- }-}-export const shouldShowAvatar = (file: EnteFile,user: LocalUser | undefined,diff --git a/web/packages/gallery/services/save.ts b/web/packages/gallery/services/save.tsnew file mode 100644index 0000000000..f081a870af--- /dev/null+++ b/web/packages/gallery/services/save.ts@@ -0,0 +1,381 @@+import { ensureElectron } from "ente-base/electron";+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 { detectFileTypeInfo } from "ente-gallery/utils/detect-type";+import { uniqueFilesByID } from "ente-gallery/utils/file";+import { writeStream } from "ente-gallery/utils/native-stream";+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 {+ defaultHiddenCollectionUserFacingName,+ findDefaultHiddenCollectionIDs,+} from "ente-new/photos/services/collection";+import { PseudoCollectionID } from "ente-new/photos/services/collection-summary";+import {+ savedCollectionFiles,+ savedCollections,+} from "ente-new/photos/services/photos-fdb";+import {+ safeDirectoryName,+ safeFileName,+} from "ente-new/photos/utils/native-fs";+import { wait } from "ente-utils/promise";++export interface FilesDownloadProgressAttributes {+ id: number;+ success: number;+ failed: number;+ total: number;+ folderName: string;+ collectionID: number;+ isHidden: boolean;+ downloadDirPath: string;+ canceller: AbortController;+}++export type SetFilesDownloadProgressAttributes = (+ value:+ | Partial<FilesDownloadProgressAttributes>+ | ((+ prev: FilesDownloadProgressAttributes,+ ) => FilesDownloadProgressAttributes),+) => void;++export type SetFilesDownloadProgressAttributesCreator = (+ folderName: string,+ collectionID?: number,+ isHidden?: boolean,+) => SetFilesDownloadProgressAttributes;++export async function downloadFilesWithProgress(+ files: EnteFile[],+ downloadDirPath: string,+ setFilesDownloadProgressAttributes: SetFilesDownloadProgressAttributes,+) {+ if (!files.length) {+ return;+ }+ const canceller = new AbortController();+ const increaseSuccess = () => {+ if (canceller.signal.aborted) return;+ setFilesDownloadProgressAttributes((prev) => ({+ ...prev,+ success: prev.success + 1,+ }));+ };+ const increaseFailed = () => {+ if (canceller.signal.aborted) return;+ setFilesDownloadProgressAttributes((prev) => ({+ ...prev,+ failed: prev.failed + 1,+ }));+ };+ const isCancelled = () => canceller.signal.aborted;++ setFilesDownloadProgressAttributes({+ downloadDirPath,+ success: 0,+ failed: 0,+ total: files.length,+ canceller,+ });++ const electron = globalThis.electron;+ if (electron) {+ await downloadFilesDesktop(+ electron,+ files,+ { increaseSuccess, increaseFailed, isCancelled },+ downloadDirPath,+ );+ } else {+ await downloadFiles(files, {+ increaseSuccess,+ increaseFailed,+ isCancelled,+ });+ }+}++export async function downloadSelectedFiles(+ files: EnteFile[],+ setFilesDownloadProgressAttributes: SetFilesDownloadProgressAttributes,+) {+ if (!files.length) {+ return;+ }+ let downloadDirPath: string;+ const electron = globalThis.electron;+ if (electron) {+ downloadDirPath = await electron.selectDirectory();+ if (!downloadDirPath) {+ return;+ }+ }+ await downloadFilesWithProgress(+ files,+ downloadDirPath,+ setFilesDownloadProgressAttributes,+ );+}++export async function downloadSingleFile(+ file: EnteFile,+ setFilesDownloadProgressAttributes: SetFilesDownloadProgressAttributes,+) {+ let downloadDirPath: string;+ const electron = globalThis.electron;+ if (electron) {+ downloadDirPath = await electron.selectDirectory();+ if (!downloadDirPath) {+ return;+ }+ }+ await downloadFilesWithProgress(+ [file],+ downloadDirPath,+ setFilesDownloadProgressAttributes,+ );+}++export async function downloadFiles(+ files: EnteFile[],+ progressBarUpdater: {+ increaseSuccess: () => void;+ increaseFailed: () => void;+ isCancelled: () => boolean;+ },+) {+ for (const file of files) {+ try {+ if (progressBarUpdater?.isCancelled()) {+ return;+ }+ await saveAsFile(file);+ progressBarUpdater?.increaseSuccess();+ } catch (e) {+ log.error("download fail for file", e);+ progressBarUpdater?.increaseFailed();+ }+ }+}++/**+ * Save the given {@link EnteFile} as a file in the user's download folder.+ */+const saveAsFile = async (file: EnteFile) => {+ 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);++ await saveBlobPartAsFile(imageData, imageFileName);++ // Downloading multiple works everywhere except, you guessed it,+ // Safari. Make up for their incompetence by adding a setTimeout.+ await wait(300) /* arbitrary constant, 300ms */;+ await saveBlobPartAsFile(videoData, videoFileName);+ } else {+ await saveBlobPartAsFile(fileBlob, fileName);+ }+};++/**+ * Save the given {@link blob} as a file in the user's download folder.+ */+const saveBlobPartAsFile = async (blobPart: BlobPart, fileName: string) =>+ createTypedObjectURL(blobPart, fileName).then((url) =>+ saveAsFileAndRevokeObjectURL(url, fileName),+ );++const createTypedObjectURL = async (blobPart: BlobPart, fileName: string) => {+ const blob = blobPart instanceof Blob ? blobPart : new Blob([blobPart]);+ const { mimeType } = await detectFileTypeInfo(new File([blob], fileName));+ return URL.createObjectURL(new Blob([blob], { type: mimeType }));+};++async function downloadFilesDesktop(+ electron: Electron,+ files: EnteFile[],+ progressBarUpdater: {+ increaseSuccess: () => void;+ increaseFailed: () => void;+ isCancelled: () => boolean;+ },+ downloadPath: string,+) {+ for (const file of files) {+ try {+ if (progressBarUpdater?.isCancelled()) {+ return;+ }+ await downloadFileDesktop(electron, file, downloadPath);+ progressBarUpdater?.increaseSuccess();+ } catch (e) {+ log.error("download fail for file", e);+ progressBarUpdater?.increaseFailed();+ }+ }+}++async function downloadFileDesktop(+ electron: Electron,+ file: EnteFile,+ downloadDir: string,+) {+ const fs = electron.fs;++ const stream = await downloadManager.fileStream(file);+ const fileName = fileFileName(file);++ if (file.metadata.fileType == FileType.livePhoto) {+ const fileBlob = await new Response(stream).blob();+ const { imageFileName, imageData, videoFileName, videoData } =+ await decodeLivePhoto(fileName, fileBlob);+ const imageExportName = await safeFileName(+ downloadDir,+ imageFileName,+ fs.exists,+ );+ const imageStream = new Response(imageData).body;+ await writeStream(+ electron,+ joinPath(downloadDir, imageExportName),+ imageStream,+ );+ try {+ const videoExportName = await safeFileName(+ downloadDir,+ videoFileName,+ fs.exists,+ );+ const videoStream = new Response(videoData).body;+ await writeStream(+ electron,+ joinPath(downloadDir, videoExportName),+ videoStream,+ );+ } catch (e) {+ await fs.rm(joinPath(downloadDir, imageExportName));+ throw e;+ }+ } else {+ const fileExportName = await safeFileName(+ downloadDir,+ fileName,+ fs.exists,+ );+ await writeStream(+ electron,+ joinPath(downloadDir, fileExportName),+ stream,+ );+ }+}++export async function downloadCollectionHelper(+ collectionID: number,+ setFilesDownloadProgressAttributes: SetFilesDownloadProgressAttributes,+) {+ try {+ const allFiles = await savedCollectionFiles();+ const collectionFiles = allFiles.filter(+ (file) => file.collectionID == collectionID,+ );+ const allCollections = await savedCollections();+ const collection = allCollections.find(+ (collection) => collection.id == collectionID,+ );+ if (!collection) {+ throw Error("collection not found");+ }+ await downloadCollectionFiles(+ collection.name,+ collectionFiles,+ setFilesDownloadProgressAttributes,+ );+ } catch (e) {+ log.error("download collection failed ", e);+ }+}++export async function downloadDefaultHiddenCollectionHelper(+ setFilesDownloadProgressAttributesCreator: SetFilesDownloadProgressAttributesCreator,+) {+ try {+ const defaultHiddenCollectionsIDs = findDefaultHiddenCollectionIDs(+ await savedCollections(),+ );+ const collectionFiles = await savedCollectionFiles();+ const defaultHiddenCollectionFiles = uniqueFilesByID(+ collectionFiles.filter((file) =>+ defaultHiddenCollectionsIDs.has(file.collectionID),+ ),+ );+ const setFilesDownloadProgressAttributes =+ setFilesDownloadProgressAttributesCreator(+ defaultHiddenCollectionUserFacingName,+ PseudoCollectionID.hiddenItems,+ true,+ );++ await downloadCollectionFiles(+ defaultHiddenCollectionUserFacingName,+ defaultHiddenCollectionFiles,+ setFilesDownloadProgressAttributes,+ );+ } catch (e) {+ log.error("download hidden files failed ", e);+ }+}++export async function downloadCollectionFiles(+ collectionName: string,+ collectionFiles: EnteFile[],+ setFilesDownloadProgressAttributes: SetFilesDownloadProgressAttributes,+) {+ if (!collectionFiles.length) {+ return;+ }+ let downloadDirPath: string;+ const electron = globalThis.electron;+ if (electron) {+ const selectedDir = await electron.selectDirectory();+ if (!selectedDir) {+ return;+ }+ downloadDirPath = await createCollectionDownloadFolder(+ selectedDir,+ collectionName,+ );+ }+ await downloadFilesWithProgress(+ collectionFiles,+ downloadDirPath,+ setFilesDownloadProgressAttributes,+ );+}++async function createCollectionDownloadFolder(+ downloadDirPath: string,+ collectionName: string,+) {+ const fs = ensureElectron().fs;+ const collectionDownloadName = await safeDirectoryName(+ downloadDirPath,+ collectionName,+ fs.exists,+ );+ const collectionDownloadPath = joinPath(+ downloadDirPath,+ collectionDownloadName,+ );+ await fs.mkdirIfNeeded(collectionDownloadPath);+ return collectionDownloadPath;+}