migrate to vips

This commit is contained in:
C4illin 2024-05-25 15:52:04 +02:00
parent d6b38c6866
commit d81a3a6ab4
11 changed files with 277 additions and 59 deletions

View file

@ -809,8 +809,7 @@ export async function convert(
return exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
return error;
}
if (stdout) {

View file

@ -320,9 +320,9 @@ export function convert(
`gm convert "${filePath}" "${targetPath}"`,
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
return error;
}
if (stdout) {
console.log(`stdout: ${stdout}`);
}

View file

@ -1,23 +1,22 @@
import {
properties as propertiesImage,
convert as convertImage,
} from "./sharp";
import { convert as convertImage, properties as propertiesImage } from "./vips";
import {
properties as propertiesPandoc,
convert as convertPandoc,
properties as propertiesPandoc,
} from "./pandoc";
import {
properties as propertiesFFmpeg,
convert as convertFFmpeg,
properties as propertiesFFmpeg,
} from "./ffmpeg";
import {
properties as propertiesGraphicsmagick,
convert as convertGraphicsmagick,
properties as propertiesGraphicsmagick,
} from "./graphicsmagick";
import { normalizeFiletype } from "../helpers/normalizeFiletype";
// This should probably be reconstructed so that the functions are not imported instead the functions hook into this to make the converters more modular
const properties: {
@ -38,8 +37,7 @@ const properties: {
converter: (
filePath: string,
fileType: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
convertTo: any,
convertTo: string,
targetPath: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
options?: any,
@ -47,7 +45,7 @@ const properties: {
) => any;
};
} = {
sharp: {
vips: {
properties: propertiesImage,
converter: convertImage,
},
@ -65,8 +63,6 @@ const properties: {
},
};
import { normalizeFiletype } from "../helpers/normalizeFiletype";
export async function mainConverter(
inputFilePath: string,
fileTypeOriginal: string,
@ -112,7 +108,7 @@ export async function mainConverter(
console.log(
`No available converter supports converting from ${fileType} to ${convertTo}.`,
);
return;
return "File type not supported"
}
try {
@ -123,14 +119,17 @@ export async function mainConverter(
targetPath,
options,
);
console.log(
`Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully using ${converterName}.`,
);
return "Done"
} catch (error) {
console.error(
`Failed to convert ${inputFilePath} from ${fileType} to ${convertTo} using ${converterName}.`,
error,
);
return "Failed, check logs"
}
}
@ -256,4 +255,4 @@ export const getAllInputs = (converter: string) => {
// }
// // print the number of unique Inputs and Outputs
// console.log(`Unique Formats: ${uniqueFormats.size}`);
// console.log(`Unique Formats: ${uniqueFormats.size}`);

View file

@ -131,11 +131,16 @@ export function convert(
`pandoc "${filePath}" -f ${fileType} -t ${convertTo} -o "${targetPath}"`,
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
return error;
}
if (stdout) {
console.log(`stdout: ${stdout}`);
}
if (stderr) {
console.error(`stderr: ${stderr}`);
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
},
);
}

View file

@ -3,8 +3,82 @@ import type { FormatEnum } from "sharp";
// declare possible conversions
export const properties = {
from: { images: ["jpeg", "png", "webp", "gif", "avif", "tiff", "svg"] },
to: { images: ["jpeg", "png", "webp", "gif", "avif", "tiff"] },
from: {
images: [
"avif",
"bif",
"csv",
"exr",
"fits",
"gif",
"hdr.gz",
"hdr",
"heic",
"heif",
"img.gz",
"img",
"j2c",
"j2k",
"jp2",
"jpeg",
"jpx",
"jxl",
"mat",
"mrxs",
"ndpi",
"nia.gz",
"nia",
"nii.gz",
"nii",
"pdf",
"pfm",
"pgm",
"pic",
"png",
"ppm",
"raw",
"scn",
"svg",
"svs",
"svslide",
"szi",
"tif",
"tiff",
"v",
"vips",
"vms",
"vmu",
"webp",
"zip",
],
},
to: {
images: [
"avif",
"dzi",
"fits",
"gif",
"hdr.gz",
"heic",
"heif",
"img.gz",
"j2c",
"j2k",
"jp2",
"jpeg",
"jpx",
"jxl",
"mat",
"nia.gz",
"nia",
"nii.gz",
"nii",
"png",
"tiff",
"vips",
"webp",
],
},
options: {
svg: {
scale: {

133
src/converters/vips.ts Normal file
View file

@ -0,0 +1,133 @@
import { exec } from "node:child_process";
// declare possible conversions
export const properties = {
from: {
images: [
"avif",
"bif",
"csv",
"exr",
"fits",
"gif",
"hdr.gz",
"hdr",
"heic",
"heif",
"img.gz",
"img",
"j2c",
"j2k",
"jp2",
"jpeg",
"jpx",
"jxl",
"mat",
"mrxs",
"ndpi",
"nia.gz",
"nia",
"nii.gz",
"nii",
"pdf",
"pfm",
"pgm",
"pic",
"png",
"ppm",
"raw",
"scn",
"svg",
"svs",
"svslide",
"szi",
"tif",
"tiff",
"v",
"vips",
"vms",
"vmu",
"webp",
"zip",
],
},
to: {
images: [
"avif",
"dzi",
"fits",
"gif",
"hdr.gz",
"heic",
"heif",
"img.gz",
"j2c",
"j2k",
"jp2",
"jpeg",
"jpx",
"jxl",
"mat",
"nia.gz",
"nia",
"nii.gz",
"nii",
"png",
"tiff",
"vips",
"webp",
],
},
options: {
svg: {
scale: {
description: "Scale the image up or down",
type: "number",
default: 1,
},
},
},
};
export function convert(
filePath: string,
fileType: string,
convertTo: string,
targetPath: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
options?: any,
) {
// if (fileType === "svg") {
// const scale = options.scale || 1;
// const metadata = await sharp(filePath).metadata();
// if (!metadata || !metadata.width || !metadata.height) {
// throw new Error("Could not get metadata from image");
// }
// const newWidth = Math.round(metadata.width * scale);
// const newHeight = Math.round(metadata.height * scale);
// return await sharp(filePath)
// .resize(newWidth, newHeight)
// .toFormat(convertTo)
// .toFile(targetPath);
// }
return exec(
`vips copy ${filePath} ${targetPath}`,
(error, stdout, stderr) => {
if (error) {
return error;
}
if (stdout) {
console.log(`stdout: ${stdout}`);
}
if (stderr) {
console.error(`stderr: ${stderr}`);
}
},
);
}