From b4699ecfcba1eb195d1caa5bfa91162deade27e6 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Fri, 29 Mar 2024 22:26:50 +0530 Subject: [PATCH] Remove ElectronFile from cast --- .../cast/src/services/ffmpeg/ffmpegFactory.ts | 5 +- .../cast/src/services/ffmpeg/ffmpegService.ts | 3 +- web/apps/cast/src/services/readerService.ts | 47 +------------------ .../cast/src/services/typeDetectionService.ts | 32 +++---------- web/apps/cast/src/types/upload/index.ts | 23 --------- 5 files changed, 10 insertions(+), 100 deletions(-) diff --git a/web/apps/cast/src/services/ffmpeg/ffmpegFactory.ts b/web/apps/cast/src/services/ffmpeg/ffmpegFactory.ts index 15939891a5..0f7d226c89 100644 --- a/web/apps/cast/src/services/ffmpeg/ffmpegFactory.ts +++ b/web/apps/cast/src/services/ffmpeg/ffmpegFactory.ts @@ -1,13 +1,12 @@ -import { ElectronFile } from "types/upload"; import ComlinkFFmpegWorker from "utils/comlink/ComlinkFFmpegWorker"; export interface IFFmpeg { run: ( cmd: string[], - inputFile: File | ElectronFile, + inputFile: File, outputFilename: string, dontTimeout?: boolean, - ) => Promise; + ) => Promise; } class FFmpegFactory { diff --git a/web/apps/cast/src/services/ffmpeg/ffmpegService.ts b/web/apps/cast/src/services/ffmpeg/ffmpegService.ts index 85bab9939e..325f1d66b3 100644 --- a/web/apps/cast/src/services/ffmpeg/ffmpegService.ts +++ b/web/apps/cast/src/services/ffmpeg/ffmpegService.ts @@ -4,10 +4,9 @@ import { INPUT_PATH_PLACEHOLDER, OUTPUT_PATH_PLACEHOLDER, } from "constants/ffmpeg"; -import { ElectronFile } from "types/upload"; import ffmpegFactory from "./ffmpegFactory"; -export async function convertToMP4(file: File | ElectronFile) { +export async function convertToMP4(file: File) { try { const ffmpegClient = await ffmpegFactory.getFFmpegClient(); return await ffmpegClient.run( diff --git a/web/apps/cast/src/services/readerService.ts b/web/apps/cast/src/services/readerService.ts index 5aa42b6d27..7682f15802 100644 --- a/web/apps/cast/src/services/readerService.ts +++ b/web/apps/cast/src/services/readerService.ts @@ -1,10 +1,7 @@ import { logError } from "@ente/shared/sentry"; import { convertBytesToHumanReadable } from "@ente/shared/utils/size"; -import { ElectronFile } from "types/upload"; -export async function getUint8ArrayView( - file: Blob | ElectronFile, -): Promise { +export async function getUint8ArrayView(file: Blob): Promise { try { return new Uint8Array(await file.arrayBuffer()); } catch (e) { @@ -14,45 +11,3 @@ export async function getUint8ArrayView( throw e; } } - -export function getFileStream(file: File, chunkSize: number) { - const fileChunkReader = fileChunkReaderMaker(file, chunkSize); - - const stream = new ReadableStream({ - async pull(controller: ReadableStreamDefaultController) { - const chunk = await fileChunkReader.next(); - if (chunk.done) { - controller.close(); - } else { - controller.enqueue(chunk.value); - } - }, - }); - const chunkCount = Math.ceil(file.size / chunkSize); - return { - stream, - chunkCount, - }; -} - -export async function getElectronFileStream( - file: ElectronFile, - chunkSize: number, -) { - const chunkCount = Math.ceil(file.size / chunkSize); - return { - stream: await file.stream(), - chunkCount, - }; -} - -async function* fileChunkReaderMaker(file: File, chunkSize: number) { - let offset = 0; - while (offset < file.size) { - const blob = file.slice(offset, chunkSize + offset); - const fileChunk = await getUint8ArrayView(blob); - yield fileChunk; - offset += chunkSize; - } - return null; -} diff --git a/web/apps/cast/src/services/typeDetectionService.ts b/web/apps/cast/src/services/typeDetectionService.ts index c280baf510..826253ce47 100644 --- a/web/apps/cast/src/services/typeDetectionService.ts +++ b/web/apps/cast/src/services/typeDetectionService.ts @@ -6,37 +6,25 @@ import { KNOWN_NON_MEDIA_FORMATS, WHITELISTED_FILE_FORMATS, } from "constants/upload"; -import FileType, { FileTypeResult } from "file-type"; -import { ElectronFile, FileTypeInfo } from "types/upload"; +import FileType from "file-type"; +import { FileTypeInfo } from "types/upload"; import { getFileExtension } from "utils/file"; import { getUint8ArrayView } from "./readerService"; -function getFileSize(file: File | ElectronFile) { - return file.size; -} - const TYPE_VIDEO = "video"; const TYPE_IMAGE = "image"; const CHUNK_SIZE_FOR_TYPE_DETECTION = 4100; -export async function getFileType( - receivedFile: File | ElectronFile, -): Promise { +export async function getFileType(receivedFile: File): Promise { try { let fileType: FILE_TYPE; - let typeResult: FileTypeResult; - - if (receivedFile instanceof File) { - typeResult = await extractFileType(receivedFile); - } else { - typeResult = await extractElectronFileType(receivedFile); - } + const typeResult = await extractFileType(receivedFile); const mimTypeParts: string[] = typeResult.mime?.split("/"); - if (mimTypeParts?.length !== 2) { throw Error(CustomError.INVALID_MIME_TYPE(typeResult.mime)); } + switch (mimTypeParts[0]) { case TYPE_IMAGE: fileType = FILE_TYPE.IMAGE; @@ -54,7 +42,7 @@ export async function getFileType( }; } catch (e) { const fileFormat = getFileExtension(receivedFile.name); - const fileSize = convertBytesToHumanReadable(getFileSize(receivedFile)); + const fileSize = convertBytesToHumanReadable(receivedFile.size); const whiteListedFormat = WHITELISTED_FILE_FORMATS.find( (a) => a.exactType === fileFormat, ); @@ -85,14 +73,6 @@ async function extractFileType(file: File) { return getFileTypeFromBuffer(fileDataChunk); } -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); -} - async function getFileTypeFromBuffer(buffer: Uint8Array) { const result = await FileType.fromBuffer(buffer); if (!result?.mime) { diff --git a/web/apps/cast/src/types/upload/index.ts b/web/apps/cast/src/types/upload/index.ts index c4a05fc6dc..ef44b4a23f 100644 --- a/web/apps/cast/src/types/upload/index.ts +++ b/web/apps/cast/src/types/upload/index.ts @@ -46,29 +46,6 @@ export interface FileTypeInfo { videoType?: string; } -/* - * ElectronFile is a custom interface that is used to represent - * any file on disk as a File-like object in the Electron desktop app. - * - * This was added to support the auto-resuming of failed uploads - * which needed absolute paths to the files which the - * normal File interface does not provide. - */ -export interface ElectronFile { - name: string; - path: string; - size: number; - lastModified: number; - stream: () => Promise>; - blob: () => Promise; - arrayBuffer: () => Promise; -} - -export interface LivePhotoAssets { - image: globalThis.File | ElectronFile; - video: globalThis.File | ElectronFile; -} - export interface UploadURL { url: string; objectKey: string;