From e216a29583aef70d5baa640a6fbcb7df6a34532b Mon Sep 17 00:00:00 2001 From: Abhinav Date: Sun, 4 Dec 2022 22:56:57 +0530 Subject: [PATCH] split FileAttribute type into 3 separate sub types --- src/services/migrateThumbnailService.ts | 6 +++--- src/services/upload/fileService.ts | 3 ++- src/types/file/index.ts | 19 ++++++++++++++----- src/types/upload/index.ts | 22 ++++++++++++++++------ src/utils/file/index.ts | 4 ++-- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/services/migrateThumbnailService.ts b/src/services/migrateThumbnailService.ts index 90a86b71d..f3ae9aaa6 100644 --- a/src/services/migrateThumbnailService.ts +++ b/src/services/migrateThumbnailService.ts @@ -11,7 +11,7 @@ import { SetProgressTracker } from 'components/FixLargeThumbnail'; import { getFileType } from 'services/typeDetectionService'; import { getLocalTrash, getTrashedFiles } from './trashService'; import { EncryptionResult, UploadURL } from 'types/upload'; -import { fileAttribute } from 'types/file'; +import { S3FileAttribute } from 'types/file'; import { USE_CF_PROXY } from 'constants/upload'; const ENDPOINT = getEndpoint(); @@ -106,7 +106,7 @@ export async function uploadThumbnail( fileKey: string, updatedThumbnail: Uint8Array, uploadURL: UploadURL -): Promise { +): Promise { const { file: encryptedThumbnail }: EncryptionResult = await worker.encryptThumbnail(updatedThumbnail, fileKey); let thumbnailObjectKey: string = null; @@ -131,7 +131,7 @@ export async function uploadThumbnail( export async function updateThumbnail( fileID: number, - newThumbnail: fileAttribute + newThumbnail: S3FileAttribute ) { try { const token = getToken(); diff --git a/src/services/upload/fileService.ts b/src/services/upload/fileService.ts index 939e769e6..c3eb92fea 100644 --- a/src/services/upload/fileService.ts +++ b/src/services/upload/fileService.ts @@ -10,6 +10,7 @@ import { ParsedMetadataJSONMap, DataStream, ElectronFile, + MetadataEncryptionResult, } from 'types/upload'; import { splitFilenameAndExtension } from 'utils/file'; import { logError } from 'utils/sentry'; @@ -105,7 +106,7 @@ export async function encryptFile( const { file: encryptedThumbnail }: EncryptionResult = await worker.encryptThumbnail(file.thumbnail, fileKey); - const { file: encryptedMetadata }: EncryptionResult = + const { file: encryptedMetadata }: MetadataEncryptionResult = await worker.encryptMetadata(file.metadata, fileKey); const encryptedKey: B64EncryptionResult = await worker.encryptToB64( diff --git a/src/types/file/index.ts b/src/types/file/index.ts index 006d1cc42..34a45b881 100644 --- a/src/types/file/index.ts +++ b/src/types/file/index.ts @@ -1,9 +1,18 @@ import { MagicMetadataCore, VISIBILITY_STATE } from 'types/magicMetadata'; import { DataStream, Metadata } from 'types/upload'; -export interface fileAttribute { - encryptedData?: DataStream | Uint8Array; - objectKey?: string; +export interface FileAttribute { + encryptedData: DataStream | Uint8Array; + decryptionHeader: string; +} + +export interface B64FileAttribute { + encryptedData: string; + decryptionHeader: string; +} + +export interface S3FileAttribute { + objectKey: string; decryptionHeader: string; } @@ -36,8 +45,8 @@ export interface EnteFile { id: number; collectionID: number; ownerID: number; - file: fileAttribute; - thumbnail: fileAttribute; + file: S3FileAttribute; + thumbnail: S3FileAttribute; metadata: Metadata; info: EnteFileInfo; magicMetadata: FileMagicMetadata; diff --git a/src/types/upload/index.ts b/src/types/upload/index.ts index 871e03e0a..780e77dca 100644 --- a/src/types/upload/index.ts +++ b/src/types/upload/index.ts @@ -1,6 +1,6 @@ import { FILE_TYPE } from 'constants/file'; import { Collection } from 'types/collection'; -import { fileAttribute } from 'types/file'; +import { B64FileAttribute, FileAttribute, S3FileAttribute } from 'types/file'; export interface DataStream { stream: ReadableStream; @@ -12,7 +12,12 @@ export function isDataStream(object: any): object is DataStream { } export interface EncryptionResult { - file: fileAttribute; + file: FileAttribute; + key: string; +} + +export interface MetadataEncryptionResult { + file: B64FileAttribute; key: string; } @@ -126,12 +131,17 @@ export interface EncryptedFile { fileKey: B64EncryptionResult; } export interface ProcessedFile { - file: fileAttribute; - thumbnail: fileAttribute; - metadata: fileAttribute; + file: FileAttribute; + thumbnail: FileAttribute; + metadata: B64FileAttribute; localID: number; } -export interface BackupedFile extends Omit {} + +export interface BackupedFile { + file: S3FileAttribute; + thumbnail: S3FileAttribute; + metadata: B64FileAttribute; +} export interface UploadFile extends BackupedFile { collectionID: number; diff --git a/src/utils/file/index.ts b/src/utils/file/index.ts index 9c3ff6c54..210d79eaf 100644 --- a/src/utils/file/index.ts +++ b/src/utils/file/index.ts @@ -1,7 +1,7 @@ import { SelectedState } from 'types/gallery'; import { + B64FileAttribute, EnteFile, - fileAttribute, FileMagicMetadataProps, FilePublicMagicMetadataProps, } from 'types/file'; @@ -199,7 +199,7 @@ export async function decryptFile(file: EnteFile, collectionKey: string) { file.keyDecryptionNonce, collectionKey ); - const encryptedMetadata = file.metadata as unknown as fileAttribute; + const encryptedMetadata = file.metadata as unknown as B64FileAttribute; file.metadata = await worker.decryptMetadata( encryptedMetadata.encryptedData, encryptedMetadata.decryptionHeader,