Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move

Manav Rathid52e3894d8c7
diff --git a/web/apps/photos/src/components/Upload.tsx b/web/apps/photos/src/components/Upload.tsx
index 7738324f4f..6eacfd8266 100644
--- a/web/apps/photos/src/components/Upload.tsx
+++ b/web/apps/photos/src/components/Upload.tsx
@@ -28,10 +28,12 @@ import { basename, dirname, joinPath } from "ente-base/file-name";
import log from "ente-base/log";
import type { CollectionMapping, Electron, ZipItem } from "ente-base/types/ipc";
import { useFileInput } from "ente-gallery/components/utils/use-file-input";
-import type {
- FileAndPath,
- UploadItem,
- UploadPhase,
+import {
+ groupItemsBasedOnParentFolder,
+ type FileAndPath,
+ type UploadItem,
+ type UploadItemAndPath,
+ type UploadPhase,
} from "ente-gallery/services/upload";
import type { Collection } from "ente-media/collection";
import type { EnteFile } from "ente-media/file";
@@ -40,7 +42,6 @@ import { CollectionMappingChoice } from "ente-new/photos/components/CollectionMa
import type { CollectionSelectorAttributes } from "ente-new/photos/components/CollectionSelector";
import { downloadAppDialogAttributes } from "ente-new/photos/components/utils/download";
import { getLatestCollections } from "ente-new/photos/services/collections";
-import { exportMetadataDirectoryName } from "ente-new/photos/services/export";
import { redirectToCustomerPortal } from "ente-new/photos/services/user-details";
import { usePhotosAppContext } from "ente-new/photos/types/context";
import { CustomError } from "ente-shared/error";
@@ -118,8 +119,6 @@ interface UploadProps {
type UploadType = "files" | "folders" | "zips";
-type UploadItemAndPath = [UploadItem, string];
-
/**
* Top level component that houses the infrastructure for handling uploads.
*/
@@ -561,8 +560,9 @@ export const Upload: React.FC<UploadProps> = ({
) => {
await preCollectionCreationAction();
const uploadItemsWithCollection = uploadItemsAndPaths.current.map(
- ([uploadItem], index) => ({
+ ([uploadItem, path], index) => ({
uploadItem,
+ uploadItemPathPrefix: dirname(path),
localID: index,
collectionID: collection.id,
}),
@@ -582,14 +582,17 @@ export const Upload: React.FC<UploadProps> = ({
await preCollectionCreationAction();
let uploadItemsWithCollection: UploadItemWithCollection[] = [];
const collections: Collection[] = [];
- let collectionNameToUploadItems = new Map<string, UploadItem[]>();
+ let collectionNameToUploadItems = new Map<
+ string,
+ UploadItemAndPath[]
+ >();
if (mapping == "root") {
collectionNameToUploadItems.set(
collectionName,
- uploadItemsAndPaths.current.map(([i]) => i),
+ uploadItemsAndPaths.current,
);
} else {
- collectionNameToUploadItems = groupFilesBasedOnParentFolder(
+ collectionNameToUploadItems = groupItemsBasedOnParentFolder(
uploadItemsAndPaths.current,
collectionName,
);
@@ -609,8 +612,9 @@ export const Upload: React.FC<UploadProps> = ({
props.setCollections([...existingCollections, ...collections]);
uploadItemsWithCollection = [
...uploadItemsWithCollection,
- ...uploadItems.map((uploadItem) => ({
+ ...uploadItems.map(([uploadItem, path]) => ({
localID: index++,
+ uploadItemPathPrefix: dirname(path),
collectionID: collection.id,
uploadItem,
})),
@@ -989,61 +993,6 @@ const deriveImportSuggestion = (
};
};
-/**
- * Group files that are that have the same parent folder into collections.
- *
- * For Example, if the user selects files have a directory structure like:
- *
- * a
- * / | \
- * b j c
- * /|\ / \
- * e f g h i
- *
- * The files will grouped into 3 collections:
- *
- * [
- * a => [j],
- * b => [e,f,g],
- * c => [h, i]
- * ]
- *
- * @param defaultFolderName Optional collection name to use for any rooted files
- * that do not have a parent folder. The function will throw if a default is not
- * provided and we encounter any such files without a parent.
- */
-const groupFilesBasedOnParentFolder = (
- uploadItemAndPaths: UploadItemAndPath[],
- defaultFolderName: string | undefined,
-) => {
- const result = new Map<string, UploadItem[]>();
- for (const [uploadItem, pathOrName] of uploadItemAndPaths) {
- let folderPath = pathOrName.substring(0, pathOrName.lastIndexOf("/"));
- // If the parent folder of a file is "metadata", then we consider it to
- // be part of the parent folder.
- //
- // e.g. for FileList
- //
- // [a/x.png, a/metadata/x.png.json]
- //
- // they will both be grouped into the collection "a". This is so that we
- // cluster the metadata json files in the same collection as the file it
- // is for.
- if (folderPath.endsWith(exportMetadataDirectoryName)) {
- folderPath = folderPath.substring(0, folderPath.lastIndexOf("/"));
- }
- let folderName = folderPath.substring(folderPath.lastIndexOf("/") + 1);
- if (!folderName) {
- if (!defaultFolderName)
- throw Error(`Leaf file (without default): ${folderPath}`);
- folderName = defaultFolderName;
- }
- if (!result.has(folderName)) result.set(folderName, []);
- result.get(folderName).push(uploadItem);
- }
- return result;
-};
-
const setPendingUploads = async (
electron: Electron,
collections: Collection[],
diff --git a/web/packages/gallery/export-dirs.ts b/web/packages/gallery/export-dirs.ts
new file mode 100644
index 0000000000..40229741ed
--- /dev/null
+++ b/web/packages/gallery/export-dirs.ts
@@ -0,0 +1,11 @@
+/**
+ * Name of the directory in which we put our metadata when exporting to the file
+ * system.
+ */
+export const exportMetadataDirectoryName = "metadata";
+
+/**
+ * Name of the directory in which we keep trash items when deleting files that
+ * have been exported to the local disk previously.
+ */
+export const exportTrashDirectoryName = "Trash";
diff --git a/web/packages/gallery/services/upload/index.ts b/web/packages/gallery/services/upload/index.ts
index 6ab21773ae..c705595f49 100644
--- a/web/packages/gallery/services/upload/index.ts
+++ b/web/packages/gallery/services/upload/index.ts
@@ -1,6 +1,7 @@
import log from "ente-base/log";
import { customAPIOrigin } from "ente-base/origins";
import type { ZipItem } from "ente-base/types/ipc";
+import { exportMetadataDirectoryName } from "ente-gallery/export-dirs";
import type { Collection } from "ente-media/collection";
import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod";
@@ -164,9 +165,72 @@ export interface TimestampedFileSystemUploadItem {
*/
export type UploadPathPrefix = string;
+export type UploadItemAndPath = [UploadItem, string];
+
+/**
+ * Group files that are that have the same parent folder into collections.
+ *
+ * This is used to segregate the list of {@link UploadItemAndPath}s that we
+ * obtain when an upload is initiated into per-collection groups when the user
+ * chooses the "parent" {@link CollectionMapping} option.
+ *
+ * For example, if the user selects files have a directory structure like:
+ *
+ * a
+ * / | \
+ * b j c
+ * /|\ / \
+ * e f g h i
+ *
+ * The files will grouped into 3 collections:
+ *
+ * [
+ * a => [j],
+ * b => [e, f, g],
+ * c => [h, i]
+ * ]
+ *
+ * @param defaultFolderName Optional collection name to use for any rooted files
+ * that do not have a parent folder. The function will throw if a default is not
+ * provided and we encounter any such files without a parent.
+ */
+export const groupItemsBasedOnParentFolder = (
+ uploadItemAndPaths: UploadItemAndPath[],
+ defaultFolderName: string | undefined,
+) => {
+ const result = new Map<string, UploadItemAndPath[]>();
+ for (const [uploadItem, pathOrName] of uploadItemAndPaths) {
+ let folderPath = pathOrName.substring(0, pathOrName.lastIndexOf("/"));
+ // If the parent folder of a file is "metadata", then we consider it to
+ // be part of the parent folder.
+ //
+ // e.g. for FileList
+ //
+ // [a/x.png, a/metadata/x.png.json]
+ //
+ // they will both be grouped into the collection "a". This is so that we
+ // cluster the metadata json files in the same collection as the file it
+ // is for.
+ if (folderPath.endsWith(exportMetadataDirectoryName)) {
+ folderPath = folderPath.substring(0, folderPath.lastIndexOf("/"));
+ }
+ let folderName = folderPath.substring(folderPath.lastIndexOf("/") + 1);
+ if (!folderName) {
+ if (!defaultFolderName)
+ throw Error(`Leaf file (without default): ${folderPath}`);
+ folderName = defaultFolderName;
+ }
+ if (!result.has(folderName)) result.set(folderName, []);
+ result.get(folderName)!.push([uploadItem, pathOrName]);
+ }
+ return result;
+};
+
export interface LivePhotoAssets {
image: UploadItem;
+ imagePathPrefix?: UploadPathPrefix;
video: UploadItem;
+ videoPathPrefix?: UploadPathPrefix;
}
/**
diff --git a/web/packages/gallery/services/upload/upload-service.ts b/web/packages/gallery/services/upload/upload-service.ts
index 96adc7c48b..b05854f530 100644
--- a/web/packages/gallery/services/upload/upload-service.ts
+++ b/web/packages/gallery/services/upload/upload-service.ts
@@ -53,7 +53,7 @@ import {
} from "ente-shared/error";
import { mergeUint8Arrays } from "ente-utils/array";
import { ensureInteger, ensureNumber } from "ente-utils/ensure";
-import type { UploadableUploadItem, UploadItem } from ".";
+import type { UploadableUploadItem, UploadItem, UploadPathPrefix } from ".";
import { type LivePhotoAssets, type UploadResult } from ".";
import { tryParseEpochMicrosecondsFromFileName } from "./date";
import {
@@ -260,6 +260,10 @@ export interface UploadAsset {
livePhotoAssets?: LivePhotoAssets;
/* Valid for non-live photos */
uploadItem?: UploadItem;
+ /**
+ * The path prefix of the uploadItem, if available.
+ */
+ uploadItemPathPrefix?: UploadPathPrefix;
/**
* Metadata we know about a file externally. Valid for non-live photos.
*
diff --git a/web/packages/new/photos/services/export.ts b/web/packages/new/photos/services/export.ts
index e05caf5663..7a16b749e9 100644
--- a/web/packages/new/photos/services/export.ts
+++ b/web/packages/new/photos/services/export.ts
@@ -30,18 +30,10 @@ import { getData, setData } from "ente-shared/storage/localStorage";
import { PromiseQueue } from "ente-utils/promise";
import i18n from "i18next";
import { migrateExport, type ExportRecord } from "./export-migration";
-
-/**
- * Name of the directory in which we put our metadata when exporting to the file
- * system.
- */
-export const exportMetadataDirectoryName = "metadata";
-
-/**
- * Name of the directory in which we keep trash items when deleting files that
- * have been exported to the local disk previously.
- */
-export const exportTrashDirectoryName = "Trash";
+import {
+ exportMetadataDirectoryName,
+ exportTrashDirectoryName,
+} from "ente-gallery/export-dirs";
/** Name of the JSON file in which we keep the state of the export. */
const exportRecordFileName = "export_status.json";
diff --git a/web/packages/new/photos/utils/native-fs.ts b/web/packages/new/photos/utils/native-fs.ts
index 1c0efe7b8b..fd8090d87c 100644
--- a/web/packages/new/photos/utils/native-fs.ts
+++ b/web/packages/new/photos/utils/native-fs.ts
@@ -9,7 +9,7 @@ import { joinPath, nameAndExtension } from "ente-base/file-name";
import {
exportMetadataDirectoryName,
exportTrashDirectoryName,
-} from "ente-new/photos/services/export";
+} from "ente-gallery/export-dirs";
import sanitize from "sanitize-filename";
/**