diff --git a/src/services/upload/readFileService.ts b/src/services/upload/readFileService.ts index 3342fd572..54e52dd07 100644 --- a/src/services/upload/readFileService.ts +++ b/src/services/upload/readFileService.ts @@ -151,3 +151,22 @@ export async function getUint8ArrayView( throw e; } } + +export function getFileTypeFromFileObject(file: globalThis.File) { + const typeParts = file.type.split('/'); + let fileType; + if (typeParts?.length !== 2) { + throw Error(CustomError.TYPE_DETECTION_FAILED); + } + switch (typeParts[0]) { + case TYPE_IMAGE: + fileType = FILE_TYPE.IMAGE; + break; + case TYPE_VIDEO: + fileType = FILE_TYPE.VIDEO; + break; + default: + fileType = FILE_TYPE.OTHERS; + } + return fileType; +} diff --git a/src/services/upload/uploadManager.ts b/src/services/upload/uploadManager.ts index 01c8c0716..98d84f605 100644 --- a/src/services/upload/uploadManager.ts +++ b/src/services/upload/uploadManager.ts @@ -36,7 +36,7 @@ export interface FileWithCollection { key?: string; isLivePhoto?: boolean; file?: globalThis.File; - livePhotoAsset?: [globalThis.File, globalThis.File]; + livePhotoAsset?: { image: globalThis.File; video: globalThis.File }; collection?: Collection; collectionID?: number; } diff --git a/src/services/upload/uploader.ts b/src/services/upload/uploader.ts index e740e3a74..60f2c23bc 100644 --- a/src/services/upload/uploader.ts +++ b/src/services/upload/uploader.ts @@ -68,21 +68,7 @@ export default async function uploader( }; let imageFile: globalThis.File; let videoFile: globalThis.File; - if ( - file1TypeInfo.fileType === FILE_TYPE.IMAGE && - file2TypeInfo.fileType === FILE_TYPE.VIDEO - ) { - imageFile = livePhotoAsset[0]; - videoFile = livePhotoAsset[1]; - } else if ( - file1TypeInfo.fileType === FILE_TYPE.VIDEO && - file2TypeInfo.fileType === FILE_TYPE.IMAGE - ) { - imageFile = livePhotoAsset[1]; - videoFile = livePhotoAsset[0]; - } else { - throw Error(CustomError.UNSUPPORTED_FILE_FORMAT); - } + const imageMetadata = await uploadService.getFileMetadata( imageFile, collection, diff --git a/src/utils/upload/index.ts b/src/utils/upload/index.ts index 1f8bb91e4..1db242d23 100644 --- a/src/utils/upload/index.ts +++ b/src/utils/upload/index.ts @@ -1,7 +1,8 @@ import { FileWithCollection } from 'services/upload/uploadManager'; import { MetadataObject } from 'services/upload/uploadService'; -import { File } from 'services/fileService'; +import { File, FILE_TYPE } from 'services/fileService'; import { splitFilenameAndExtension } from 'utils/file'; +import { getFileTypeFromFileObject } from 'services/upload/readFileService'; const TYPE_JSON = 'json'; export function fileAlreadyInCollection( @@ -63,15 +64,35 @@ export function segregateFiles( const collectionID = mediaFiles[i].collectionID; const file1 = mediaFile1.file; const file2 = mediaFile2.file; + const file1Type = getFileTypeFromFileObject(file1); + const file2Type = getFileTypeFromFileObject(file2); if ( - splitFilenameAndExtension(file1.name)[0] === - splitFilenameAndExtension(file2.name)[0] + file1Type !== FILE_TYPE.OTHERS && + file2Type !== FILE_TYPE.OTHERS ) { - livePhotoFiles.push({ - collectionID: collectionID, - isLivePhoto: true, - livePhotoAsset: [file1, file2], - }); + let imageFile; + let videoFile; + if ( + file1Type !== file2Type && + splitFilenameAndExtension(file1.name)[0] === + splitFilenameAndExtension(file2.name)[0] + ) { + if ( + file1Type === FILE_TYPE.IMAGE && + file2Type === FILE_TYPE.VIDEO + ) { + imageFile = file1; + videoFile = file2; + } else { + imageFile = file2; + videoFile = file1; + } + livePhotoFiles.push({ + collectionID: collectionID, + isLivePhoto: true, + livePhotoAsset: { image: imageFile, video: videoFile }, + }); + } } } else { normalFiles.push(mediaFile1);