forked from dhaveed/ColorSVG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·87 lines (71 loc) · 2.4 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const express = require('express');
const app = express();
const morgan = require('morgan');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const icongen = require('icon-gen');
const fs = require('fs-extra');
const zipFolder = require('zip-folder');
const uniqid = require('uniqid');
const exec = require('child-process-promise').exec;
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({
'extended': 'true'
}));
app.use(bodyParser.json());
app.use(bodyParser.json({
type: 'application/vnd.api+json'
}));
app.use(methodOverride());
const port = process.env.PORT || 8080;
app.get('/api/download/:dir', (req, res) => {
const tempDir = 'tmp/' + req.params.dir;
res.download(tempDir + '/icons.zip');
fs.remove(tempDir, () => {});
});
app.post('/api/convert', (req, res) => {
console.log("Converting");
console.log(req.body.image);
const tmp_name = uniqid();
const dir = './tmp/' + tmp_name;
const src = dir + '/src';
const dist = dir + '/dist';
fs.ensureDir(src, err => {
console.log(err);
fs.ensureDir(dist, err => {
console.log(err);
fs.writeFile(src + "/image.svg", req.body.image, err => {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
const options = {
report: true
};
const cmd = 'icon-gen -i ' + src + '/image.svg -o ' + dist + ' -r';
exec(cmd)
.then(function (result) {
zipFolder(dist, dir + '/icons.zip', err => {
if (err) {
console.log('oh no!', err);
res.send('error');
} else {
console.log('EXCELLENT');
res.send(tmp_name);
}
});
})
.catch(function (err) {
console.error(err);
res.send('error');
});
});
});
});
});
app.get('*', function (req, res) {
res.sendfile('./public/index.html');
});
app.listen(port);
console.log("app listening on " + port);