-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
63 lines (51 loc) · 2.45 KB
/
server.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
import { App } from '@tinyhttp/app'
import { nanoid } from 'nanoid'
import fs from 'fs/promises'
import serve from 'serve-static'
import { exec_promise, formidable_promise } from './utils.js'
const app = new App()
app.use(serve('public'))
app.post('/upload', async (req, res, next) => {
const id = nanoid(8)
const svg_dir = `public/svgs/${id}`
try {
// MAYBE: change all the nanoid + tmp_dir nonsense to mkdtemp
const tmp_dir = `${svg_dir}/tmp`
await fs.mkdir(tmp_dir, { recursive: true })
let { _ , files } = await formidable_promise(req, {
multiples: true,
uploadDir: tmp_dir
})
// Managing the unique file issue
if (!Array.isArray(files)) files = [files]
files = files.map((file) => ({
pathname: file.path.split('/').pop(),
// Turning the original name into the svg one
name: `${file.name.substring(0, file.name.lastIndexOf('.'))}.svg`
}))
for (const file of files) {
// The converting linux command uses convert (ImageMagik) and autotrace
// The 000001 color is just a magic number for background transparency
// TODO: Creer un script custom pour convertir les images en utilisant les ressources suivantes : ppmcolormask | potrace | mkbitmap | tool to read all the colors from an image
await exec_promise(`convert -background '#000001' -alpha background ${tmp_dir}/${file.pathname} ${tmp_dir}/out.ppm && autotrace --output-format svg --output-file ${svg_dir}/${file.name} --background-color 000001 ${tmp_dir}/out.ppm`)
}
// Putting all the files into a zip archive.
// Went with a zip archive because both Linux and Windows users will
// be able to extract it easily .
// The -j will junk the paths so it only stores files, no directories.
await exec_promise(`zip ${svg_dir}/${id}.zip ${svg_dir}/*.svg -j`)
res.json({
archive: `${svg_dir.substring(svg_dir.indexOf('/'))}/${id}.zip`,
files: files.map((file) => {
return {
name: file.name,
// doing substring to remove the public/ bit from the path
link: `${svg_dir.substring(svg_dir.indexOf('/'))}/${file.name}`
}
})
})
} catch(err) {
next(err)
}
})
app.listen(process.env.PORT || 8080, () => console.log('server is running'))