Skip to content

armgit / ente

Public Imported from GitHub
0

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

Funnel

Manav Rathi54f9bd880a84
diff --git a/web/apps/photos/src/services/deduplicationService.ts b/web/apps/photos/src/services/deduplicationService.ts
index fb2b9237b9..0590bb4e88 100644
--- a/web/apps/photos/src/services/deduplicationService.ts
+++ b/web/apps/photos/src/services/deduplicationService.ts
@@ -1,8 +1,7 @@
import log from "@/base/log";
import { apiURL } from "@/base/origins";
-import { EnteFile, hasFileHash } from "@/media/file";
-import type { Metadata } from "@/media/file-metadata";
-import { FileType } from "@/media/file-type";
+import { EnteFile } from "@/media/file";
+import { metadataHash, type Metadata } from "@/media/file-metadata";
import HTTPService from "@ente/shared/network/HTTPService";
import { getToken } from "@ente/shared/storage/localStorage/helpers";
@@ -65,6 +64,8 @@ export async function getDuplicates(
}
}
+const hasFileHash = (file: Metadata) => !!metadataHash(file);
+
function getDupesGroupedBySameFileHashes(dupe: Duplicate) {
const result: Duplicate[] = [];
@@ -103,9 +104,7 @@ function groupDupesByFileHashes(dupe: Duplicate) {
.map((file) => {
return {
file,
- hash:
- file.metadata.hash ??
- `${file.metadata.imageHash}_${file.metadata.videoHash}`,
+ hash: metadataHash(file.metadata),
};
})
.sort((firstFile, secondFile) => {
@@ -173,15 +172,5 @@ async function sortDuplicateFiles(
}
function areFileHashesSame(firstFile: Metadata, secondFile: Metadata) {
- if (
- firstFile.fileType === FileType.livePhoto &&
- firstFile.hash === undefined
- ) {
- return (
- firstFile.imageHash === secondFile.imageHash &&
- firstFile.videoHash === secondFile.videoHash
- );
- } else {
- return firstFile.hash === secondFile.hash;
- }
+ return metadataHash(firstFile) === metadataHash(secondFile);
}
diff --git a/web/apps/photos/src/services/upload/upload-service.ts b/web/apps/photos/src/services/upload/upload-service.ts
index 2ee52a83bc..72a475bc44 100644
--- a/web/apps/photos/src/services/upload/upload-service.ts
+++ b/web/apps/photos/src/services/upload/upload-service.ts
@@ -15,17 +15,17 @@ import { readStream } from "@/gallery/utils/native-stream";
import {
EncryptedMagicMetadata,
EnteFile,
- hasFileHash,
MetadataFileAttributes,
S3FileAttributes,
type EncryptedEnteFile,
type FilePublicMagicMetadata,
type FilePublicMagicMetadataProps,
} from "@/media/file";
-import type {
- Metadata,
- ParsedMetadata,
- PublicMagicMetadata,
+import {
+ metadataHash,
+ type Metadata,
+ type ParsedMetadata,
+ type PublicMagicMetadata,
} from "@/media/file-metadata";
import { FileType, type FileTypeInfo } from "@/media/file-type";
import { encodeLivePhoto } from "@/media/live-photo";
@@ -1140,52 +1140,12 @@ const computeHash = async (uploadItem: UploadItem, worker: CryptoWorker) => {
* available), so this also in effect compares the contents of the files, not
* just the "meta" information about them.
*/
-const areFilesSame = (f: Metadata, g: Metadata) =>
- hasFileHash(f) && hasFileHash(g)
- ? areFilesSameHash(f, g)
- : areFilesSameNoHash(f, g);
-
-const areFilesSameHash = (f: Metadata, g: Metadata) => {
- if (f.fileType !== g.fileType || f.title !== g.title) {
- return false;
- }
- if (
- f.fileType === FileType.livePhoto &&
- (f.hash === undefined || g.hash === undefined)
- ) {
- return f.imageHash === g.imageHash && f.videoHash === g.videoHash;
- } else {
- return f.hash === g.hash;
- }
-};
+const areFilesSame = (f: Metadata, g: Metadata) => {
+ if (f.fileType !== g.fileType || f.title !== g.title) return false;
-/**
- * Older files that were uploaded before we introduced hashing will not have
- * hashes, so retain and use the logic we used back then for such files.
- *
- * Deprecation notice April 2024: Note that hashing was introduced very early
- * (years ago), so the chance of us finding files without hashes is rare. And
- * even in these cases, the worst that'll happen is that a duplicate file would
- * get uploaded which can later be deduped. So we can get rid of this case at
- * some point (e.g. the mobile app doesn't do this extra check, just uploads).
- */
-const areFilesSameNoHash = (f: Metadata, g: Metadata) => {
- /*
- * The maximum difference in the creation/modification times of two similar
- * files is set to 1 second. This is because while uploading files in the
- * web - browsers and users could have set reduced precision of file times
- * to prevent timing attacks and fingerprinting.
- *
- * See:
- * https://developer.mozilla.org/en-US/docs/Web/API/File/lastModified#reduced_time_precision
- */
- const oneSecond = 1e6;
- return (
- f.fileType == g.fileType &&
- f.title == g.title &&
- Math.abs(f.creationTime - g.creationTime) < oneSecond &&
- Math.abs(f.modificationTime - g.modificationTime) < oneSecond
- );
+ const fh = metadataHash(f);
+ const gh = metadataHash(g);
+ return fh && gh && fh == gh;
};
const readAsset = async (
diff --git a/web/packages/media/file-metadata.ts b/web/packages/media/file-metadata.ts
index 4f5fab3830..b9c096c093 100644
--- a/web/packages/media/file-metadata.ts
+++ b/web/packages/media/file-metadata.ts
@@ -292,6 +292,32 @@ const PublicMagicMetadata = z
})
.passthrough();
+/**
+ * Return the hash of the file by reading it from its metadata.
+ *
+ * This is a convenience function that directly reads the information from the
+ * metadata in the happy path, but also has branches to handle the legacy format
+ * that older clients used to upload. For more details, see the note in the
+ * documentation for {@link hash} in {@link Metadata}.
+ */
+export const metadataHash = (metadata: Metadata) => {
+ const hash = metadata.hash;
+ if (hash) return hash;
+
+ // Handle past live photos upload from web client.
+ if (
+ metadata.fileType == FileType.livePhoto &&
+ metadata.imageHash &&
+ metadata.videoHash
+ ) {
+ return `${metadata.imageHash}:${metadata.videoHash}`;
+ }
+
+ // Items uploaded by very old clients might not have a hash, so this is not
+ // necessarily an error even if rare.
+ return undefined;
+};
+
/**
* Return the public magic metadata for the given {@link file}.
*
diff --git a/web/packages/media/file.ts b/web/packages/media/file.ts
index 276f7de985..4979b7be35 100644
--- a/web/packages/media/file.ts
+++ b/web/packages/media/file.ts
@@ -328,5 +328,3 @@ export const mergeMetadata1 = (file: EnteFile): EnteFile => {
export const mergeMetadata = (files: EnteFile[]) =>
files.map((file) => mergeMetadata1(file));
-export const hasFileHash = (file: Metadata) =>
- !!file.hash || (!!file.imageHash && !!file.videoHash);
diff --git a/web/packages/new/photos/services/dedup.ts b/web/packages/new/photos/services/dedup.ts
index 6d3b8260a1..a243096031 100644
--- a/web/packages/new/photos/services/dedup.ts
+++ b/web/packages/new/photos/services/dedup.ts
@@ -1,5 +1,6 @@
import { newID } from "@/base/id";
import type { EnteFile } from "@/media/file";
+import { metadataHash } from "@/media/file-metadata";
import { wait } from "@/utils/promise";
import { createCollectionNameByID } from "./collection";
import { getLocalCollections } from "./collections";
@@ -87,9 +88,7 @@ export const deduceDuplicates = async () => {
const filesByHash = new Map<string, EnteFile[]>();
for (const file of files) {
- let hash = file.metadata.hash;
- if (!hash && file.metadata.imageHash && file.metadata.videoHash)
- hash = `${file.metadata.imageHash}_${file.metadata.videoHash}`;
+ const hash = metadataHash(file.metadata);
if (!hash) {
// Some very old files uploaded by ancient versions of Ente might
// not have hashes. Ignore these.
@@ -149,7 +148,6 @@ export const removeSelectedDuplicateGroups = async (
duplicateGroups: DuplicateGroup[],
) => {
console.log(duplicateGroups);
- for (const duplicateGroup of duplicateGroups) {
- }
+ // for (const duplicateGroup of duplicateGroups) {}
await wait(1000);
};
diff --git a/web/packages/new/photos/services/files.ts b/web/packages/new/photos/services/files.ts
index 112dc2ea52..d01a19f33c 100644
--- a/web/packages/new/photos/services/files.ts
+++ b/web/packages/new/photos/services/files.ts
@@ -1,6 +1,6 @@
import { blobCache } from "@/base/blob-cache";
import { mergeMetadata, type EnteFile, type Trash } from "@/media/file";
-import { FileType } from "@/media/file-type";
+import { metadataHash } from "@/media/file-metadata";
import localForage from "@ente/shared/storage/localForage";
const FILES_TABLE = "files";
@@ -178,11 +178,7 @@ export const clearCachedThumbnailsIfChanged = async (
// Both files exist, have metadata, but their (appropriate) hashes
// differ, which indicates that the change was in the file's contents,
// not the metadata itself, and thus we should refresh the thumbnail.
- if (
- m1.fileType == FileType.livePhoto && m1.imageHash
- ? m1.imageHash != m2.imageHash
- : m1.hash != m2.hash
- ) {
+ if (metadataHash(m1) != metadataHash(m2)) {
// This is an infrequent occurrence, so we lazily get the cache.
const thumbnailCache = await blobCache("thumbs");
const key = newFile.id.toString();