Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prise en charge d'une url vers un SVG #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM pluswerk/puppeteer
FROM ghcr.io/puppeteer/puppeteer:latest

WORKDIR /app

RUN yarn add convert-svg-to-png express body-parser
RUN yarn add convert-svg-to-png express body-parser axios

COPY index.mjs /app/

Expand Down
38 changes: 26 additions & 12 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -1,35 +1,51 @@
import convertSvgToPng from "convert-svg-to-png";
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";

const serverPort = 3000;
const app = express();
const converter = convertSvgToPng.createConverter({
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox']
}
});

app.use(bodyParser.text({
type: '*/*',
limit: '10mb',
}));

app.post('/convert', async (req, res) => {
app.get('/convert', async (req, res) => {
var converter;
try {
converter = convertSvgToPng.createConverter({
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox']
}
});

const options = {};
options.background = req.query.background || '#fff';
if (req.query.background) options.background = req.query.background;
options.baseUrl = req.query.baseUrl || 'file:///app';
options.scale = req.query.scale || 1;
if (req.query.baseFile) options.baseFile = req.query.baseFile;
if (req.query.height) options.height = req.query.height;
if (req.query.width) options.width = req.query.width;
console.log('convert svg', options);
const png = await converter.convert(req.body, options);
res.set('Content-Type', 'image/png');
res.send(png);
console.log('convert url', req.query.url);
var resp = await axios.get(req.query.url);
var svg = resp.data;
if (svg.trim().toLowerCase().startsWith("<svg")) {
const png = await converter.convert(svg, options);
res.set('Content-Type', 'image/png');
res.send(png);
} else {
res.redirect(req.query.url);
}
} catch (e) {
res.status(500).send(e.message);
console.log('error', e);
} finally {
if(converter) {
console.log('graceful shutdown puppeteer');
await converter.destroy();
}
}
});

Expand All @@ -41,8 +57,6 @@ server.on('connection', function (socket) {
});

const shutdown = async () => {
console.log('graceful shutdown puppeteer');
await converter.destroy();
console.log('graceful shutdown express');
server.close(function () {
console.log('closed express');
Expand Down