Skip to content

armgit / ente

Public Imported from GitHub
0

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

add tests for mappingFileAndJSONFileCheck

Abhinav52cc3d9c4e96
diff --git a/apps/photos/src/services/upload/fileService.ts b/apps/photos/src/services/upload/fileService.ts
index 675d808794..91fdb351dc 100644
--- a/apps/photos/src/services/upload/fileService.ts
+++ b/apps/photos/src/services/upload/fileService.ts
@@ -9,7 +9,6 @@ import {
ElectronFile,
ExtractMetadataResult,
} from 'types/upload';
-import { splitFilenameAndExtension } from 'utils/file';
import { logError } from '@ente/shared/sentry';
import { addLogLine } from '@ente/shared/logging';
import { getFileNameSize } from '@ente/shared/logging/web';
@@ -30,8 +29,6 @@ import { DedicatedCryptoWorker } from '@ente/shared/crypto/internal/crypto.worke
import { Remote } from 'comlink';
import { EncryptedMagicMetadata } from 'types/magicMetadata';
-const EDITED_FILE_SUFFIX = '-edited';
-
export function getFileSize(file: File | ElectronFile) {
return file.size;
}
@@ -81,13 +78,12 @@ export async function extractFileMetadata(
fileTypeInfo: FileTypeInfo,
rawFile: File | ElectronFile
): Promise<ExtractMetadataResult> {
- const originalName = getFileOriginalName(rawFile);
const googleMetadata =
parsedMetadataJSONMap.get(
- getFullMetadataJSONMapKeyForFile(collectionID, originalName)
+ getFullMetadataJSONMapKeyForFile(collectionID, rawFile.name)
) ??
parsedMetadataJSONMap.get(
- getClippedMetadataJSONMapKeyForFile(collectionID, originalName)
+ getClippedMetadataJSONMapKeyForFile(collectionID, rawFile.name)
) ??
{};
@@ -159,28 +155,3 @@ export async function encryptFile(
throw e;
}
}
-
-/*
- Get the original file name for edited file to associate it to original file's metadataJSON file
- as edited file doesn't have their own metadata file
-*/
-function getFileOriginalName(file: File | ElectronFile) {
- let originalName: string = null;
- const [nameWithoutExtension, extension] = splitFilenameAndExtension(
- file.name
- );
-
- const isEditedFile = nameWithoutExtension.endsWith(EDITED_FILE_SUFFIX);
- if (isEditedFile) {
- originalName = nameWithoutExtension.slice(
- 0,
- -1 * EDITED_FILE_SUFFIX.length
- );
- } else {
- originalName = nameWithoutExtension;
- }
- if (extension) {
- originalName += '.' + extension;
- }
- return originalName;
-}
diff --git a/apps/photos/src/services/upload/metadataService.ts b/apps/photos/src/services/upload/metadataService.ts
index 1eacb79d66..d310a0b7e4 100644
--- a/apps/photos/src/services/upload/metadataService.ts
+++ b/apps/photos/src/services/upload/metadataService.ts
@@ -140,7 +140,7 @@ export const getFullMetadataJSONMapKeyForFile = (
collectionID: number,
fileName: string
) => {
- return `${collectionID}-${fileName}`;
+ return `${collectionID}-${getFileOriginalName(fileName)}`;
};
export async function parseMetadataJSON(receivedFile: File | ElectronFile) {
@@ -239,3 +239,29 @@ function convertSignalNameToFusedDateString(filename: string) {
const dateStringParts = filename.split('-');
return `${dateStringParts[1]}${dateStringParts[2]}${dateStringParts[3]}-${dateStringParts[4]}`;
}
+
+const EDITED_FILE_SUFFIX = '-edited';
+
+/*
+ Get the original file name for edited file to associate it to original file's metadataJSON file
+ as edited file doesn't have their own metadata file
+*/
+function getFileOriginalName(fileName: string) {
+ let originalName: string = null;
+ const [nameWithoutExtension, extension] =
+ splitFilenameAndExtension(fileName);
+
+ const isEditedFile = nameWithoutExtension.endsWith(EDITED_FILE_SUFFIX);
+ if (isEditedFile) {
+ originalName = nameWithoutExtension.slice(
+ 0,
+ -1 * EDITED_FILE_SUFFIX.length
+ );
+ } else {
+ originalName = nameWithoutExtension;
+ }
+ if (extension) {
+ originalName += '.' + extension;
+ }
+ return originalName;
+}
diff --git a/apps/photos/tests/upload.test.ts b/apps/photos/tests/upload.test.ts
index a5ff2263de..75e2bfa0bc 100644
--- a/apps/photos/tests/upload.test.ts
+++ b/apps/photos/tests/upload.test.ts
@@ -4,6 +4,11 @@ import { getUserDetailsV2 } from 'services/userService';
import { groupFilesBasedOnCollectionID } from 'utils/file';
import { FILE_TYPE } from 'constants/file';
import { tryToParseDateTime } from '@ente/shared/time';
+import {
+ getClippedMetadataJSONMapKeyForFile,
+ getFullMetadataJSONMapKeyForFile,
+ getMetadataJSONMapKeyForJSON,
+} from 'services/upload/metadataService';
const DATE_TIME_PARSING_TEST_FILE_NAMES = [
{
@@ -55,6 +60,37 @@ const DATE_TIME_PARSING_TEST_FILE_NAMES = [
},
];
+const FILE_NAME_TO_JSON_NAME = [
+ {
+ filename: 'IMG20210211125718-edited.jpg',
+ jsonFilename: 'IMG20210211125718.jpg.json',
+ },
+ {
+ filename: 'IMG20210211174722.jpg',
+ jsonFilename: 'IMG20210211174722.jpg.json',
+ },
+ {
+ filename: '21345678901234567890123456789012345678901234567.png',
+ jsonFilename: '2134567890123456789012345678901234567890123456.json',
+ },
+ {
+ filename: 'IMG20210211174722(1).jpg',
+ jsonFilename: 'IMG20210211174722.jpg(1).json',
+ },
+ {
+ filename: 'IMG2021021(4455)74722(1).jpg',
+ jsonFilename: 'IMG2021021(4455)74722.jpg(1).json',
+ },
+ {
+ filename: 'IMG2021021(json)74722(1).jpg',
+ jsonFilename: 'IMG2021021(json)74722.jpg(1).json',
+ },
+ {
+ filename: 'IMG2021021(1)74722(1).jpg',
+ jsonFilename: 'IMG2021021(1)74722.jpg(1).json',
+ },
+];
+
export async function testUpload() {
if (!process.env.NEXT_PUBLIC_EXPECTED_JSON_PATH) {
throw Error(
@@ -78,6 +114,7 @@ export async function testUpload() {
await googleMetadataReadingCheck(expectedState);
await totalFileCountCheck(expectedState);
parseDateTimeFromFileNameTest();
+ mappingFileAndJSONFileCheck();
} catch (e) {
console.log(e);
}
@@ -352,6 +389,34 @@ function parseDateTimeFromFileNameTest() {
console.log('parseDateTimeFromFileNameTest passed βœ…');
}
+function mappingFileAndJSONFileCheck() {
+ FILE_NAME_TO_JSON_NAME.forEach(({ filename, jsonFilename }) => {
+ const jsonFileNameGeneratedKey = getMetadataJSONMapKeyForJSON(
+ 0,
+ jsonFilename
+ );
+ let fileNameGeneratedKey = getFullMetadataJSONMapKeyForFile(
+ 0,
+ filename
+ );
+ if (fileNameGeneratedKey !== jsonFileNameGeneratedKey) {
+ fileNameGeneratedKey = getClippedMetadataJSONMapKeyForFile(
+ 0,
+ filename
+ );
+ }
+
+ if (fileNameGeneratedKey !== jsonFileNameGeneratedKey) {
+ throw Error(
+ `mappingFileAndJSONFileCheck failed ❌ ,
+ for ${filename}
+ expected: ${jsonFileNameGeneratedKey} got: ${fileNameGeneratedKey}`
+ );
+ }
+ });
+ console.log('mappingFileAndJSONFileCheck passed βœ…');
+}
+
// format: YYYY-MM-DD HH:MM:SS
function getFormattedDateTime(date: Date) {
const year = date.getFullYear();