Explorar o código

Remove ElectronFile from cast

Manav Rathi hai 1 ano
pai
achega
b4699ecfcb

+ 2 - 3
web/apps/cast/src/services/ffmpeg/ffmpegFactory.ts

@@ -1,13 +1,12 @@
-import { ElectronFile } from "types/upload";
 import ComlinkFFmpegWorker from "utils/comlink/ComlinkFFmpegWorker";
 import ComlinkFFmpegWorker from "utils/comlink/ComlinkFFmpegWorker";
 
 
 export interface IFFmpeg {
 export interface IFFmpeg {
     run: (
     run: (
         cmd: string[],
         cmd: string[],
-        inputFile: File | ElectronFile,
+        inputFile: File,
         outputFilename: string,
         outputFilename: string,
         dontTimeout?: boolean,
         dontTimeout?: boolean,
-    ) => Promise<File | ElectronFile>;
+    ) => Promise<File>;
 }
 }
 
 
 class FFmpegFactory {
 class FFmpegFactory {

+ 1 - 2
web/apps/cast/src/services/ffmpeg/ffmpegService.ts

@@ -4,10 +4,9 @@ import {
     INPUT_PATH_PLACEHOLDER,
     INPUT_PATH_PLACEHOLDER,
     OUTPUT_PATH_PLACEHOLDER,
     OUTPUT_PATH_PLACEHOLDER,
 } from "constants/ffmpeg";
 } from "constants/ffmpeg";
-import { ElectronFile } from "types/upload";
 import ffmpegFactory from "./ffmpegFactory";
 import ffmpegFactory from "./ffmpegFactory";
 
 
-export async function convertToMP4(file: File | ElectronFile) {
+export async function convertToMP4(file: File) {
     try {
     try {
         const ffmpegClient = await ffmpegFactory.getFFmpegClient();
         const ffmpegClient = await ffmpegFactory.getFFmpegClient();
         return await ffmpegClient.run(
         return await ffmpegClient.run(

+ 1 - 46
web/apps/cast/src/services/readerService.ts

@@ -1,10 +1,7 @@
 import { logError } from "@ente/shared/sentry";
 import { logError } from "@ente/shared/sentry";
 import { convertBytesToHumanReadable } from "@ente/shared/utils/size";
 import { convertBytesToHumanReadable } from "@ente/shared/utils/size";
-import { ElectronFile } from "types/upload";
 
 
-export async function getUint8ArrayView(
-    file: Blob | ElectronFile,
-): Promise<Uint8Array> {
+export async function getUint8ArrayView(file: Blob): Promise<Uint8Array> {
     try {
     try {
         return new Uint8Array(await file.arrayBuffer());
         return new Uint8Array(await file.arrayBuffer());
     } catch (e) {
     } catch (e) {
@@ -14,45 +11,3 @@ export async function getUint8ArrayView(
         throw e;
         throw e;
     }
     }
 }
 }
-
-export function getFileStream(file: File, chunkSize: number) {
-    const fileChunkReader = fileChunkReaderMaker(file, chunkSize);
-
-    const stream = new ReadableStream<Uint8Array>({
-        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;
-}

+ 6 - 26
web/apps/cast/src/services/typeDetectionService.ts

@@ -6,37 +6,25 @@ import {
     KNOWN_NON_MEDIA_FORMATS,
     KNOWN_NON_MEDIA_FORMATS,
     WHITELISTED_FILE_FORMATS,
     WHITELISTED_FILE_FORMATS,
 } from "constants/upload";
 } 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 { getFileExtension } from "utils/file";
 import { getUint8ArrayView } from "./readerService";
 import { getUint8ArrayView } from "./readerService";
 
 
-function getFileSize(file: File | ElectronFile) {
-    return file.size;
-}
-
 const TYPE_VIDEO = "video";
 const TYPE_VIDEO = "video";
 const TYPE_IMAGE = "image";
 const TYPE_IMAGE = "image";
 const CHUNK_SIZE_FOR_TYPE_DETECTION = 4100;
 const CHUNK_SIZE_FOR_TYPE_DETECTION = 4100;
 
 
-export async function getFileType(
-    receivedFile: File | ElectronFile,
-): Promise<FileTypeInfo> {
+export async function getFileType(receivedFile: File): Promise<FileTypeInfo> {
     try {
     try {
         let fileType: FILE_TYPE;
         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("/");
         const mimTypeParts: string[] = typeResult.mime?.split("/");
-
         if (mimTypeParts?.length !== 2) {
         if (mimTypeParts?.length !== 2) {
             throw Error(CustomError.INVALID_MIME_TYPE(typeResult.mime));
             throw Error(CustomError.INVALID_MIME_TYPE(typeResult.mime));
         }
         }
+
         switch (mimTypeParts[0]) {
         switch (mimTypeParts[0]) {
             case TYPE_IMAGE:
             case TYPE_IMAGE:
                 fileType = FILE_TYPE.IMAGE;
                 fileType = FILE_TYPE.IMAGE;
@@ -54,7 +42,7 @@ export async function getFileType(
         };
         };
     } catch (e) {
     } catch (e) {
         const fileFormat = getFileExtension(receivedFile.name);
         const fileFormat = getFileExtension(receivedFile.name);
-        const fileSize = convertBytesToHumanReadable(getFileSize(receivedFile));
+        const fileSize = convertBytesToHumanReadable(receivedFile.size);
         const whiteListedFormat = WHITELISTED_FILE_FORMATS.find(
         const whiteListedFormat = WHITELISTED_FILE_FORMATS.find(
             (a) => a.exactType === fileFormat,
             (a) => a.exactType === fileFormat,
         );
         );
@@ -85,14 +73,6 @@ async function extractFileType(file: File) {
     return getFileTypeFromBuffer(fileDataChunk);
     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) {
 async function getFileTypeFromBuffer(buffer: Uint8Array) {
     const result = await FileType.fromBuffer(buffer);
     const result = await FileType.fromBuffer(buffer);
     if (!result?.mime) {
     if (!result?.mime) {

+ 0 - 23
web/apps/cast/src/types/upload/index.ts

@@ -46,29 +46,6 @@ export interface FileTypeInfo {
     videoType?: string;
     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<ReadableStream<Uint8Array>>;
-    blob: () => Promise<Blob>;
-    arrayBuffer: () => Promise<Uint8Array>;
-}
-
-export interface LivePhotoAssets {
-    image: globalThis.File | ElectronFile;
-    video: globalThis.File | ElectronFile;
-}
-
 export interface UploadURL {
 export interface UploadURL {
     url: string;
     url: string;
     objectKey: string;
     objectKey: string;