Skip to content

armgit / ente

Public Imported from GitHub
0

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

Impl

Manav Rathi6bd1547e091a
diff --git a/web/apps/photos/src/components/Upload.tsx b/web/apps/photos/src/components/Upload.tsx
index ffa379411c..4e4657558f 100644
--- a/web/apps/photos/src/components/Upload.tsx
+++ b/web/apps/photos/src/components/Upload.tsx
@@ -563,7 +563,7 @@ export const Upload: React.FC<UploadProps> = ({
const uploadItemsWithCollection = uploadItemsAndPaths.current.map(
([uploadItem, path], index) => ({
uploadItem,
- uploadItemPathPrefix: uploadPathPrefix(path),
+ pathPrefix: uploadPathPrefix(path),
localID: index,
collectionID: collection.id,
}),
@@ -615,7 +615,7 @@ export const Upload: React.FC<UploadProps> = ({
...uploadItemsWithCollection,
...uploadItems.map(([uploadItem, path]) => ({
localID: index++,
- uploadItemPathPrefix: uploadPathPrefix(path),
+ pathPrefix: uploadPathPrefix(path),
collectionID: collection.id,
uploadItem,
})),
diff --git a/web/apps/photos/src/services/upload-manager.ts b/web/apps/photos/src/services/upload-manager.ts
index b1a0a5a9cb..e093dabcf3 100644
--- a/web/apps/photos/src/services/upload-manager.ts
+++ b/web/apps/photos/src/services/upload-manager.ts
@@ -17,7 +17,7 @@ import {
metadataJSONMapKeyForJSON,
tryParseTakeoutMetadataJSON,
type ParsedMetadataJSON,
-} from "ente-gallery/services/upload/takeout";
+} from "ente-gallery/services/upload/metadata-json";
import UploadService, {
areLivePhotoAssets,
upload,
@@ -447,6 +447,7 @@ class UploadManager {
const item = {
uploadItem: file,
+ pathPrefix: undefined,
localID: 1,
collectionID: collection.id,
externalParsedMetadata: { creationDate, creationTime },
@@ -481,16 +482,19 @@ class UploadManager {
) {
this.uiService.reset(items.length);
- for (const { uploadItem, fileName, collectionID } of items) {
+ for (const item of items) {
this.abortIfCancelled();
+ const { uploadItem, pathPrefix, fileName, collectionID } = item;
log.info(`Parsing metadata JSON ${fileName}`);
const metadataJSON = await tryParseTakeoutMetadataJSON(uploadItem!);
if (metadataJSON) {
- this.parsedMetadataJSONMap.set(
- metadataJSONMapKeyForJSON(collectionID, fileName),
- metadataJSON,
+ const key = metadataJSONMapKeyForJSON(
+ pathPrefix,
+ collectionID,
+ fileName,
);
+ this.parsedMetadataJSONMap.set(key, metadataJSON);
this.uiService.increaseFileUploaded();
}
}
@@ -725,6 +729,7 @@ const makeUploadItemWithCollectionIDAndName = (
: uploadItemFileName(f.uploadItem))!,
isLivePhoto: f.isLivePhoto,
uploadItem: f.uploadItem,
+ pathPrefix: f.pathPrefix,
livePhotoAssets: f.livePhotoAssets,
externalParsedMetadata: f.externalParsedMetadata,
});
@@ -771,12 +776,14 @@ const clusterLivePhotos = async (
fileType: fFileType,
collectionID: f.collectionID,
uploadItem: f.uploadItem,
+ pathPrefix: f.pathPrefix,
};
const ga: PotentialLivePhotoAsset = {
fileName: g.fileName,
fileType: gFileType,
collectionID: g.collectionID,
uploadItem: g.uploadItem,
+ pathPrefix: g.pathPrefix,
};
if (await areLivePhotoAssets(fa, ga, parsedMetadataJSONMap)) {
const [image, video] =
@@ -786,6 +793,7 @@ const clusterLivePhotos = async (
collectionID: f.collectionID,
fileName: image.fileName,
isLivePhoto: true,
+ pathPrefix: image.pathPrefix,
livePhotoAssets: {
image: image.uploadItem,
video: video.uploadItem,
diff --git a/web/apps/photos/tests/upload.test.ts b/web/apps/photos/tests/upload.test.ts
index 7779ef81f8..ceb31acbc7 100644
--- a/web/apps/photos/tests/upload.test.ts
+++ b/web/apps/photos/tests/upload.test.ts
@@ -4,9 +4,9 @@ import {
tryParseEpochMicrosecondsFromFileName,
} from "ente-gallery/services/upload/date";
import {
- matchTakeoutMetadata,
+ matchJSONMetadata,
metadataJSONMapKeyForJSON,
-} from "ente-gallery/services/upload/takeout";
+} from "ente-gallery/services/upload/metadata-json";
import { FileType } from "ente-media/file-type";
import { getLocalCollections } from "ente-new/photos/services/collections";
import {
@@ -447,14 +447,14 @@ function parseDateTimeFromFileNameTest() {
const fileNameToJSONMappingTests = () => {
for (const { filename, jsonFilename } of fileNameToJSONMappingCases) {
- const jsonKey = metadataJSONMapKeyForJSON(0, jsonFilename);
+ const jsonKey = metadataJSONMapKeyForJSON(undefined, 0, jsonFilename);
// See the docs for the file name matcher as to why it doesn't return
// the key but instead indexes into the map for us. To test it, we
// construct a placeholder map with a dummy entry for the expected key.
const map = new Map([[jsonKey, {}]]);
- if (!matchTakeoutMetadata(filename, 0, map)) {
+ if (!matchJSONMetadata(undefined, 0, filename, map)) {
throw Error(
`fileNameToJSONMappingTests failed ❌ for ${filename} and ${jsonFilename}`,
);
diff --git a/web/packages/gallery/services/upload/index.ts b/web/packages/gallery/services/upload/index.ts
index 1df06e4258..4948d2c609 100644
--- a/web/packages/gallery/services/upload/index.ts
+++ b/web/packages/gallery/services/upload/index.ts
@@ -248,9 +248,7 @@ export const groupItemsBasedOnParentFolder = (
export interface LivePhotoAssets {
image: UploadItem;
- imagePathPrefix?: UploadPathPrefix;
video: UploadItem;
- videoPathPrefix?: UploadPathPrefix;
}
/**
@@ -264,6 +262,7 @@ export interface ClusteredUploadItem {
fileName: string;
isLivePhoto: boolean;
uploadItem?: UploadItem;
+ pathPrefix: UploadPathPrefix | undefined;
// TODO: Tie this to the isLivePhoto flag using a discriminated union.
livePhotoAssets?: LivePhotoAssets;
}
diff --git a/web/packages/gallery/services/upload/takeout.ts b/web/packages/gallery/services/upload/metadata-json.ts
similarity index 81%
rename from web/packages/gallery/services/upload/takeout.ts
rename to web/packages/gallery/services/upload/metadata-json.ts
index 8f85f7f465..5ca3fb1be5 100644
--- a/web/packages/gallery/services/upload/takeout.ts
+++ b/web/packages/gallery/services/upload/metadata-json.ts
@@ -4,15 +4,23 @@ import { ensureElectron } from "ente-base/electron";
import { nameAndExtension } from "ente-base/file-name";
import log from "ente-base/log";
import { type Location } from "ente-base/types";
-import type { UploadItem } from "ente-gallery/services/upload";
+import type {
+ UploadItem,
+ UploadPathPrefix,
+} from "ente-gallery/services/upload";
import { readStream } from "ente-gallery/utils/native-stream";
/**
* The data we read from the JSON metadata sidecar files.
*
- * Originally these were used to read the JSON metadata sidecar files present in
- * a Google Takeout. However, during our own export, we also write out files
- * with a similar structure.
+ * The metadata JSON file format is similar to the JSON metadata sidecar format
+ * produced by Google Takeout. This is so that the user can import their Google
+ * Takeouts while preserving the metadata.
+ *
+ * During export the Ente app also writes out its own metadata in a similar and
+ * compatible format so that the user can easily round trip (i.e. export their
+ * data out of Ente, and import it back again). This also allows users to reuse
+ * existing third party tools for working with the takeout format.
*/
export interface ParsedMetadataJSON {
creationTime?: number;
@@ -25,29 +33,54 @@ export interface ParsedMetadataJSON {
* Derive a key for the given {@link jsonFileName} that should be used to index
* into the {@link ParsedMetadataJSON} JSON map.
*
+ * @param pathPrefix The {@link UploadPathPrefix}, if available.
+ *
* @param collectionID The collection to which we're uploading.
*
* @param jsonFileName The file name for the JSON file.
*
* @returns A key suitable for indexing into the metadata JSON map.
+ *
+ * @see also {@link matchJSONMetadata}.
*/
export const metadataJSONMapKeyForJSON = (
+ pathPrefix: UploadPathPrefix | undefined,
collectionID: number,
jsonFileName: string,
-) => `${collectionID}-${jsonFileName.slice(0, -1 * ".json".length)}`;
+) =>
+ makeKey3(
+ pathPrefix,
+ collectionID,
+ jsonFileName.slice(0, -1 * ".json".length),
+ );
+
+const makeKey3 = (
+ pathPrefix: UploadPathPrefix | undefined,
+ collectionID: number,
+ fileName: string,
+) => `${pathPrefix ?? ""}-${collectionID}-${fileName}`;
/**
* Return the matching entry, if any, from {@link parsedMetadataJSONMap} for the
- * {@link fileName} and {@link collectionID} combination.
+ * {@link pathPrefix}, {@link collectionID} and {@link fileName} combination.
*
* This is the sibling of {@link metadataJSONMapKeyForJSON}, except for deriving
* the filename key we might have to try a bunch of different variations, so
* this does not return a single key but instead tries the combinations until it
* finds an entry in the map, and returns the found entry instead of the key.
+ *
+ * In brief,
+ *
+ * - When inserting the metadata entry into {@link parsedMetadataJSONMap}, we
+ * use {@link metadataJSONMapKeyForJSON}.
+ *
+ * - When reading back the metadata entry from {@link parsedMetadataJSONMap}, we
+ * use {@link matchJSONMetadata}
*/
-export const matchTakeoutMetadata = (
- fileName: string,
+export const matchJSONMetadata = (
+ pathPrefix: UploadPathPrefix | undefined,
collectionID: number,
+ fileName: string,
parsedMetadataJSONMap: Map<string, ParsedMetadataJSON>,
) => {
// Break the fileName down into its components.
@@ -85,9 +118,13 @@ export const matchTakeoutMetadata = (
name = name.slice(0, -1 * editedFileSuffix.length);
}
+ // Convenience function to wary only the file name component when
+ // constructing a key. Delegates to `makeKey3`.
+ const makeKey = (fn: string) => makeKey3(pathPrefix, collectionID, fn);
+
// Derive a key from the collection name, file name and the suffix if any.
let baseFileName = `${name}${extension}`;
- let key = `${collectionID}-${baseFileName}${numberedSuffix}`;
+ let key = makeKey(`${baseFileName}${numberedSuffix}`);
let takeoutMetadata = parsedMetadataJSONMap.get(key);
if (takeoutMetadata) return takeoutMetadata;
@@ -97,7 +134,9 @@ export const matchTakeoutMetadata = (
// the clipped file name to get the key.
const maxGoogleFileNameLength = 46;
- key = `${collectionID}-${baseFileName.slice(0, maxGoogleFileNameLength)}${numberedSuffix}`;
+ key = makeKey(
+ `${baseFileName.slice(0, maxGoogleFileNameLength)}${numberedSuffix}`,
+ );
takeoutMetadata = parsedMetadataJSONMap.get(key);
if (takeoutMetadata) return takeoutMetadata;
@@ -118,7 +157,9 @@ export const matchTakeoutMetadata = (
const supplSuffix = ".supplemental-metadata";
baseFileName = `${name}${extension}${supplSuffix}`;
- key = `${collectionID}-${baseFileName.slice(0, maxGoogleFileNameLength)}${numberedSuffix}`;
+ key = makeKey(
+ `${baseFileName.slice(0, maxGoogleFileNameLength)}${numberedSuffix}`,
+ );
takeoutMetadata = parsedMetadataJSONMap.get(key);
return takeoutMetadata;
diff --git a/web/packages/gallery/services/upload/upload-service.ts b/web/packages/gallery/services/upload/upload-service.ts
index 1241bb5247..ca1c59cc04 100644
--- a/web/packages/gallery/services/upload/upload-service.ts
+++ b/web/packages/gallery/services/upload/upload-service.ts
@@ -72,8 +72,7 @@ import {
type MultipartCompletedPart,
type ObjectUploadURL,
} from "./remote";
-import type { ParsedMetadataJSON } from "./takeout";
-import { matchTakeoutMetadata } from "./takeout";
+import { type ParsedMetadataJSON, matchJSONMetadata } from "./metadata-json";
import {
fallbackThumbnail,
generateThumbnailNative,
@@ -247,7 +246,7 @@ export const uploadItemFileName = (uploadItem: UploadItem) => {
return uploadItem.file.name;
};
-/* -- Various intermediate type used during upload -- */
+/* -- Various intermediate types used during upload -- */
export type ExternalParsedMetadata = ParsedMetadata & {
creationTime?: number | undefined;
@@ -271,11 +270,13 @@ export interface UploadAsset {
*/
uploadItem?: UploadItem;
/**
- * The path prefix of the uploadItem, if available.
+ * The path prefix of the uploadItem (if not a live photo), or of the image
+ * component of the live photo (otherwise).
*
- * Valid for non-live photos.
+ * The only expected scenario where this will not be present is when we're
+ * uploading an edited file (edited in the in-app image editor).
*/
- uploadItemPathPrefix?: UploadPathPrefix;
+ pathPrefix: UploadPathPrefix | undefined;
/**
* Metadata we know about a file externally. Valid for non-live photos.
*
@@ -353,6 +354,7 @@ export interface PotentialLivePhotoAsset {
fileType: FileType;
collectionID: number;
uploadItem: UploadItem;
+ pathPrefix: UploadPathPrefix | undefined;
}
/**
@@ -364,6 +366,7 @@ export const areLivePhotoAssets = async (
parsedMetadataJSONMap: Map<string, ParsedMetadataJSON>,
) => {
if (f.collectionID != g.collectionID) return false;
+ if (f.pathPrefix != g.pathPrefix) return false;
const [fName, fExt] = nameAndExtension(f.fileName);
const [gName, gExt] = nameAndExtension(g.fileName);
@@ -413,15 +416,16 @@ export const areLivePhotoAssets = async (
// items that coincidentally have the same name (this is not uncommon since,
// e.g. many cameras use a deterministic numbering scheme).
- const fParsedMetadataJSON = matchTakeoutMetadata(
- f.fileName,
+ const fParsedMetadataJSON = matchJSONMetadata(
+ f.pathPrefix,
f.collectionID,
+ f.fileName,
parsedMetadataJSONMap,
);
-
- const gParsedMetadataJSON = matchTakeoutMetadata(
- g.fileName,
+ const gParsedMetadataJSON = matchJSONMetadata(
+ g.pathPrefix,
g.collectionID,
+ g.fileName,
parsedMetadataJSONMap,
);
@@ -958,8 +962,9 @@ const extractAssetMetadata = async (
{
isLivePhoto,
uploadItem,
- externalParsedMetadata,
livePhotoAssets,
+ pathPrefix,
+ externalParsedMetadata,
}: UploadAsset,
fileTypeInfo: FileTypeInfo,
lastModifiedMs: number,
@@ -971,6 +976,7 @@ const extractAssetMetadata = async (
? await extractLivePhotoMetadata(
// @ts-ignore
livePhotoAssets,
+ pathPrefix,
fileTypeInfo,
lastModifiedMs,
collectionID,
@@ -980,6 +986,7 @@ const extractAssetMetadata = async (
: await extractImageOrVideoMetadata(
// @ts-ignore
uploadItem,
+ pathPrefix,
externalParsedMetadata,
fileTypeInfo,
lastModifiedMs,
@@ -990,6 +997,7 @@ const extractAssetMetadata = async (
const extractLivePhotoMetadata = async (
livePhotoAssets: LivePhotoAssets,
+ pathPrefix: UploadPathPrefix | undefined,
fileTypeInfo: FileTypeInfo,
lastModifiedMs: number,
collectionID: number,
@@ -1004,6 +1012,7 @@ const extractLivePhotoMetadata = async (
const { metadata: imageMetadata, publicMagicMetadata } =
await extractImageOrVideoMetadata(
livePhotoAssets.image,
+ pathPrefix,
undefined,
imageFileTypeInfo,
lastModifiedMs,
@@ -1030,6 +1039,7 @@ const extractLivePhotoMetadata = async (
const extractImageOrVideoMetadata = async (
uploadItem: UploadItem,
+ pathPrefix: UploadPathPrefix | undefined,
externalParsedMetadata: ExternalParsedMetadata | undefined,
fileTypeInfo: FileTypeInfo,
lastModifiedMs: number,
@@ -1066,9 +1076,10 @@ const extractImageOrVideoMetadata = async (
//
// See: [Note: Duplicate retrieval of creation date for live photo clubbing]
- const parsedMetadataJSON = matchTakeoutMetadata(
- fileName,
+ const parsedMetadataJSON = matchJSONMetadata(
+ pathPrefix,
collectionID,
+ fileName,
parsedMetadataJSONMap,
);