update google json matching logic (#1432)
This commit is contained in:
commit
c324b6b5d4
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue