update google json matching logic (#1432)

This commit is contained in:
Vishnu Mohandas 2023-11-17 17:51:33 +05:30 committed by GitHub
commit c324b6b5d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 22 deletions

View file

@ -15,7 +15,11 @@ import { addLogLine } from '@ente/shared/logging';
import { getFileNameSize } from '@ente/shared/logging/web';
import { encryptFiledata } from './encryptionService';
import { extractMetadata, getMetadataJSONMapKey } from './metadataService';
import {
extractMetadata,
getClippedMetadataJSONMapKeyForFile,
getFullMetadataJSONMapKeyForFile,
} from './metadataService';
import {
getFileStream,
getElectronFileStream,
@ -80,8 +84,12 @@ export async function extractFileMetadata(
const originalName = getFileOriginalName(rawFile);
const googleMetadata =
parsedMetadataJSONMap.get(
getMetadataJSONMapKey(collectionID, originalName)
) ?? {};
getFullMetadataJSONMapKeyForFile(collectionID, originalName)
) ??
parsedMetadataJSONMap.get(
getClippedMetadataJSONMapKeyForFile(collectionID, originalName)
) ??
{};
const { metadata, publicMagicMetadata } = await extractMetadata(
worker,

View file

@ -21,11 +21,7 @@ import { getFileHash } from './hashService';
import { Remote } from 'comlink';
import { DedicatedCryptoWorker } from '@ente/shared/crypto/internal/crypto.worker';
import { FilePublicMagicMetadataProps } from 'types/file';
interface ParsedMetadataJSONWithTitle {
title: string;
parsedMetadataJSON: ParsedMetadataJSON;
}
import { splitFilenameAndExtension } from 'utils/file';
const NULL_PARSED_METADATA_JSON: ParsedMetadataJSON = {
creationTime: null,
@ -116,11 +112,36 @@ export async function getImageMetadata(
return imageMetadata;
}
export const getMetadataJSONMapKey = (
export const getMetadataJSONMapKeyForJSON = (
collectionID: number,
jsonFileName: string
) => {
let title = jsonFileName.slice(0, -1 * '.json'.length);
const endsWithNumberedSuffixWithBrackets = title.match(/\(\d+\)$/);
if (endsWithNumberedSuffixWithBrackets) {
title = title.slice(
0,
-1 * endsWithNumberedSuffixWithBrackets[0].length
);
const [name, extension] = splitFilenameAndExtension(title);
return `${collectionID}-${name}${endsWithNumberedSuffixWithBrackets}.${extension}`;
}
return `${collectionID}-${title}`;
};
title: string
) => `${collectionID}-${title}`;
export const getClippedMetadataJSONMapKeyForFile = (
collectionID: number,
fileName: string
) => {
return `${collectionID}-${fileName.slice(0, 46)}`;
};
export const getFullMetadataJSONMapKeyForFile = (
collectionID: number,
fileName: string
) => {
return `${collectionID}-${fileName}`;
};
export async function parseMetadataJSON(receivedFile: File | ElectronFile) {
try {
@ -134,11 +155,10 @@ export async function parseMetadataJSON(receivedFile: File | ElectronFile) {
const parsedMetadataJSON: ParsedMetadataJSON =
NULL_PARSED_METADATA_JSON;
if (!metadataJSON || !metadataJSON['title']) {
if (!metadataJSON) {
return;
}
const title = metadataJSON['title'];
if (
metadataJSON['photoTakenTime'] &&
metadataJSON['photoTakenTime']['timestamp']
@ -177,7 +197,7 @@ export async function parseMetadataJSON(receivedFile: File | ElectronFile) {
parsedMetadataJSON.latitude = locationData.latitude;
parsedMetadataJSON.longitude = locationData.longitude;
}
return { title, parsedMetadataJSON } as ParsedMetadataJSONWithTitle;
return parsedMetadataJSON;
} catch (e) {
logError(e, 'parseMetadataJSON failed');
// ignore

View file

@ -2,7 +2,8 @@ import { getLocalFiles } from '../fileService';
import { SetFiles } from 'types/gallery';
import { sortFiles, decryptFile, getUserOwnedFiles } from 'utils/file';
import { logError } from '@ente/shared/sentry';
import { getMetadataJSONMapKey, parseMetadataJSON } from './metadataService';
import { parseMetadataJSON } from './metadataService';
import { getMetadataJSONMapKeyForJSON } from './metadataService';
import {
areFileWithCollectionsSame,
segregateMetadataAndMediaFiles,
@ -217,14 +218,13 @@ class UploadManager {
`parsing metadata json file ${getFileNameSize(file)}`
);
const parsedMetadataJSONWithTitle = await parseMetadataJSON(
file
);
if (parsedMetadataJSONWithTitle) {
const { title, parsedMetadataJSON } =
parsedMetadataJSONWithTitle;
const parsedMetadataJSON = await parseMetadataJSON(file);
if (parsedMetadataJSON) {
this.parsedMetadataJSONMap.set(
getMetadataJSONMapKey(collectionID, title),
getMetadataJSONMapKeyForJSON(
collectionID,
file.name
),
parsedMetadataJSON && { ...parsedMetadataJSON }
);
UIService.increaseFileUploaded();