Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Get to FileWithCollection

Manav Rathib66d74c65242
diff --git a/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx b/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
index c818504622..ff795aca78 100644
--- a/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
+++ b/web/apps/photos/src/components/PhotoViewer/ImageEditorOverlay/index.tsx
@@ -46,7 +46,6 @@ import { detectFileTypeInfo } from "services/detect-type";
import downloadManager from "services/download";
import uploadManager from "services/upload/uploadManager";
import { EnteFile } from "types/file";
-import { FileWithCollection } from "types/upload";
import { getEditorCloseConfirmationMessage } from "utils/ui";
import ColoursMenu from "./ColoursMenu";
import CropMenu, { cropRegionOfCanvas, getCropRegionArgs } from "./CropMenu";
@@ -507,10 +506,10 @@ const ImageEditorOverlay = (props: IProps) => {
);
const editedFile = await getEditedFile();
- const file: FileWithCollection = {
- file: editedFile,
- collectionID: props.file.collectionID,
+ const file = {
+ fileOrPath: editedFile,
localID: 1,
+ collectionID: props.file.collectionID,
};
uploadManager.prepareForNewUpload();
diff --git a/web/apps/photos/src/components/Upload/Uploader.tsx b/web/apps/photos/src/components/Upload/Uploader.tsx
index 412af7ca55..228f95f782 100644
--- a/web/apps/photos/src/components/Upload/Uploader.tsx
+++ b/web/apps/photos/src/components/Upload/Uploader.tsx
@@ -33,7 +33,7 @@ import {
SetLoading,
UploadTypeSelectorIntent,
} from "types/gallery";
-import { FileWithCollection, type FileWithCollection2 } from "types/upload";
+import { type FileWithCollection } from "types/upload";
import {
InProgressUpload,
SegregatedFinishedUploads,
@@ -48,9 +48,9 @@ import {
} from "utils/ui";
import {
DEFAULT_IMPORT_SUGGESTION,
- filterOutSystemFiles,
getImportSuggestion,
groupFilesBasedOnParentFolder,
+ pruneHiddenFiles,
type ImportSuggestion,
} from "utils/upload";
import { SetCollectionNamerAttributes } from "../Collections/CollectionNamer";
@@ -122,11 +122,10 @@ export default function Uploader(props: Props) {
string[] | undefined
>();
const [electronFiles, setElectronFiles] = useState<ElectronFile[]>(null);
- const [webFiles, setWebFiles] = useState([]);
+ const [webFiles, setWebFiles] = useState<File[]>([]);
+
+ const fileOrPathsToUpload = useRef<(File | string)[]>([]);
- const toUploadFiles = useRef<
- File[] | ElectronFile[] | string[] | undefined | null
- >(null);
/**
* If true, then the next upload we'll be processing was initiated by our
* desktop app.
@@ -340,30 +339,34 @@ export default function Uploader(props: Props) {
props.setLoading(true);
if (webFiles?.length > 0) {
// File selection by drag and drop or selection of file.
- toUploadFiles.current = webFiles;
+ fileOrPathsToUpload.current = webFiles;
setWebFiles([]);
} else if (appContext.sharedFiles?.length > 0) {
- toUploadFiles.current = appContext.sharedFiles;
+ fileOrPathsToUpload.current = appContext.sharedFiles;
appContext.resetSharedFiles();
} else if (electronFiles?.length > 0) {
// File selection from desktop app - deprecated
- toUploadFiles.current = electronFiles;
+ log.warn("Using deprecated code path for ElectronFiles");
+ fileOrPathsToUpload.current = electronFiles.map((f) => f.path);
setElectronFiles([]);
} else if (desktopFilePaths && desktopFilePaths.length > 0) {
// File selection from our desktop app
- toUploadFiles.current = desktopFilePaths;
+ fileOrPathsToUpload.current = desktopFilePaths;
setDesktopFilePaths(undefined);
}
- toUploadFiles.current = filterOutSystemFiles(toUploadFiles.current);
- if (toUploadFiles.current.length === 0) {
+ fileOrPathsToUpload.current = pruneHiddenFiles(
+ fileOrPathsToUpload.current,
+ );
+ if (fileOrPathsToUpload.current.length === 0) {
props.setLoading(false);
return;
}
const importSuggestion = getImportSuggestion(
pickedUploadType.current,
- toUploadFiles.current.map((file) =>
+ fileOrPathsToUpload.current.map((file) =>
+ /** TODO(MR): Is path valid for Web files? */
typeof file == "string" ? file : file["path"],
),
);
@@ -409,12 +412,13 @@ export default function Uploader(props: Props) {
`upload file to an existing collection name:${collection.name}, collectionID:${collection.id}`,
);
await preCollectionCreationAction();
- const filesWithCollectionToUpload: FileWithCollection[] =
- toUploadFiles.current.map((file, index) => ({
- file,
+ const filesWithCollectionToUpload = fileOrPathsToUpload.current.map(
+ (fileOrPath, index) => ({
+ fileOrPath,
localID: index,
collectionID: collection.id,
- }));
+ }),
+ );
await waitInQueueAndUploadFiles(
filesWithCollectionToUpload,
[collection],
@@ -434,7 +438,7 @@ export default function Uploader(props: Props) {
`upload file to an new collections strategy:${strategy} ,collectionName:${collectionName}`,
);
await preCollectionCreationAction();
- let filesWithCollectionToUpload: FileWithCollection2[] = [];
+ let filesWithCollectionToUpload: FileWithCollection[] = [];
const collections: Collection[] = [];
let collectionNameToFilesMap = new Map<
string,
@@ -443,11 +447,11 @@ export default function Uploader(props: Props) {
if (strategy == "root") {
collectionNameToFilesMap.set(
collectionName,
- toUploadFiles.current,
+ fileOrPathsToUpload.current,
);
} else {
collectionNameToFilesMap = groupFilesBasedOnParentFolder(
- toUploadFiles.current,
+ fileOrPathsToUpload.current,
);
}
log.info(
@@ -494,7 +498,7 @@ export default function Uploader(props: Props) {
filesWithCollectionToUpload as FileWithCollection[],
collections,
);
- toUploadFiles.current = null;
+ fileOrPathsToUpload.current = null;
} catch (e) {
log.error("Failed to upload files to new collections", e);
}
@@ -553,7 +557,11 @@ export default function Uploader(props: Props) {
await electron.setPendingUploadFiles(
"files",
filesWithCollectionToUploadIn.map(
- ({ file }) => (file as ElectronFile).path,
+ // TODO(MR): ElectronFile
+ ({ fileOrPath }) =>
+ typeof fileOrPath == "string"
+ ? fileOrPath
+ : (fileOrPath as any as ElectronFile).path,
),
);
}
@@ -566,8 +574,7 @@ export default function Uploader(props: Props) {
if (isElectron()) {
if (watcher.isUploadRunning()) {
await watcher.allFileUploadsDone(
- /* TODO(MR): ElectronFile changes */
- filesWithCollectionToUploadIn as FileWithCollection2[],
+ filesWithCollectionToUploadIn,
collections,
);
} else if (watcher.isSyncPaused()) {
@@ -592,7 +599,6 @@ export default function Uploader(props: Props) {
const uploaderName = uploadManager.getUploaderName();
await preUploadAction();
await uploadManager.uploadFiles(
- /* TODO(MR): ElectronFile changes */
filesWithCollections.files as FileWithCollection[],
filesWithCollections.collections,
uploaderName,
@@ -857,7 +863,7 @@ export default function Uploader(props: Props) {
open={userNameInputDialogView}
onClose={handleUserNameInputDialogClose}
onNameSubmit={handlePublicUpload}
- toUploadFilesCount={toUploadFiles.current?.length}
+ toUploadFilesCount={fileOrPathsToUpload.current?.length}
uploaderName={uploaderNameRef.current}
/>
</>
diff --git a/web/apps/photos/src/services/upload/uploadManager.ts b/web/apps/photos/src/services/upload/uploadManager.ts
index fa61ab68d7..64535517f2 100644
--- a/web/apps/photos/src/services/upload/uploadManager.ts
+++ b/web/apps/photos/src/services/upload/uploadManager.ts
@@ -31,7 +31,7 @@ import { SetFiles } from "types/gallery";
import {
FileWithCollection,
PublicUploadProps,
- type LivePhotoAssets2,
+ type LivePhotoAssets,
} from "types/upload";
import {
FinishedUploads,
@@ -690,14 +690,14 @@ type FileWithCollectionIDAndName = {
/** Alias */
file?: File | string;
/* Valid for live photos */
- livePhotoAssets?: LivePhotoAssets2;
+ livePhotoAssets?: LivePhotoAssets;
};
const makeFileWithCollectionIDAndName = (
f: FileWithCollection,
): FileWithCollectionIDAndName => {
/* TODO(MR): ElectronFile */
- const fileOrPath = (f.fileOrPath ?? f.file) as File | string;
+ const fileOrPath = f.fileOrPath;
if (!(fileOrPath instanceof File || typeof fileOrPath == "string"))
throw new Error(`Unexpected file ${f}`);
@@ -714,7 +714,7 @@ const makeFileWithCollectionIDAndName = (
file: fileOrPath,
fileOrPath: fileOrPath,
/* TODO(MR): ElectronFile */
- livePhotoAssets: f.livePhotoAssets as LivePhotoAssets2,
+ livePhotoAssets: f.livePhotoAssets as LivePhotoAssets,
};
};
@@ -729,7 +729,7 @@ type ClusteredFile = {
fileName: string;
isLivePhoto: boolean;
fileOrPath?: File | string;
- livePhotoAssets?: LivePhotoAssets2;
+ livePhotoAssets?: LivePhotoAssets;
};
/**
diff --git a/web/apps/photos/src/services/upload/uploadService.ts b/web/apps/photos/src/services/upload/uploadService.ts
index dfbd3a8473..e44a3c0740 100644
--- a/web/apps/photos/src/services/upload/uploadService.ts
+++ b/web/apps/photos/src/services/upload/uploadService.ts
@@ -38,7 +38,7 @@ import { EncryptedMagicMetadata } from "types/magicMetadata";
import {
ParsedExtractedMetadata,
PublicUploadProps,
- type LivePhotoAssets2,
+ type LivePhotoAssets,
} from "types/upload";
import {
getNonEmptyMagicMetadataProps,
@@ -168,7 +168,7 @@ export const fopSize = async (fileOrPath: File | string): Promise<number> =>
interface UploadAsset2 {
isLivePhoto?: boolean;
fileOrPath?: File | string;
- livePhotoAssets?: LivePhotoAssets2;
+ livePhotoAssets?: LivePhotoAssets;
}
interface FileInMemory {
@@ -522,7 +522,7 @@ const readAssetDetails = async ({
? readLivePhotoDetails(livePhotoAssets)
: readImageOrVideoDetails(fileOrPath);
-const readLivePhotoDetails = async ({ image, video }: LivePhotoAssets2) => {
+const readLivePhotoDetails = async ({ image, video }: LivePhotoAssets) => {
const img = await readImageOrVideoDetails(image);
const vid = await readImageOrVideoDetails(video);
@@ -613,7 +613,7 @@ const extractAssetMetadata = async (
);
const extractLivePhotoMetadata = async (
- livePhotoAssets: LivePhotoAssets2,
+ livePhotoAssets: LivePhotoAssets,
fileTypeInfo: FileTypeInfo,
lastModifiedMs: number,
collectionID: number,
@@ -835,7 +835,7 @@ const readAsset = async (
: await readImageOrVideo(fileOrPath, fileTypeInfo);
const readLivePhoto = async (
- livePhotoAssets: LivePhotoAssets2,
+ livePhotoAssets: LivePhotoAssets,
fileTypeInfo: FileTypeInfo,
) => {
const readImage = await readFileOrPath(livePhotoAssets.image);
diff --git a/web/apps/photos/src/services/watch.ts b/web/apps/photos/src/services/watch.ts
index f036676b6a..be357019b4 100644
--- a/web/apps/photos/src/services/watch.ts
+++ b/web/apps/photos/src/services/watch.ts
@@ -17,7 +17,7 @@ import debounce from "debounce";
import uploadManager from "services/upload/uploadManager";
import { Collection } from "types/collection";
import { EncryptedEnteFile } from "types/file";
-import { type FileWithCollection2 } from "types/upload";
+import { type FileWithCollection } from "types/upload";
import { groupFilesBasedOnCollectionID } from "utils/file";
import { isHiddenFile } from "utils/upload";
import { removeFromCollection } from "./collectionService";
@@ -322,7 +322,7 @@ class FolderWatcher {
*/
async onFileUpload(
fileUploadResult: UPLOAD_RESULT,
- fileWithCollection: FileWithCollection2,
+ fileWithCollection: FileWithCollection,
file: EncryptedEnteFile,
) {
// The files we get here will have fileWithCollection.file as a string,
@@ -375,7 +375,7 @@ class FolderWatcher {
* {@link upload} get uploaded.
*/
async allFileUploadsDone(
- filesWithCollection: FileWithCollection2[],
+ filesWithCollection: FileWithCollection[],
collections: Collection[],
) {
const electron = ensureElectron();
@@ -411,7 +411,7 @@ class FolderWatcher {
this.debouncedRunNextEvent();
}
- private deduceSyncedAndIgnored(filesWithCollection: FileWithCollection2[]) {
+ private deduceSyncedAndIgnored(filesWithCollection: UploadedFile[]) {
const syncedFiles: FolderWatch["syncedFiles"] = [];
const ignoredFiles: FolderWatch["ignoredFiles"] = [];
@@ -453,7 +453,7 @@ class FolderWatcher {
markIgnored(videoPath);
}
} else {
- const path = ensureString(fileWithCollection.file);
+ const path = ensureString(fileWithCollection.fileOrPath);
const file = this.uploadedFileForPath.get(path);
if (file) {
markSynced(file, path);
diff --git a/web/apps/photos/src/types/upload/index.ts b/web/apps/photos/src/types/upload/index.ts
index b83db63684..817a9dc102 100644
--- a/web/apps/photos/src/types/upload/index.ts
+++ b/web/apps/photos/src/types/upload/index.ts
@@ -1,40 +1,23 @@
import type { ElectronFile } from "@/next/types/file";
-import { Collection } from "types/collection";
export interface Location {
latitude: number;
longitude: number;
}
-export interface UploadAsset {
+export interface FileWithCollection {
+ localID: number;
+ collectionID: number;
isLivePhoto?: boolean;
- file?: File | ElectronFile;
- fileOrPath?: File | ElectronFile;
+ fileOrPath?: File | string;
livePhotoAssets?: LivePhotoAssets;
}
export interface LivePhotoAssets {
- image: globalThis.File | ElectronFile;
- video: globalThis.File | ElectronFile;
-}
-
-export interface FileWithCollection extends UploadAsset {
- localID: number;
- collection?: Collection;
- collectionID?: number;
-}
-
-export interface LivePhotoAssets2 {
image: File | string;
video: File | string;
}
-export interface FileWithCollection2 extends UploadAsset {
- localID: number;
- collection?: Collection;
- collectionID: number;
-}
-
export interface ParsedExtractedMetadata {
location: Location;
creationTime: number;
diff --git a/web/apps/photos/src/utils/upload/index.ts b/web/apps/photos/src/utils/upload/index.ts
index 8923e68941..5378b2a92c 100644
--- a/web/apps/photos/src/utils/upload/index.ts
+++ b/web/apps/photos/src/utils/upload/index.ts
@@ -4,6 +4,7 @@ import { ElectronFile } from "@/next/types/file";
import { PICKED_UPLOAD_TYPE } from "constants/upload";
import isElectron from "is-electron";
import { exportMetadataDirectoryName } from "services/export";
+import { fopFileName } from "services/upload/uploadService";
export const hasFileHash = (file: Metadata) =>
file.hash || (file.imageHash && file.videoHash);
@@ -116,30 +117,14 @@ export function groupFilesBasedOnParentFolder(
return collectionNameToFilesMap;
}
-export function filterOutSystemFiles(
- files: File[] | ElectronFile[] | string[] | undefined | null,
-) {
- if (!files) return files;
-
- if (files[0] instanceof File) {
- const browserFiles = files as File[];
- return browserFiles.filter((file) => {
- return !isSystemFile(file);
- });
- } else if (typeof files[0] == "string") {
- const filePaths = files as string[];
- return filePaths.filter((path) => !isHiddenFile(path));
- } else {
- const electronFiles = files as ElectronFile[];
- return electronFiles.filter((file) => {
- return !isSystemFile(file);
- });
- }
-}
+/**
+ * Filter out hidden files from amongst {@link fileOrPaths}.
+ *
+ * Hidden files are those whose names begin with a "." (dot).
+ */
-export function isSystemFile(file: File | ElectronFile) {
- return file.name.startsWith(".");
-}
+export const pruneHiddenFiles = (fileOrPaths: (File | string)[]) =>
+ fileOrPaths.filter((f) => !fopFileName(f).startsWith("."));
/**
* Return true if the file at the given {@link path} is hidden.