From 476d8de72a3dea8a2327b6fbc3b64c2dfe136e7b Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Thu, 25 Apr 2024 10:16:16 +0530 Subject: [PATCH] Wrap --- .../src/services/upload/uploadService.ts | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/web/apps/photos/src/services/upload/uploadService.ts b/web/apps/photos/src/services/upload/uploadService.ts index aa6b50bc3..bca030fc6 100644 --- a/web/apps/photos/src/services/upload/uploadService.ts +++ b/web/apps/photos/src/services/upload/uploadService.ts @@ -5,6 +5,7 @@ import { basename } from "@/next/file"; import log from "@/next/log"; import { ElectronFile } from "@/next/types/file"; import { CustomErrorMessage } from "@/next/types/ipc"; +import { ensure } from "@/utils/ensure"; import { DedicatedCryptoWorker } from "@ente/shared/crypto/internal/crypto.worker"; import { EncryptionResult } from "@ente/shared/crypto/types"; import { CustomError, handleUploadError } from "@ente/shared/error"; @@ -35,7 +36,6 @@ import { UploadFile, UploadURL, type FileWithCollection2, - type LivePhotoAssets, type LivePhotoAssets2, type Metadata, type UploadAsset2, @@ -47,7 +47,7 @@ import { import { readStream } from "utils/native-stream"; import { hasFileHash } from "utils/upload"; import * as convert from "xml-js"; -import { detectFileTypeInfo } from "../detect-type"; +import { detectFileTypeInfoFromChunk } from "../detect-type"; import { getFileStream } from "../readerService"; import { extractAssetMetadata } from "./metadata"; import publicUploadHttpClient from "./publicUploadHttpClient"; @@ -416,20 +416,20 @@ const readFileTypeInfoAndSize = async ( ): Promise<{ fileTypeInfo: FileTypeInfo; fileSize: number }> => { const { dataOrStream, fileSize } = await readFileOrPath(fileOrPath); - function getFileSize(file: File | ElectronFile) { - return file.size; - } + const fileTypeInfo = await detectFileTypeInfoFromChunk(async () => { + if (dataOrStream instanceof Uint8Array) { + return dataOrStream; + } else { + const reader = dataOrStream.stream.getReader(); + const chunk = ensure((await reader.read()).value); + await reader.cancel(); + return chunk; + } + }, getFileName(fileOrPath)); - async function extractElectronFileType(file: ElectronFile) { - const stream = await file.stream(); - const reader = stream.getReader(); - const { value: fileDataChunk } = await reader.read(); - await reader.cancel(); - return getFileTypeFromBuffer(fileDataChunk); - } - - fileSize = getAssetSize(uploadAsset); - fileTypeInfo = await getAssetFileType(uploadAsset); + return { fileTypeInfo, fileSize }; +}; +/* const getAssetSize = ({ isLivePhoto, file, @@ -471,7 +471,7 @@ const readFileTypeInfoAndSize = async ( }; }; }; - +*/ const readAsset = async ( fileTypeInfo: FileTypeInfo, { isLivePhoto, file, livePhotoAssets }: UploadAsset2,