mirror of
https://github.com/C4illin/ConvertX.git
synced 2026-06-28 23:15:47 +00:00
migrate to vips
This commit is contained in:
@@ -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/
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -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>
|
||||
@@ -35,8 +35,7 @@ export const Header = ({ loggedIn }: { loggedIn?: boolean }) => {
|
||||
style={{
|
||||
textDecoration: "none",
|
||||
color: "inherit",
|
||||
}}
|
||||
>
|
||||
}}>
|
||||
ConvertX
|
||||
</a>
|
||||
</strong>
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
|
||||
@@ -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}`);
|
||||
|
||||
@@ -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}`);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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
133
src/converters/vips.ts
Normal 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}`);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -1,25 +1,24 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { rmSync } from "node:fs";
|
||||
import { mkdir, unlink } from "node:fs/promises";
|
||||
import cookie from "@elysiajs/cookie";
|
||||
import { html } from "@elysiajs/html";
|
||||
import { jwt } from "@elysiajs/jwt";
|
||||
import { staticPlugin } from "@elysiajs/static";
|
||||
import { Database } from "bun:sqlite";
|
||||
import { Elysia, t } from "elysia";
|
||||
import { BaseHtml } from "./components/base";
|
||||
import { Header } from "./components/header";
|
||||
import {
|
||||
mainConverter,
|
||||
getPossibleTargets,
|
||||
getPossibleInputs,
|
||||
getAllTargets,
|
||||
getAllInputs,
|
||||
getAllTargets,
|
||||
getPossibleTargets,
|
||||
mainConverter,
|
||||
} from "./converters/main";
|
||||
import {
|
||||
normalizeFiletype,
|
||||
normalizeOutputFiletype,
|
||||
} from "./helpers/normalizeFiletype";
|
||||
import { rmSync } from "node:fs";
|
||||
|
||||
const db = new Database("./data/mydb.sqlite", { create: true });
|
||||
const uploadsDir = "./data/uploads/";
|
||||
@@ -45,6 +44,7 @@ CREATE TABLE IF NOT EXISTS file_names (
|
||||
job_id INTEGER NOT NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
output_file_name TEXT NOT NULL,
|
||||
status TEXT DEFAULT 'not started',
|
||||
FOREIGN KEY (job_id) REFERENCES jobs(id)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS jobs (
|
||||
@@ -56,6 +56,14 @@ CREATE TABLE IF NOT EXISTS jobs (
|
||||
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;
|
||||
|
||||
interface IUser {
|
||||
@@ -69,6 +77,7 @@ interface IFileNames {
|
||||
job_id: number;
|
||||
file_name: string;
|
||||
output_file_name: string;
|
||||
status: string;
|
||||
}
|
||||
|
||||
interface IJobs {
|
||||
@@ -204,27 +213,27 @@ const app = new Elysia()
|
||||
};
|
||||
}
|
||||
const savedPassword = await Bun.password.hash(body.password);
|
||||
|
||||
|
||||
db.query("INSERT INTO users (email, password) VALUES (?, ?)").run(
|
||||
body.email,
|
||||
savedPassword,
|
||||
);
|
||||
|
||||
|
||||
const user = (await db
|
||||
.query("SELECT * FROM users WHERE email = ?")
|
||||
.get(body.email)) as IUser;
|
||||
|
||||
|
||||
const accessToken = await jwt.sign({
|
||||
id: String(user.id),
|
||||
});
|
||||
|
||||
|
||||
if (!auth) {
|
||||
set.status = 500;
|
||||
return {
|
||||
message: "No auth cookie, perhaps your browser is blocking cookies.",
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// set cookie
|
||||
auth.set({
|
||||
value: accessToken,
|
||||
@@ -233,8 +242,8 @@ const app = new Elysia()
|
||||
maxAge: 60 * 60 * 24 * 7,
|
||||
sameSite: "strict",
|
||||
});
|
||||
|
||||
redirect("/");
|
||||
|
||||
return redirect("/");
|
||||
},
|
||||
{ body: t.Object({ email: t.String(), password: t.String() }) },
|
||||
)
|
||||
@@ -408,6 +417,8 @@ const app = new Elysia()
|
||||
sameSite: "strict",
|
||||
});
|
||||
|
||||
console.log("jobId set to:", id);
|
||||
|
||||
return (
|
||||
<BaseHtml>
|
||||
<Header loggedIn />
|
||||
@@ -610,7 +621,7 @@ const app = new Elysia()
|
||||
);
|
||||
|
||||
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
|
||||
@@ -623,7 +634,7 @@ const app = new Elysia()
|
||||
const newFileName = fileName.replace(fileTypeOrig, newFileExt);
|
||||
const targetPath = `${userOutputDir}${newFileName}`;
|
||||
|
||||
await mainConverter(
|
||||
const result = await mainConverter(
|
||||
filePath,
|
||||
fileType,
|
||||
convertTo,
|
||||
@@ -631,7 +642,8 @@ const app = new Elysia()
|
||||
{},
|
||||
converterName,
|
||||
);
|
||||
query.run(jobId.value, fileName, newFileName);
|
||||
|
||||
query.run(jobId.value, fileName, newFileName, result);
|
||||
}),
|
||||
)
|
||||
.then(() => {
|
||||
@@ -642,7 +654,7 @@ const app = new Elysia()
|
||||
);
|
||||
|
||||
// delete all uploaded files in userUploadsDir
|
||||
rmSync(userUploadsDir, { recursive: true, force: true });
|
||||
// rmSync(userUploadsDir, { recursive: true, force: true });
|
||||
})
|
||||
.catch((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) {
|
||||
return redirect("/login");
|
||||
}
|
||||
@@ -775,6 +787,7 @@ const app = new Elysia()
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Converted File Name</th>
|
||||
<th>Status</th>
|
||||
<th>View</th>
|
||||
<th>Download</th>
|
||||
</tr>
|
||||
@@ -784,6 +797,7 @@ const app = new Elysia()
|
||||
// biome-ignore lint/correctness/useJsxKeyInIterable: <explanation>
|
||||
<tr>
|
||||
<td>{file.output_file_name}</td>
|
||||
<td>{file.status}</td>
|
||||
<td>
|
||||
<a
|
||||
href={`/download/${outputPath}${file.output_file_name}`}>
|
||||
@@ -1021,16 +1035,11 @@ const clearJobs = () => {
|
||||
const jobs = db
|
||||
.query("SELECT * FROM jobs WHERE date_created < ?")
|
||||
.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) {
|
||||
// delete the file
|
||||
unlink(`${outputDir}${job.user_id}/${job.id}/${file.output_file_name}`);
|
||||
}
|
||||
for (const job of jobs) {
|
||||
// delete the directories
|
||||
rmSync(`${outputDir}${job.user_id}/${job.id}`, { recursive: true });
|
||||
rmSync(`${uploadsDir}${job.user_id}/${job.id}`, { recursive: true });
|
||||
|
||||
// delete the job
|
||||
db.query("DELETE FROM jobs WHERE id = ?").run(job.id);
|
||||
@@ -1038,5 +1047,5 @@ const clearJobs = () => {
|
||||
|
||||
// run every 24 hours
|
||||
setTimeout(clearJobs, 24 * 60 * 60 * 1000);
|
||||
}
|
||||
clearJobs();
|
||||
};
|
||||
clearJobs();
|
||||
|
||||
Reference in New Issue
Block a user