diff --git a/src/services/upload/uploadManager.ts b/src/services/upload/uploadManager.ts index 8fbc6fd36..62e9ce338 100644 --- a/src/services/upload/uploadManager.ts +++ b/src/services/upload/uploadManager.ts @@ -21,6 +21,7 @@ import { CustomError } from 'utils/error'; import { Collection } from 'types/collection'; import { EnteFile } from 'types/file'; import { + ElectronFile, FileWithCollection, MetadataAndFileTypeInfo, MetadataAndFileTypeInfoMap, @@ -227,50 +228,20 @@ class UploadManager { logUploadInfo(`extractMetadataFromFiles executed`); UIService.reset(mediaFiles.length); for (const { file, localID, collectionID } of mediaFiles) { + let fileTypeInfo = null; + let metadata = null; try { - const { fileTypeInfo, metadata } = await (async () => { - if (file.size >= MAX_FILE_SIZE_SUPPORTED) { - logUploadInfo( - `${getFileNameSize( - file - )} rejected because of large size` - ); - - return { fileTypeInfo: null, metadata: null }; - } - const fileTypeInfo = await UploadService.getFileType( - file - ); - if (fileTypeInfo.fileType === FILE_TYPE.OTHERS) { - logUploadInfo( - `${getFileNameSize( - file - )} rejected because of unknown file format` - ); - return { fileTypeInfo, metadata: null }; - } - logUploadInfo( - ` extracting ${getFileNameSize(file)} metadata` - ); - const metadata = - (await UploadService.extractFileMetadata( - file, - collectionID, - fileTypeInfo - )) || null; - return { fileTypeInfo, metadata }; - })(); - + const result = await this.extractFileTypeAndMetadata( + file, + collectionID + ); + fileTypeInfo = result.fileTypeInfo; + metadata = result.metadata; logUploadInfo( `metadata extraction successful${getFileNameSize( file )} ` ); - this.metadataAndFileTypeInfoMap.set(localID, { - fileTypeInfo: fileTypeInfo && { ...fileTypeInfo }, - metadata: metadata && { ...metadata }, - }); - UIService.increaseFileUploaded(); } catch (e) { logError(e, 'metadata extraction failed for a file'); logUploadInfo( @@ -279,6 +250,11 @@ class UploadManager { )} error: ${e.message}` ); } + this.metadataAndFileTypeInfoMap.set(localID, { + fileTypeInfo: fileTypeInfo && { ...fileTypeInfo }, + metadata: metadata && { ...metadata }, + }); + UIService.increaseFileUploaded(); } } catch (e) { logError(e, 'error extracting metadata'); @@ -286,6 +262,36 @@ class UploadManager { } } + private async extractFileTypeAndMetadata( + file: File | ElectronFile, + collectionID: number + ) { + if (file.size >= MAX_FILE_SIZE_SUPPORTED) { + logUploadInfo( + `${getFileNameSize(file)} rejected because of large size` + ); + + return { fileTypeInfo: null, metadata: null }; + } + const fileTypeInfo = await UploadService.getFileType(file); + if (fileTypeInfo.fileType === FILE_TYPE.OTHERS) { + logUploadInfo( + `${getFileNameSize( + file + )} rejected because of unknown file format` + ); + return { fileTypeInfo, metadata: null }; + } + logUploadInfo(` extracting ${getFileNameSize(file)} metadata`); + const metadata = + (await UploadService.extractFileMetadata( + file, + collectionID, + fileTypeInfo + )) || null; + return { fileTypeInfo, metadata }; + } + private async uploadMediaFiles(mediaFiles: FileWithCollection[]) { logUploadInfo(`uploadMediaFiles called`); this.filesToBeUploaded.push(...mediaFiles);