Skip to content

Commit

Permalink
migrate to vips
Browse files Browse the repository at this point in the history
  • Loading branch information
C4illin committed May 25, 2024
1 parent d6b38c6 commit d81a3a6
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 59 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ RUN rm -rf /var/lib/apt/lists/partial && apt-get update -o Acquire::CompressionT
texlive-latex-recommended \
ffmpeg \
graphicsmagick \
ghostscript
ghostscript \
libvips-tools

COPY --from=install /temp/prod/node_modules node_modules
# COPY --from=prerelease /app/src/index.tsx /app/src/
Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"@elysiajs/jwt": "^1.0.2",
"@elysiajs/static": "^1.0.3",
"@picocss/pico": "^2.0.6",
"elysia": "^1.0.22",
"sharp": "^0.33.4"
"elysia": "^1.0.22"
},
"module": "src/index.tsx",
"bun-create": {
Expand Down
5 changes: 2 additions & 3 deletions src/components/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const Header = ({ loggedIn }: { loggedIn?: boolean }) => {
rightNav = (
<ul>
<li>
<a href="/test">History</a>
<a href="/history">History</a>
</li>
<li>
<a href="/logoff">Logout</a>
Expand Down Expand Up @@ -35,8 +35,7 @@ export const Header = ({ loggedIn }: { loggedIn?: boolean }) => {
style={{
textDecoration: "none",
color: "inherit",
}}
>
}}>
ConvertX
</a>
</strong>
Expand Down
3 changes: 1 addition & 2 deletions src/converters/ffmpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/converters/graphicsmagick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down
27 changes: 13 additions & 14 deletions src/converters/main.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -38,16 +37,15 @@ 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,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
) => any;
};
} = {
sharp: {
vips: {
properties: propertiesImage,
converter: convertImage,
},
Expand All @@ -65,8 +63,6 @@ const properties: {
},
};

import { normalizeFiletype } from "../helpers/normalizeFiletype";

export async function mainConverter(
inputFilePath: string,
fileTypeOriginal: string,
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
}
}

Expand Down Expand Up @@ -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}`);
13 changes: 9 additions & 4 deletions src/converters/pandoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
},
);
}
78 changes: 76 additions & 2 deletions src/converters/sharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
133 changes: 133 additions & 0 deletions src/converters/vips.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
},
);
}
Loading

0 comments on commit d81a3a6

Please sign in to comment.