From d5eeef9f6884b2bb878508bed97ea9ceaa662995 Mon Sep 17 00:00:00 2001 From: C4illin Date: Fri, 23 Aug 2024 19:14:06 +0200 Subject: [PATCH] feat: add resvg converter --- Dockerfile | 8 +++++++ README.md | 3 ++- src/converters/main.ts | 9 ++++++++ src/converters/resvg.ts | 41 ++++++++++++++++++++++++++++++++++++ src/helpers/printVersions.ts | 10 +++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/converters/resvg.ts diff --git a/Dockerfile b/Dockerfile index db620cf..b920f7b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ FROM oven/bun:1.1.25-alpine AS base +LABEL org.opencontainers.image.source="https://github.com/C4illin/ConvertX" WORKDIR /app # install dependencies into temp directory @@ -13,6 +14,12 @@ RUN mkdir -p /temp/prod COPY package.json bun.lockb /temp/prod/ RUN cd /temp/prod && bun install --frozen-lockfile --production +FROM base AS builder +RUN apk --no-cache add curl gcc +ENV PATH=/root/.cargo/bin:$PATH +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y +RUN cargo install resvg + # copy node_modules from temp directory # then copy all (non-ignored) project files into the image # FROM base AS prerelease @@ -48,6 +55,7 @@ RUN apk --no-cache add \ # texmf-dist-fontsextra \ COPY --from=install /temp/prod/node_modules node_modules +COPY --from=builder /root/.cargo/bin/resvg /usr/local/bin/resvg # COPY --from=prerelease /app/src/index.tsx /app/src/ # COPY --from=prerelease /app/package.json . COPY . . diff --git a/README.md b/README.md index 8c83d0c..f95ae4e 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,9 @@ A self-hosted online file converter. Supports 831 different formats. Written wit | Converter | Use case | Converts from | Converts to | |------------------------------------------------------------------------------|---------------|---------------|-------------| | [libjxl](https://github.com/libjxl/libjxl) | JPEG XL | 11 | 11 | +| [resvg](https://github.com/RazrFalcon/resvg) | SVG | 1 | 1 | | [Vips](https://github.com/libvips/libvips) | Images | 45 | 23 | -| [XeLaTeX](https://tug.org/xetex/) | Documents | 1 | 1 | +| [XeLaTeX](https://tug.org/xetex/) | LaTeX | 1 | 1 | | [Pandoc](https://pandoc.org/) | Documents | 43 | 65 | | [GraphicsMagick](http://www.graphicsmagick.org/) | Images | 166 | 133 | | [FFmpeg](https://ffmpeg.org/) | Video | ~473 | ~280 | diff --git a/src/converters/main.ts b/src/converters/main.ts index 076c03e..6b86fea 100644 --- a/src/converters/main.ts +++ b/src/converters/main.ts @@ -25,6 +25,11 @@ import { properties as propertiesLibjxl, } from "./libjxl"; +import { + convert as convertresvg, + properties as propertiesresvg, +} from "./resvg"; + 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 @@ -59,6 +64,10 @@ const properties: { properties: propertiesLibjxl, converter: convertLibjxl, }, + resvg: { + properties: propertiesresvg, + converter: convertresvg, + }, vips: { properties: propertiesImage, converter: convertImage, diff --git a/src/converters/resvg.ts b/src/converters/resvg.ts new file mode 100644 index 0000000..e971378 --- /dev/null +++ b/src/converters/resvg.ts @@ -0,0 +1,41 @@ +import { exec } from "node:child_process"; + +export const properties = { + from: { + images: ["svg"], + }, + to: { + images: ["png"], + }, +}; + + +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + // biome-ignore lint/suspicious/noExplicitAny: + options?: any, +): Promise { + return new Promise((resolve, reject) => { + exec( + `resvg "${filePath}" "${targetPath}"`, + (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } + + if (stdout) { + console.log(`stdout: ${stdout}`); + } + + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("success"); + }, + ); + }); +} \ No newline at end of file diff --git a/src/helpers/printVersions.ts b/src/helpers/printVersions.ts index a36da86..51f7433 100644 --- a/src/helpers/printVersions.ts +++ b/src/helpers/printVersions.ts @@ -73,6 +73,16 @@ if (process.env.NODE_ENV === "production") { } }); + exec("resvg -V", (error, stdout) => { + if (error) { + console.error("resvg is not installed"); + } + + if (stdout) { + console.log(`resvg v${stdout.split("\n")[0]}`); + } + }); + exec("bun -v", (error, stdout) => { if (error) { console.error("Bun is not installed. wait what");