update ExtractMetadata to return both metadata and publicMagicMetadata

This commit is contained in:
Abhinav 2023-06-05 23:22:17 +05:30
parent 6a8a6cae76
commit d61d788d1e
7 changed files with 49 additions and 26 deletions

View file

@ -2,12 +2,12 @@ import { MULTIPART_PART_SIZE, FILE_READER_CHUNK_SIZE } from 'constants/upload';
import {
FileTypeInfo,
FileInMemory,
Metadata,
EncryptedFile,
FileWithMetadata,
ParsedMetadataJSONMap,
DataStream,
ElectronFile,
ExtractMetadataResult,
} from 'types/upload';
import { splitFilenameAndExtension } from 'utils/file';
import { logError } from 'utils/sentry';
@ -74,13 +74,14 @@ export async function extractFileMetadata(
collectionID: number,
fileTypeInfo: FileTypeInfo,
rawFile: File | ElectronFile
) {
): Promise<ExtractMetadataResult> {
const originalName = getFileOriginalName(rawFile);
const googleMetadata =
parsedMetadataJSONMap.get(
getMetadataJSONMapKey(collectionID, originalName)
) ?? {};
const extractedMetadata: Metadata = await extractMetadata(
const { metadata, publicMagicMetadata } = await extractMetadata(
worker,
rawFile,
fileTypeInfo
@ -90,9 +91,9 @@ export async function extractFileMetadata(
if (!value) {
continue;
}
extractedMetadata[key] = value;
metadata[key] = value;
}
return extractedMetadata;
return { metadata, publicMagicMetadata };
}
export async function encryptFile(

View file

@ -8,6 +8,7 @@ import {
FileWithCollection,
LivePhotoAssets,
ParsedMetadataJSONMap,
ExtractMetadataResult,
} from 'types/upload';
import { CustomError } from 'utils/error';
import { getFileTypeFromExtensionForLivePhotoClustering } from 'utils/file/livePhoto';
@ -51,12 +52,15 @@ export async function extractLivePhotoMetadata(
collectionID: number,
fileTypeInfo: FileTypeInfo,
livePhotoAssets: LivePhotoAssets
) {
): Promise<ExtractMetadataResult> {
const imageFileTypeInfo: FileTypeInfo = {
fileType: FILE_TYPE.IMAGE,
exactType: fileTypeInfo.imageType,
};
const imageMetadata = await extractFileMetadata(
const {
metadata: imageMetadata,
publicMagicMetadata: imagePublicMagicMetadata,
} = await extractFileMetadata(
worker,
parsedMetadataJSONMap,
collectionID,
@ -65,12 +69,15 @@ export async function extractLivePhotoMetadata(
);
const videoHash = await getFileHash(worker, livePhotoAssets.video);
return {
...imageMetadata,
title: getLivePhotoName(livePhotoAssets),
fileType: FILE_TYPE.LIVE_PHOTO,
imageHash: imageMetadata.hash,
videoHash: videoHash,
hash: undefined,
metadata: {
...imageMetadata,
title: getLivePhotoName(livePhotoAssets),
fileType: FILE_TYPE.LIVE_PHOTO,
imageHash: imageMetadata.hash,
videoHash: videoHash,
hash: undefined,
},
publicMagicMetadata: imagePublicMagicMetadata,
};
}

View file

@ -8,6 +8,7 @@ import {
FileTypeInfo,
ParsedExtractedMetadata,
ElectronFile,
ExtractMetadataResult as ExtractMetadataResult,
} from 'types/upload';
import { NULL_EXTRACTED_METADATA, NULL_LOCATION } from 'constants/upload';
import { getVideoMetadata } from './videoMetadataService';
@ -19,6 +20,7 @@ import {
import { getFileHash } from './hashService';
import { Remote } from 'comlink';
import { DedicatedCryptoWorker } from 'worker/crypto.worker';
import { FilePublicMagicMetadataProps } from 'types/magicMetadata';
interface ParsedMetadataJSONWithTitle {
title: string;
@ -52,7 +54,7 @@ export async function extractMetadata(
worker: Remote<DedicatedCryptoWorker>,
receivedFile: File | ElectronFile,
fileTypeInfo: FileTypeInfo
) {
): Promise<ExtractMetadataResult> {
let extractedMetadata: ParsedExtractedMetadata = NULL_EXTRACTED_METADATA;
if (fileTypeInfo.fileType === FILE_TYPE.IMAGE) {
extractedMetadata = await getImageMetadata(receivedFile, fileTypeInfo);
@ -72,10 +74,12 @@ export async function extractMetadata(
longitude: extractedMetadata.location.longitude,
fileType: fileTypeInfo.fileType,
hash: fileHash,
};
const publicMagicMetadata: FilePublicMagicMetadataProps = {
w: extractedMetadata.width,
h: extractedMetadata.height,
};
return metadata;
return { metadata, publicMagicMetadata };
}
export async function getImageMetadata(

View file

@ -7,11 +7,11 @@ import { CustomError, handleUploadError } from 'utils/error';
import {
BackupedFile,
EncryptedFile,
ExtractMetadataResult,
FileTypeInfo,
FileWithCollection,
FileWithMetadata,
isDataStream,
Metadata,
ParsedMetadataJSON,
ParsedMetadataJSONMap,
ProcessedFile,
@ -109,7 +109,7 @@ class UploadService {
{ isLivePhoto, file, livePhotoAssets }: UploadAsset,
collectionID: number,
fileTypeInfo: FileTypeInfo
): Promise<Metadata> {
): Promise<ExtractMetadataResult> {
return isLivePhoto
? extractLivePhotoMetadata(
worker,

View file

@ -55,12 +55,13 @@ export default async function uploader(
);
addLogLine(`extracting metadata ${fileNameSize}`);
const metadata = await UploadService.extractAssetMetadata(
worker,
uploadAsset,
collection.id,
fileTypeInfo
);
const { metadata, publicMagicMetadata } =
await UploadService.extractAssetMetadata(
worker,
uploadAsset,
collection.id,
fileTypeInfo
);
const matchingExistingFiles = findMatchingExistingFiles(
existingFiles,
@ -117,7 +118,11 @@ export default async function uploader(
metadata.hasStaticThumbnail = true;
}
let pubMagicMetadata: FilePublicMagicMetadata;
if (uploaderName) {
if (publicMagicMetadata) {
pubMagicMetadata = await uploadService.constructPublicMagicMetadata(
publicMagicMetadata
);
} else if (uploaderName) {
pubMagicMetadata = await uploadService.constructPublicMagicMetadata(
{ uploaderName }
);

View file

@ -24,6 +24,8 @@ export interface FilePublicMagicMetadataProps {
editedName?: string;
caption?: string;
uploaderName?: string;
w?: number;
h?: number;
}
export interface FilePublicMagicMetadata

View file

@ -5,6 +5,7 @@ import { MetadataFileAttributes, S3FileAttributes } from 'types/file';
import {
EncryptedMagicMetadata,
FilePublicMagicMetadata,
FilePublicMagicMetadataProps,
} from 'types/magicMetadata';
export interface DataStream {
@ -27,8 +28,6 @@ export interface Metadata {
hash?: string;
imageHash?: string;
videoHash?: string;
w?: number;
h?: number;
}
export interface Location {
@ -155,3 +154,8 @@ export interface PublicUploadProps {
passwordToken: string;
accessedThroughSharedURL: boolean;
}
export interface ExtractMetadataResult {
metadata: Metadata;
publicMagicMetadata: FilePublicMagicMetadataProps;
}