Restructure

This commit is contained in:
Manav Rathi 2024-04-20 12:16:58 +05:30
parent 6337ffc203
commit 14e4205feb
No known key found for this signature in database
2 changed files with 26 additions and 46 deletions

View File

@ -266,71 +266,53 @@ export function generateStreamFromArrayBuffer(data: Uint8Array) {
}); });
} }
export async function getRenderableImage(fileName: string, imageBlob: Blob) { export const getRenderableImage = async (fileName: string, imageBlob: Blob) => {
let fileTypeInfo: FileTypeInfo; let fileTypeInfo: FileTypeInfo;
try { try {
const tempFile = new File([imageBlob], fileName); const tempFile = new File([imageBlob], fileName);
fileTypeInfo = await getFileType(tempFile); fileTypeInfo = await getFileType(tempFile);
log.debug(() => `file type info: ${JSON.stringify(fileTypeInfo)}`); log.debug(() => `file type info: ${JSON.stringify(fileTypeInfo)}`);
const { exactType } = fileTypeInfo; const { exactType } = fileTypeInfo;
let convertedImageBlob: Blob;
if (isRawFile(exactType)) {
try {
if (!isSupportedRawFormat(exactType)) {
throw Error(CustomError.UNSUPPORTED_RAW_FORMAT);
}
if (!isElectron()) { if (!isRawFile(exactType)) {
throw new Error("not available on web"); // Not something we know how to handle yet, give back the original.
}
log.info(
`RawConverter called for ${fileName}-${convertBytesToHumanReadable(
imageBlob.size,
)}`,
);
convertedImageBlob = await convertToJPEGInElectron(
imageBlob,
fileName,
);
log.info(`${fileName} successfully converted`);
} catch (e) {
try {
if (!isFileHEIC(exactType)) {
throw e;
}
log.info(
`HEICConverter called for ${fileName}-${convertBytesToHumanReadable(
imageBlob.size,
)}`,
);
convertedImageBlob =
await heicConversionService.convert(imageBlob);
log.info(`${fileName} successfully converted`);
} catch (e) {
throw Error(CustomError.NON_PREVIEWABLE_FILE);
}
}
return convertedImageBlob;
} else {
return imageBlob; return imageBlob;
} }
} catch (e) {
log.error( let jpegBlob: Blob | undefined;
`Failed to get renderable image for ${JSON.stringify(fileTypeInfo)}`,
e, if (isElectron() && isSupportedRawFormat(exactType)) {
); // If we're running in our desktop app, see if our Node.js layer can
return null; // convert this into a JPEG for us.
} jpegBlob = await tryConvertToJPEGInElectron(imageBlob, fileName);
} }
const convertToJPEGInElectron = async ( if (!jpegBlob && isFileHEIC(exactType)) {
// If it is an HEIC file, use our web HEIC converter.
jpegBlob = await heicConversionService.convert(imageBlob);
}
return jpegBlob;
} catch (e) {
log.error(
`Failed to get renderable image for ${JSON.stringify(fileTypeInfo ?? fileName)}`,
e,
);
return undefined;
}
};
const tryConvertToJPEGInElectron = async (
fileBlob: Blob, fileBlob: Blob,
filename: string, filename: string,
): Promise<Blob> => { ): Promise<Blob | undefined> => {
try { try {
const startTime = Date.now(); const startTime = Date.now();
const inputFileData = new Uint8Array(await fileBlob.arrayBuffer()); const inputFileData = new Uint8Array(await fileBlob.arrayBuffer());
const electron = globalThis.electron; const electron = globalThis.electron;
// If we're running in a worker, we need to reroute the request back to
// the main thread since workers don't have access to the `window` (and
// thus, to the `window.electron`) object.
const convertedFileData = electron const convertedFileData = electron
? await electron.convertToJPEG(inputFileData, filename) ? await electron.convertToJPEG(inputFileData, filename)
: await workerBridge.convertToJPEG(inputFileData, filename); : await workerBridge.convertToJPEG(inputFileData, filename);

View File

@ -74,8 +74,6 @@ export const CustomError = {
EXIF_DATA_NOT_FOUND: "exif data not found", EXIF_DATA_NOT_FOUND: "exif data not found",
SELECT_FOLDER_ABORTED: "select folder aborted", SELECT_FOLDER_ABORTED: "select folder aborted",
NON_MEDIA_FILE: "non media file", NON_MEDIA_FILE: "non media file",
UNSUPPORTED_RAW_FORMAT: "unsupported raw format",
NON_PREVIEWABLE_FILE: "non previewable file",
PROCESSING_FAILED: "processing failed", PROCESSING_FAILED: "processing failed",
EXPORT_RECORD_JSON_PARSING_FAILED: "export record json parsing failed", EXPORT_RECORD_JSON_PARSING_FAILED: "export record json parsing failed",
TWO_FACTOR_ENABLED: "two factor enabled", TWO_FACTOR_ENABLED: "two factor enabled",