From 8afcd3a766893c6cfd67cae7ad3ad9cf6bd782a4 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Fri, 29 Oct 2021 17:15:06 +0530 Subject: [PATCH] refactor changeFilesVisibility to have a seperate updateMagicMetadata function and move encryption logic to upateMagicMetadata API --- src/services/fileService.ts | 17 ++++++++-- src/utils/file/index.ts | 68 ++++++++++++++++++++++--------------- 2 files changed, 56 insertions(+), 29 deletions(-) diff --git a/src/services/fileService.ts b/src/services/fileService.ts index 02144c5da..a93092dc9 100644 --- a/src/services/fileService.ts +++ b/src/services/fileService.ts @@ -2,11 +2,16 @@ import { getEndpoint } from 'utils/common/apiUtil'; import localForage from 'utils/storage/localForage'; import { getToken } from 'utils/common/key'; -import { DataStream, MetadataObject } from './upload/uploadService'; +import { + DataStream, + EncryptionResult, + MetadataObject, +} from './upload/uploadService'; import { Collection } from './collectionService'; import HTTPService from './HTTPService'; import { logError } from 'utils/sentry'; import { decryptFile, sortFiles } from 'utils/file'; +import CryptoWorker from 'utils/crypto'; const ENDPOINT = getEndpoint(); const DIFF_LIMIT: number = 1000; @@ -280,10 +285,18 @@ export const updateMagicMetadata = async (files: File[]) => { return; } const reqBody: UpdateMagicMetadataRequest = { metadataList: [] }; + const worker = await new CryptoWorker(); for (const file of files) { + const { file: encryptedMagicMetadata }: EncryptionResult = + await worker.encryptMetadata(file.magicMetadata.data, file.key); reqBody.metadataList.push({ id: file.id, - magicMetadata: file.magicMetadata as EncryptedMagicMetadataCore, + magicMetadata: { + version: file.pubMagicMetadata.version, + count: file.pubMagicMetadata.count, + data: encryptedMagicMetadata.encryptedData as unknown as string, + header: encryptedMagicMetadata.decryptionHeader, + }, }); } await HTTPService.put(`${ENDPOINT}/files/magic-metadata`, reqBody, null, { diff --git a/src/utils/file/index.ts b/src/utils/file/index.ts index 154639c32..bb00afaba 100644 --- a/src/utils/file/index.ts +++ b/src/utils/file/index.ts @@ -10,7 +10,6 @@ import { } from 'services/fileService'; import { decodeMotionPhoto } from 'services/motionPhotoService'; import { getMimeTypeFromBlob } from 'services/upload/readFileService'; -import { EncryptionResult } from 'services/upload/uploadService'; import DownloadManger from 'services/downloadManager'; import { logError } from 'utils/sentry'; import { User } from 'services/userService'; @@ -245,46 +244,61 @@ export function fileIsArchived(file: File) { return file.magicMetadata.data.visibility === VISIBILITY_STATE.ARCHIVED; } +export async function updateMagicMetadata( + file: File, + magicMetadataUpdates: MagicMetadataProps +) { + const worker = await new CryptoWorker(); + + if (!file.magicMetadata) { + file.magicMetadata = NEW_MAGIC_METADATA; + } + if (typeof file.magicMetadata.data === 'string') { + file.magicMetadata.data = (await worker.decryptMetadata( + file.magicMetadata.data, + file.magicMetadata.header, + file.key + )) as MagicMetadataProps; + } + if (magicMetadataUpdates) { + // copies the existing magic metadata properties of the files and updates the visibility value + // The expected behaviour while updating magic metadata is to let the existing property as it is and update/add the property you want + const magicMetadataProps: MagicMetadataProps = { + ...file.magicMetadata.data, + ...magicMetadataUpdates, + }; + + return { + ...file, + magicMetadata: { + ...file.pubMagicMetadata, + data: magicMetadataProps, + }, + }; + } else { + return file; + } +} + export async function changeFilesVisibility( files: File[], selected: SelectedState, visibility: VISIBILITY_STATE ) { - const worker = await new CryptoWorker(); const selectedFiles = getSelectedFiles(selected, files); const updatedFiles: File[] = []; for (const file of selectedFiles) { - if (!file.magicMetadata) { - file.magicMetadata = NEW_MAGIC_METADATA; - } - if (typeof file.magicMetadata.data === 'string') { - file.magicMetadata.data = (await worker.decryptMetadata( - file.magicMetadata.data, - file.magicMetadata.header, - file.key - )) as MagicMetadataProps; - } - // copies the existing magic metadata properties of the files and updates the visibility value - // The expected behaviour while updating magic metadata is to let the existing property as it is and update/add the property you want const updatedMagicMetadataProps: MagicMetadataProps = { - ...file.magicMetadata.data, visibility, }; - const encryptedMagicMetadata: EncryptionResult = - await worker.encryptMetadata(updatedMagicMetadataProps, file.key); - updatedFiles.push({ - ...file, - magicMetadata: { - version: file.magicMetadata.version, - count: Object.keys(updatedMagicMetadataProps).length, - data: encryptedMagicMetadata.file - .encryptedData as unknown as string, - header: encryptedMagicMetadata.file.decryptionHeader, - }, - }); + + updatedFiles.push( + await updateMagicMetadata(file, updatedMagicMetadataProps) + ); } return updatedFiles; } + export function isSharedFile(file: File) { const user: User = getData(LS_KEYS.USER);