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

@ -33,7 +33,8 @@ RUN rm -rf /var/lib/apt/lists/partial && apt-get update -o Acquire::CompressionT
texlive-latex-recommended \ texlive-latex-recommended \
ffmpeg \ ffmpeg \
graphicsmagick \ graphicsmagick \
ghostscript ghostscript \
libvips-tools
COPY --from=install /temp/prod/node_modules node_modules COPY --from=install /temp/prod/node_modules node_modules
# COPY --from=prerelease /app/src/index.tsx /app/src/ # COPY --from=prerelease /app/src/index.tsx /app/src/

BIN
bun.lockb

Binary file not shown.

View file

@ -11,8 +11,7 @@
"@elysiajs/jwt": "^1.0.2", "@elysiajs/jwt": "^1.0.2",
"@elysiajs/static": "^1.0.3", "@elysiajs/static": "^1.0.3",
"@picocss/pico": "^2.0.6", "@picocss/pico": "^2.0.6",
"elysia": "^1.0.22", "elysia": "^1.0.22"
"sharp": "^0.33.4"
}, },
"module": "src/index.tsx", "module": "src/index.tsx",
"bun-create": { "bun-create": {

View file

@ -4,7 +4,7 @@ export const Header = ({ loggedIn }: { loggedIn?: boolean }) => {
rightNav = ( rightNav = (
<ul> <ul>
<li> <li>
<a href="/test">History</a> <a href="/history">History</a>
</li> </li>
<li> <li>
<a href="/logoff">Logout</a> <a href="/logoff">Logout</a>
@ -35,8 +35,7 @@ export const Header = ({ loggedIn }: { loggedIn?: boolean }) => {
style={{ style={{
textDecoration: "none", textDecoration: "none",
color: "inherit", color: "inherit",
}} }}>
>
ConvertX ConvertX
</a> </a>
</strong> </strong>

View file

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

View file

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

View file

@ -1,23 +1,22 @@
import { import { convert as convertImage, properties as propertiesImage } from "./vips";
properties as propertiesImage,
convert as convertImage,
} from "./sharp";
import { import {
properties as propertiesPandoc,
convert as convertPandoc, convert as convertPandoc,
properties as propertiesPandoc,
} from "./pandoc"; } from "./pandoc";
import { import {
properties as propertiesFFmpeg,
convert as convertFFmpeg, convert as convertFFmpeg,
properties as propertiesFFmpeg,
} from "./ffmpeg"; } from "./ffmpeg";
import { import {
properties as propertiesGraphicsmagick,
convert as convertGraphicsmagick, convert as convertGraphicsmagick,
properties as propertiesGraphicsmagick,
} from "./graphicsmagick"; } 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 // 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: { const properties: {
@ -38,8 +37,7 @@ const properties: {
converter: ( converter: (
filePath: string, filePath: string,
fileType: string, fileType: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation> convertTo: string,
convertTo: any,
targetPath: string, targetPath: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation> // biome-ignore lint/suspicious/noExplicitAny: <explanation>
options?: any, options?: any,
@ -47,7 +45,7 @@ const properties: {
) => any; ) => any;
}; };
} = { } = {
sharp: { vips: {
properties: propertiesImage, properties: propertiesImage,
converter: convertImage, converter: convertImage,
}, },
@ -65,8 +63,6 @@ const properties: {
}, },
}; };
import { normalizeFiletype } from "../helpers/normalizeFiletype";
export async function mainConverter( export async function mainConverter(
inputFilePath: string, inputFilePath: string,
fileTypeOriginal: string, fileTypeOriginal: string,
@ -112,7 +108,7 @@ export async function mainConverter(
console.log( console.log(
`No available converter supports converting from ${fileType} to ${convertTo}.`, `No available converter supports converting from ${fileType} to ${convertTo}.`,
); );
return; return "File type not supported"
} }
try { try {
@ -123,14 +119,17 @@ export async function mainConverter(
targetPath, targetPath,
options, options,
); );
console.log( console.log(
`Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully using ${converterName}.`, `Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully using ${converterName}.`,
); );
return "Done"
} catch (error) { } catch (error) {
console.error( console.error(
`Failed to convert ${inputFilePath} from ${fileType} to ${convertTo} using ${converterName}.`, `Failed to convert ${inputFilePath} from ${fileType} to ${convertTo} using ${converterName}.`,
error, error,
); );
return "Failed, check logs"
} }
} }
@ -256,4 +255,4 @@ export const getAllInputs = (converter: string) => {
// } // }
// // print the number of unique Inputs and Outputs // // 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}"`, `pandoc "${filePath}" -f ${fileType} -t ${convertTo} -o "${targetPath}"`,
(error, stdout, stderr) => { (error, stdout, stderr) => {
if (error) { if (error) {
console.error(`exec error: ${error}`); return error;
return; }
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 // declare possible conversions
export const properties = { export const properties = {
from: { images: ["jpeg", "png", "webp", "gif", "avif", "tiff", "svg"] }, from: {
to: { images: ["jpeg", "png", "webp", "gif", "avif", "tiff"] }, 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: { options: {
svg: { svg: {
scale: { 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}`);
}
},
);
}

View file

@ -1,25 +1,24 @@
import { Database } from "bun:sqlite";
import { randomUUID } from "node:crypto"; import { randomUUID } from "node:crypto";
import { rmSync } from "node:fs";
import { mkdir, unlink } from "node:fs/promises"; import { mkdir, unlink } from "node:fs/promises";
import cookie from "@elysiajs/cookie"; import cookie from "@elysiajs/cookie";
import { html } from "@elysiajs/html"; import { html } from "@elysiajs/html";
import { jwt } from "@elysiajs/jwt"; import { jwt } from "@elysiajs/jwt";
import { staticPlugin } from "@elysiajs/static"; import { staticPlugin } from "@elysiajs/static";
import { Database } from "bun:sqlite";
import { Elysia, t } from "elysia"; import { Elysia, t } from "elysia";
import { BaseHtml } from "./components/base"; import { BaseHtml } from "./components/base";
import { Header } from "./components/header"; import { Header } from "./components/header";
import { import {
mainConverter,
getPossibleTargets,
getPossibleInputs,
getAllTargets,
getAllInputs, getAllInputs,
getAllTargets,
getPossibleTargets,
mainConverter,
} from "./converters/main"; } from "./converters/main";
import { import {
normalizeFiletype, normalizeFiletype,
normalizeOutputFiletype, normalizeOutputFiletype,
} from "./helpers/normalizeFiletype"; } from "./helpers/normalizeFiletype";
import { rmSync } from "node:fs";
const db = new Database("./data/mydb.sqlite", { create: true }); const db = new Database("./data/mydb.sqlite", { create: true });
const uploadsDir = "./data/uploads/"; const uploadsDir = "./data/uploads/";
@ -45,6 +44,7 @@ CREATE TABLE IF NOT EXISTS file_names (
job_id INTEGER NOT NULL, job_id INTEGER NOT NULL,
file_name TEXT NOT NULL, file_name TEXT NOT NULL,
output_file_name TEXT NOT NULL, output_file_name TEXT NOT NULL,
status TEXT DEFAULT 'not started',
FOREIGN KEY (job_id) REFERENCES jobs(id) FOREIGN KEY (job_id) REFERENCES jobs(id)
); );
CREATE TABLE IF NOT EXISTS jobs ( CREATE TABLE IF NOT EXISTS jobs (
@ -56,6 +56,14 @@ CREATE TABLE IF NOT EXISTS jobs (
FOREIGN KEY (user_id) REFERENCES users(id) FOREIGN KEY (user_id) REFERENCES users(id)
);`); );`);
const dbVersion = (
db.query("PRAGMA user_version").get() as { user_version?: number }
).user_version;
if (dbVersion === 0) {
db.exec("ALTER TABLE file_names ADD COLUMN status TEXT DEFAULT 'not started';");
db.exec("PRAGMA user_version = 1;");
}
let FIRST_RUN = db.query("SELECT * FROM users").get() === null || false; let FIRST_RUN = db.query("SELECT * FROM users").get() === null || false;
interface IUser { interface IUser {
@ -69,6 +77,7 @@ interface IFileNames {
job_id: number; job_id: number;
file_name: string; file_name: string;
output_file_name: string; output_file_name: string;
status: string;
} }
interface IJobs { interface IJobs {
@ -204,27 +213,27 @@ const app = new Elysia()
}; };
} }
const savedPassword = await Bun.password.hash(body.password); const savedPassword = await Bun.password.hash(body.password);
db.query("INSERT INTO users (email, password) VALUES (?, ?)").run( db.query("INSERT INTO users (email, password) VALUES (?, ?)").run(
body.email, body.email,
savedPassword, savedPassword,
); );
const user = (await db const user = (await db
.query("SELECT * FROM users WHERE email = ?") .query("SELECT * FROM users WHERE email = ?")
.get(body.email)) as IUser; .get(body.email)) as IUser;
const accessToken = await jwt.sign({ const accessToken = await jwt.sign({
id: String(user.id), id: String(user.id),
}); });
if (!auth) { if (!auth) {
set.status = 500; set.status = 500;
return { return {
message: "No auth cookie, perhaps your browser is blocking cookies.", message: "No auth cookie, perhaps your browser is blocking cookies.",
}; };
} }
// set cookie // set cookie
auth.set({ auth.set({
value: accessToken, value: accessToken,
@ -233,8 +242,8 @@ const app = new Elysia()
maxAge: 60 * 60 * 24 * 7, maxAge: 60 * 60 * 24 * 7,
sameSite: "strict", sameSite: "strict",
}); });
redirect("/"); return redirect("/");
}, },
{ body: t.Object({ email: t.String(), password: t.String() }) }, { body: t.Object({ email: t.String(), password: t.String() }) },
) )
@ -408,6 +417,8 @@ const app = new Elysia()
sameSite: "strict", sameSite: "strict",
}); });
console.log("jobId set to:", id);
return ( return (
<BaseHtml> <BaseHtml>
<Header loggedIn /> <Header loggedIn />
@ -610,7 +621,7 @@ const app = new Elysia()
); );
const query = db.query( const query = db.query(
"INSERT INTO file_names (job_id, file_name, output_file_name) VALUES (?, ?, ?)", "INSERT INTO file_names (job_id, file_name, output_file_name, status) VALUES (?, ?, ?, ?)",
); );
// Start the conversion process in the background // Start the conversion process in the background
@ -623,7 +634,7 @@ const app = new Elysia()
const newFileName = fileName.replace(fileTypeOrig, newFileExt); const newFileName = fileName.replace(fileTypeOrig, newFileExt);
const targetPath = `${userOutputDir}${newFileName}`; const targetPath = `${userOutputDir}${newFileName}`;
await mainConverter( const result = await mainConverter(
filePath, filePath,
fileType, fileType,
convertTo, convertTo,
@ -631,7 +642,8 @@ const app = new Elysia()
{}, {},
converterName, converterName,
); );
query.run(jobId.value, fileName, newFileName);
query.run(jobId.value, fileName, newFileName, result);
}), }),
) )
.then(() => { .then(() => {
@ -642,7 +654,7 @@ const app = new Elysia()
); );
// delete all uploaded files in userUploadsDir // delete all uploaded files in userUploadsDir
rmSync(userUploadsDir, { recursive: true, force: true }); // rmSync(userUploadsDir, { recursive: true, force: true });
}) })
.catch((error) => { .catch((error) => {
console.error("Error in conversion process:", error); console.error("Error in conversion process:", error);
@ -658,7 +670,7 @@ const app = new Elysia()
}), }),
}, },
) )
.get("/test", async ({ jwt, redirect, cookie: { auth } }) => { .get("/history", async ({ jwt, redirect, cookie: { auth } }) => {
if (!auth?.value) { if (!auth?.value) {
return redirect("/login"); return redirect("/login");
} }
@ -775,6 +787,7 @@ const app = new Elysia()
<thead> <thead>
<tr> <tr>
<th>Converted File Name</th> <th>Converted File Name</th>
<th>Status</th>
<th>View</th> <th>View</th>
<th>Download</th> <th>Download</th>
</tr> </tr>
@ -784,6 +797,7 @@ const app = new Elysia()
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation> // biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
<tr> <tr>
<td>{file.output_file_name}</td> <td>{file.output_file_name}</td>
<td>{file.status}</td>
<td> <td>
<a <a
href={`/download/${outputPath}${file.output_file_name}`}> href={`/download/${outputPath}${file.output_file_name}`}>
@ -1021,16 +1035,11 @@ const clearJobs = () => {
const jobs = db const jobs = db
.query("SELECT * FROM jobs WHERE date_created < ?") .query("SELECT * FROM jobs WHERE date_created < ?")
.all(new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()) as IJobs[]; .all(new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()) as IJobs[];
for (const job of jobs) {
const files = db
.query("SELECT * FROM file_names WHERE job_id = ?")
.all(job.id) as IFileNames[];
for (const file of files) { for (const job of jobs) {
// delete the file // delete the directories
unlink(`${outputDir}${job.user_id}/${job.id}/${file.output_file_name}`); rmSync(`${outputDir}${job.user_id}/${job.id}`, { recursive: true });
} rmSync(`${uploadsDir}${job.user_id}/${job.id}`, { recursive: true });
// delete the job // delete the job
db.query("DELETE FROM jobs WHERE id = ?").run(job.id); db.query("DELETE FROM jobs WHERE id = ?").run(job.id);
@ -1038,5 +1047,5 @@ const clearJobs = () => {
// run every 24 hours // run every 24 hours
setTimeout(clearJobs, 24 * 60 * 60 * 1000); setTimeout(clearJobs, 24 * 60 * 60 * 1000);
} };
clearJobs(); clearJobs();