From ff680cb29534a25c3148a90fd064bb86c71fb482 Mon Sep 17 00:00:00 2001 From: C4illin Date: Tue, 11 Jun 2024 19:30:06 +0200 Subject: [PATCH] feat: add libjxl for jpegxl conversion --- Debian.Dockerfile | 51 +++++++++++++++++++++++++++++ Dockerfile | 14 ++++---- src/converters/libjxl.ts | 71 ++++++++++++++++++++++++++++++++++++++++ src/converters/main.ts | 9 +++++ 4 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 Debian.Dockerfile create mode 100644 src/converters/libjxl.ts diff --git a/Debian.Dockerfile b/Debian.Dockerfile new file mode 100644 index 0000000..0bb17c9 --- /dev/null +++ b/Debian.Dockerfile @@ -0,0 +1,51 @@ +FROM oven/bun:1-debian as base +WORKDIR /app + +# install dependencies into temp directory +# this will cache them and speed up future builds +FROM base AS install +RUN mkdir -p /temp/dev +COPY package.json bun.lockb /temp/dev/ +RUN cd /temp/dev && bun install --frozen-lockfile + +# install with --production (exclude devDependencies) +RUN mkdir -p /temp/prod +COPY package.json bun.lockb /temp/prod/ +RUN cd /temp/prod && bun install --frozen-lockfile --production + +# copy node_modules from temp directory +# then copy all (non-ignored) project files into the image +# FROM base AS prerelease +# COPY --from=install /temp/dev/node_modules node_modules +# COPY . . + +# # [optional] tests & build +# ENV NODE_ENV=production +# RUN bun test +# RUN bun run build + +# copy production dependencies and source code into final image +FROM base AS release +LABEL maintainer="Emrik Östling (C4illin)" +LABEL description="ConvertX: self-hosted online file converter supporting 700+ file formats." +LABEL repo="https://github.com/C4illin/ConvertX" + +# install additional dependencies +RUN rm -rf /var/lib/apt/lists/partial && apt-get update -o Acquire::CompressionTypes::Order::=gz \ + && apt-get install -y \ + pandoc \ + texlive-latex-recommended \ + texlive-fonts-recommended \ + texlive-latex-extra \ + ffmpeg \ + graphicsmagick \ + ghostscript \ + libvips-tools + +COPY --from=install /temp/prod/node_modules node_modules +# COPY --from=prerelease /app/src/index.tsx /app/src/ +# COPY --from=prerelease /app/package.json . +COPY . . + +EXPOSE 3000/tcp +ENTRYPOINT [ "bun", "run", "./src/index.tsx" ] \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 0bb17c9..1d9b5dc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM oven/bun:1-debian as base +FROM oven/bun:1-alpine as base WORKDIR /app # install dependencies into temp directory @@ -31,16 +31,16 @@ LABEL description="ConvertX: self-hosted online file converter supporting 700+ f LABEL repo="https://github.com/C4illin/ConvertX" # install additional dependencies -RUN rm -rf /var/lib/apt/lists/partial && apt-get update -o Acquire::CompressionTypes::Order::=gz \ - && apt-get install -y \ +RUN apk update && apk add --no-cache \ pandoc \ - texlive-latex-recommended \ - texlive-fonts-recommended \ - texlive-latex-extra \ + texlive \ + texmf-dist-fontsextra \ + texmf-dist-latexextra \ ffmpeg \ graphicsmagick \ ghostscript \ - libvips-tools + vips-tools \ + libjxl-tools COPY --from=install /temp/prod/node_modules node_modules # COPY --from=prerelease /app/src/index.tsx /app/src/ diff --git a/src/converters/libjxl.ts b/src/converters/libjxl.ts new file mode 100644 index 0000000..aabf531 --- /dev/null +++ b/src/converters/libjxl.ts @@ -0,0 +1,71 @@ +import { exec } from "node:child_process"; + +// declare possible conversions +export const properties = { + from: { + jxl: ["jxl"], + images: [ + "apng", + "exr", + "gif", + "jpeg", + "pam", + "pfm", + "pgm", + "pgx", + "png", + "ppm", + ], + }, + to: { + jxl: [ + "apng", + "exr", + "gif", + "jpeg", + "pam", + "pfm", + "pgm", + "pgx", + "png", + "ppm", + ], + images: ["jxl"], + }, +}; + +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + // biome-ignore lint/suspicious/noExplicitAny: + options?: any, +): Promise { + let tool = ""; + if (fileType === "jxl") { + tool = "djxl"; + } + + if (convertTo === "jxl") { + tool = "cjxl"; + } + + return new Promise((resolve, reject) => { + exec(`${tool} "${filePath}" "${targetPath}"`, (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } + + if (stdout) { + console.log(`stdout: ${stdout}`); + } + + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("success"); + }); + }); +} diff --git a/src/converters/main.ts b/src/converters/main.ts index 0807b97..c855ee6 100644 --- a/src/converters/main.ts +++ b/src/converters/main.ts @@ -20,6 +20,11 @@ import { properties as propertiesPdflatex, } from "./pdflatex"; +import { + convert as convertLibjxl, + properties as propertiesLibjxl, +} from "./libjxl"; + 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 @@ -50,6 +55,10 @@ const properties: { ) => any; }; } = { + libjxl: { + properties: propertiesLibjxl, + converter: convertLibjxl, + }, vips: { properties: propertiesImage, converter: convertImage,