From 80db61133a162db750b98eaba824071247aa2d22 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 15 Feb 2022 09:20:41 +0530 Subject: [PATCH] refactored code to not create stream for livePhotodata assets as they zipping library doesn't support streams --- src/constants/upload/index.ts | 2 + src/services/upload/livePhotoService.ts | 64 +++++++++++++++---------- src/utils/error/index.ts | 1 + 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/constants/upload/index.ts b/src/constants/upload/index.ts index 19432605f..2109e566b 100644 --- a/src/constants/upload/index.ts +++ b/src/constants/upload/index.ts @@ -41,3 +41,5 @@ export enum FileUploadResults { } export const MAX_FILE_SIZE_SUPPORTED = 5 * 1024 * 1024 * 1024; // 5 GB + +export const LIVE_PHOTO_ASSET_SIZE_LIMIT = 20 * 1024 * 1024; // 20MB diff --git a/src/services/upload/livePhotoService.ts b/src/services/upload/livePhotoService.ts index 81b6ad557..eac3e2a79 100644 --- a/src/services/upload/livePhotoService.ts +++ b/src/services/upload/livePhotoService.ts @@ -1,16 +1,17 @@ import { FILE_TYPE } from 'constants/file'; -import { MULTIPART_PART_SIZE } from 'constants/upload'; +import { LIVE_PHOTO_ASSET_SIZE_LIMIT } from 'constants/upload'; import { encodeMotionPhoto } from 'services/motionPhotoService'; import { FileTypeInfo, FileWithCollection, - isDataStream, LivePhotoAssets, Metadata, } from 'types/upload'; +import { CustomError } from 'utils/error'; import { splitFilenameAndExtension } from 'utils/file'; -import { readFile } from './fileService'; -import { getFileData } from './readFileService'; +import { logError } from 'utils/sentry'; +import { getUint8ArrayView } from './readFileService'; +import { generateThumbnail } from './thumbnailService'; import uploadService from './uploadService'; import UploadService from './uploadService'; @@ -53,31 +54,26 @@ export async function readLivePhoto( fileTypeInfo: FileTypeInfo, livePhotoAssets: LivePhotoAssets ) { - const image = await readFile( + const { thumbnail, hasStaticThumbnail } = await generateThumbnail( worker, reader, - { exactType: fileTypeInfo.exactType, fileType: FILE_TYPE.IMAGE }, - livePhotoAssets.image + livePhotoAssets.image, + { exactType: fileTypeInfo.exactType, fileType: FILE_TYPE.IMAGE } ); - const video = await getFileData(reader, livePhotoAssets.video); + const image = await getUint8ArrayView(reader, livePhotoAssets.image); + + const video = await getUint8ArrayView(reader, livePhotoAssets.video); - /* - did it based on the assumption that live photo assets ideally would not be larger than MULTIPART_PART_SIZE and hence not require to be streamed - also, allowing that would require a small amount of code changes as the zipping library doesn't support stream as a input - */ - if (isDataStream(video) || isDataStream(image.filedata)) { - throw new Error('too large live photo assets'); - } return { filedata: await encodeMotionPhoto({ - image: image.filedata as Uint8Array, - video: video as Uint8Array, + image, + video, imageNameTitle: livePhotoAssets.image.name, videoNameTitle: livePhotoAssets.video.name, }), - thumbnail: image.thumbnail, - hasStaticThumbnail: image.hasStaticThumbnail, + thumbnail, + hasStaticThumbnail, }; } @@ -171,15 +167,35 @@ function areFilesLivePhotoAssets( firstFileIdentifier: LivePhotoIdentifier, secondFileIdentifier: LivePhotoIdentifier ) { - return ( + if ( firstFileIdentifier.collectionID === secondFileIdentifier.collectionID && firstFileIdentifier.fileType !== secondFileIdentifier.fileType && firstFileIdentifier.fileType !== FILE_TYPE.OTHERS && secondFileIdentifier.fileType !== FILE_TYPE.OTHERS && splitFilenameAndExtension(firstFileIdentifier.name)[0] === - splitFilenameAndExtension(secondFileIdentifier.name)[0] && - firstFileIdentifier.size <= MULTIPART_PART_SIZE && // so that they are small enough to be read and uploaded in single chunk - secondFileIdentifier.size <= MULTIPART_PART_SIZE - ); + splitFilenameAndExtension(secondFileIdentifier.name)[0] + ) { + // checks size of live Photo assets are less than allowed limit + // I did that based on the assumption that live photo assets ideally would not be larger than LIVE_PHOTO_ASSET_SIZE_LIMIT + // also zipping library doesn't support stream as a input + if ( + firstFileIdentifier.size <= LIVE_PHOTO_ASSET_SIZE_LIMIT && + secondFileIdentifier.size <= LIVE_PHOTO_ASSET_SIZE_LIMIT + ) { + return true; + } else { + logError( + new Error(CustomError.TOO_LARGE_LIVE_PHOTO_ASSETS), + CustomError.TOO_LARGE_LIVE_PHOTO_ASSETS, + { + fileSizes: [ + firstFileIdentifier.size, + secondFileIdentifier.size, + ], + } + ); + } + } + return false; } diff --git a/src/utils/error/index.ts b/src/utils/error/index.ts index 9680c7274..57fde1701 100644 --- a/src/utils/error/index.ts +++ b/src/utils/error/index.ts @@ -39,6 +39,7 @@ export enum CustomError { SUBSCRIPTION_NEEDED = 'subscription not present', NOT_FOUND = 'not found ', NO_METADATA = 'no metadata', + TOO_LARGE_LIVE_PHOTO_ASSETS = 'too large live photo assets', } function parseUploadErrorCodes(error) {