|
@@ -1,14 +1,18 @@
|
|
import { exec } from "child_process";
|
|
import { exec } from "child_process";
|
|
-import util from "util";
|
|
|
|
-
|
|
|
|
import log from "electron-log";
|
|
import log from "electron-log";
|
|
|
|
+import { existsSync } from "fs";
|
|
import * as fs from "node:fs/promises";
|
|
import * as fs from "node:fs/promises";
|
|
import path from "path";
|
|
import path from "path";
|
|
|
|
+import util from "util";
|
|
import { CustomErrors } from "../constants/errors";
|
|
import { CustomErrors } from "../constants/errors";
|
|
import { isDev } from "../main/general";
|
|
import { isDev } from "../main/general";
|
|
|
|
+import { logError, logErrorSentry } from "../main/log";
|
|
|
|
+import { writeStream } from "../services/fs";
|
|
|
|
+import { ElectronFile } from "../types";
|
|
import { isPlatform } from "../utils/common/platform";
|
|
import { isPlatform } from "../utils/common/platform";
|
|
import { generateTempFilePath } from "../utils/temp";
|
|
import { generateTempFilePath } from "../utils/temp";
|
|
-import { logErrorSentry } from "../main/log";
|
|
|
|
|
|
+import { deleteTempFile } from "./ffmpeg";
|
|
|
|
+
|
|
const shellescape = require("any-shell-escape");
|
|
const shellescape = require("any-shell-escape");
|
|
|
|
|
|
const asyncExec = util.promisify(exec);
|
|
const asyncExec = util.promisify(exec);
|
|
@@ -80,6 +84,17 @@ function getImageMagickStaticPath() {
|
|
export async function convertToJPEG(
|
|
export async function convertToJPEG(
|
|
fileData: Uint8Array,
|
|
fileData: Uint8Array,
|
|
filename: string,
|
|
filename: string,
|
|
|
|
+): Promise<Uint8Array> {
|
|
|
|
+ if (isPlatform("windows")) {
|
|
|
|
+ throw Error(CustomErrors.WINDOWS_NATIVE_IMAGE_PROCESSING_NOT_SUPPORTED);
|
|
|
|
+ }
|
|
|
|
+ const convertedFileData = await convertToJPEG_(fileData, filename);
|
|
|
|
+ return convertedFileData;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+async function convertToJPEG_(
|
|
|
|
+ fileData: Uint8Array,
|
|
|
|
+ filename: string,
|
|
): Promise<Uint8Array> {
|
|
): Promise<Uint8Array> {
|
|
let tempInputFilePath: string;
|
|
let tempInputFilePath: string;
|
|
let tempOutputFilePath: string;
|
|
let tempOutputFilePath: string;
|
|
@@ -159,6 +174,44 @@ function constructConvertCommand(
|
|
}
|
|
}
|
|
|
|
|
|
export async function generateImageThumbnail(
|
|
export async function generateImageThumbnail(
|
|
|
|
+ inputFile: File | ElectronFile,
|
|
|
|
+ maxDimension: number,
|
|
|
|
+ maxSize: number,
|
|
|
|
+): Promise<Uint8Array> {
|
|
|
|
+ let inputFilePath = null;
|
|
|
|
+ let createdTempInputFile = null;
|
|
|
|
+ try {
|
|
|
|
+ if (isPlatform("windows")) {
|
|
|
|
+ throw Error(
|
|
|
|
+ CustomErrors.WINDOWS_NATIVE_IMAGE_PROCESSING_NOT_SUPPORTED,
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ if (!existsSync(inputFile.path)) {
|
|
|
|
+ const tempFilePath = await generateTempFilePath(inputFile.name);
|
|
|
|
+ await writeStream(tempFilePath, await inputFile.stream());
|
|
|
|
+ inputFilePath = tempFilePath;
|
|
|
|
+ createdTempInputFile = true;
|
|
|
|
+ } else {
|
|
|
|
+ inputFilePath = inputFile.path;
|
|
|
|
+ }
|
|
|
|
+ const thumbnail = await generateImageThumbnail_(
|
|
|
|
+ inputFilePath,
|
|
|
|
+ maxDimension,
|
|
|
|
+ maxSize,
|
|
|
|
+ );
|
|
|
|
+ return thumbnail;
|
|
|
|
+ } finally {
|
|
|
|
+ if (createdTempInputFile) {
|
|
|
|
+ try {
|
|
|
|
+ await deleteTempFile(inputFilePath);
|
|
|
|
+ } catch (e) {
|
|
|
|
+ logError(e, "failed to deleteTempFile");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+async function generateImageThumbnail_(
|
|
inputFilePath: string,
|
|
inputFilePath: string,
|
|
width: number,
|
|
width: number,
|
|
maxSize: number,
|
|
maxSize: number,
|