Remove unnecessary flexibility
This commit is contained in:
parent
dfa50e8ed1
commit
3ab14d5949
5 changed files with 20 additions and 31 deletions
|
@ -38,9 +38,9 @@ import {
|
|||
updateAndRestart,
|
||||
updateOnNextRestart,
|
||||
} from "./services/app-update";
|
||||
import { convertToJPEG, generateImageThumbnail } from "./services/image";
|
||||
import { ffmpegExec } from "./services/ffmpeg";
|
||||
import { getDirFiles } from "./services/fs";
|
||||
import { convertToJPEG, generateImageThumbnail } from "./services/image";
|
||||
import {
|
||||
clipImageEmbedding,
|
||||
clipTextEmbeddingIfAvailable,
|
||||
|
@ -147,12 +147,8 @@ export const attachIPCHandlers = () => {
|
|||
|
||||
ipcMain.handle(
|
||||
"generateImageThumbnail",
|
||||
(
|
||||
_,
|
||||
dataOrPath: Uint8Array | string,
|
||||
maxDimension: number,
|
||||
maxSize: number,
|
||||
) => generateImageThumbnail(dataOrPath, maxDimension, maxSize),
|
||||
(_, imageData: Uint8Array, maxDimension: number, maxSize: number) =>
|
||||
generateImageThumbnail(imageData, maxDimension, maxSize),
|
||||
);
|
||||
|
||||
ipcMain.handle(
|
||||
|
|
|
@ -20,8 +20,8 @@ export const convertToJPEG = async (imageData: Uint8Array) => {
|
|||
return new Uint8Array(await fs.readFile(outputFilePath));
|
||||
} finally {
|
||||
try {
|
||||
deleteTempFile(inputFilePath);
|
||||
deleteTempFile(outputFilePath);
|
||||
await deleteTempFile(inputFilePath);
|
||||
await deleteTempFile(outputFilePath);
|
||||
} catch (e) {
|
||||
log.error("Ignoring error when cleaning up temp files", e);
|
||||
}
|
||||
|
@ -63,20 +63,11 @@ const imageMagickPath = () =>
|
|||
path.join(isDev ? "build" : process.resourcesPath, "image-magick");
|
||||
|
||||
export const generateImageThumbnail = async (
|
||||
dataOrPath: Uint8Array | string,
|
||||
imageData: Uint8Array,
|
||||
maxDimension: number,
|
||||
maxSize: number,
|
||||
): Promise<Uint8Array> => {
|
||||
let inputFilePath: string;
|
||||
let isInputFileTemporary: boolean;
|
||||
if (typeof dataOrPath == "string") {
|
||||
inputFilePath = dataOrPath;
|
||||
isInputFileTemporary = false;
|
||||
} else {
|
||||
inputFilePath = await makeTempFilePath();
|
||||
isInputFileTemporary = true;
|
||||
}
|
||||
|
||||
const inputFilePath = await makeTempFilePath();
|
||||
const outputFilePath = await makeTempFilePath(".jpeg");
|
||||
|
||||
// Construct the command first, it may throw NotAvailable on win32.
|
||||
|
@ -89,8 +80,8 @@ export const generateImageThumbnail = async (
|
|||
);
|
||||
|
||||
try {
|
||||
if (dataOrPath instanceof Uint8Array)
|
||||
await fs.writeFile(inputFilePath, dataOrPath);
|
||||
if (imageData instanceof Uint8Array)
|
||||
await fs.writeFile(inputFilePath, imageData);
|
||||
|
||||
let thumbnail: Uint8Array;
|
||||
do {
|
||||
|
@ -107,8 +98,8 @@ export const generateImageThumbnail = async (
|
|||
return thumbnail;
|
||||
} finally {
|
||||
try {
|
||||
if (isInputFileTemporary) await deleteTempFile(inputFilePath);
|
||||
deleteTempFile(outputFilePath);
|
||||
await deleteTempFile(inputFilePath);
|
||||
await deleteTempFile(outputFilePath);
|
||||
} catch (e) {
|
||||
log.error("Ignoring error when cleaning up temp files", e);
|
||||
}
|
||||
|
|
|
@ -128,13 +128,13 @@ const convertToJPEG = (imageData: Uint8Array): Promise<Uint8Array> =>
|
|||
ipcRenderer.invoke("convertToJPEG", imageData);
|
||||
|
||||
const generateImageThumbnail = (
|
||||
dataOrPath: Uint8Array | string,
|
||||
imageData: Uint8Array,
|
||||
maxDimension: number,
|
||||
maxSize: number,
|
||||
): Promise<Uint8Array> =>
|
||||
ipcRenderer.invoke(
|
||||
"generateImageThumbnail",
|
||||
dataOrPath,
|
||||
imageData,
|
||||
maxDimension,
|
||||
maxSize,
|
||||
);
|
||||
|
|
|
@ -84,7 +84,7 @@ const generateImageThumbnail = async (
|
|||
const available = !moduleState.isNativeThumbnailCreationNotAvailable;
|
||||
if (electron && available) {
|
||||
try {
|
||||
return await generateImageThumbnailInElectron(electron, file);
|
||||
return await generateImageThumbnailInElectron(electron, blob);
|
||||
} catch (e) {
|
||||
if (e.message == CustomErrorMessage.NotAvailable) {
|
||||
moduleState.isNativeThumbnailCreationNotAvailable = true;
|
||||
|
@ -102,8 +102,9 @@ const generateImageThumbnailInElectron = async (
|
|||
blob: Blob,
|
||||
): Promise<Uint8Array> => {
|
||||
const startTime = Date.now();
|
||||
const data = new Uint8Array(await blob.arrayBuffer());
|
||||
const jpegData = await electron.generateImageThumbnail(
|
||||
inputFile,
|
||||
data,
|
||||
maxThumbnailDimension,
|
||||
maxThumbnailSize,
|
||||
);
|
||||
|
|
|
@ -205,6 +205,7 @@ export interface Electron {
|
|||
* {@link CustomErrorMessage.NotAvailable} message.
|
||||
*
|
||||
* @param imageData The raw image data (the contents of the image file).
|
||||
*
|
||||
* @returns JPEG data of the converted image.
|
||||
*/
|
||||
convertToJPEG: (imageData: Uint8Array) => Promise<Uint8Array>;
|
||||
|
@ -220,8 +221,8 @@ export interface Electron {
|
|||
* not yet possible, this function will throw an error with the
|
||||
* {@link CustomErrorMessage.NotAvailable} message.
|
||||
*
|
||||
* @param dataOrPath The data-of or path-to the image whose thumbnail we
|
||||
* want.
|
||||
* @param imageData The raw image data (the contents of the image file)
|
||||
* whose thumbnail we want to generate.
|
||||
* @param maxDimension The maximum width or height of the generated
|
||||
* thumbnail.
|
||||
* @param maxSize Maximum size (in bytes) of the generated thumbnail.
|
||||
|
@ -229,7 +230,7 @@ export interface Electron {
|
|||
* @returns JPEG data of the generated thumbnail.
|
||||
*/
|
||||
generateImageThumbnail: (
|
||||
dataOrPath: Uint8Array | string,
|
||||
imageData: Uint8Array,
|
||||
maxDimension: number,
|
||||
maxSize: number,
|
||||
) => Promise<Uint8Array>;
|
||||
|
|
Loading…
Add table
Reference in a new issue