This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
69 lines (57 loc) · 1.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const download = require('download')
const gm = require('gm').subClass({ imageMagick: true })
const uuid = require('uuid/v4')
const path = require('path')
const fs = require('fs')
const potrace = require('potrace')
const express = require('express')
process.on('unhandledRejection', (err) => console.error(err))
const paths = [ 'tmp/download', 'tmp/process' ]
paths.forEach((pathName) => {
if(!fs.existsSync(pathName)) {
fs.mkdirSync(pathName)
}
})
function size(filename) {
return new Promise((resolve, reject) =>
gm(filename).size((err, size) => err ? reject(err) : resolve(size))
)
}
async function prepare(url) {
const data = await download(url, 'tmp/download')
return new Promise(async (resolve, reject) => {
const tempfile = path.resolve('tmp/process/' + uuid() + ".bmp")
const filesize = await size(data)
gm(data)
.resize(500, 500)
.blur(2,2)
.write(tempfile, (err) => err ? reject(err) : resolve(tempfile))
})
}
function vectorize(filename, color) {
return new Promise((resolve, reject) => {
potrace.trace(filename, {
turdSize: 10,
optTolerance: 10,
color: color ? '#' + color : undefined
}, (err, svg) => err ? reject(err) : resolve(svg))
})
}
async function run(url, color) {
const tmpfile = await prepare(url)
const svg = await vectorize(tmpfile, color)
return svg
}
const app = express()
.disable('x-powered-by')
.get('/*', async (req, res) => {
const url = req.originalUrl.match(/\/(([0-9A-Fa-f]{6})\/|)(http.*)/)
if(!url) {
res.send("Usage: " + req.hostname + "/[COLOR/]IMAGE_URL")
} else {
const svg = await run(url[3], url[2])
res.set('Content-Type', 'image/svg+xml')
res.send(svg)
}
})
app.listen(process.env.PORT || 3000)