|
@@ -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) {
|