-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
99 lines (71 loc) · 2.21 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const express = require('express')
const app = express()
const path = require("path");
const {exec} = require('child_process')
const outputFilePath = "output.pdf"
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
app.use(express.static("public"));
var list = ""
const fs = require('fs')
const multer = require('multer')
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "public/uploads");
},
filename: function (req, file, cb) {
cb(
null,
file.fieldname + "-" + Date.now() + path.extname(file.originalname)
);
},
});
const imageFilter = function (req, file, cb) {
if (
file.mimetype == "image/png" ||
file.mimetype == "image/jpg" ||
file.mimetype == "image/jpeg"
) {
cb(null, true);
} else {
cb(null, false);
return cb(new Error("Only .png, .jpg and .jpeg format allowed!"));
}
};
var upload = multer({ storage: storage, fileFilter: imageFilter });
const PORT = process.env.PORT || 3000
var dir = "public";
var subDirectory = "public/uploads";
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
fs.mkdirSync(subDirectory);
}
app.get('/',(req,res) => {
res.sendFile(__dirname + "/index.html")
})
app.post('/merge',upload.array('files',100),(req,res) => {
list = ""
if(req.files){
req.files.forEach(file => {
console.log(file.path)
list+= `${file.path}`
list+= " "
});
console.log(list)
exec(`magick convert ${list} -units pixelsperinch -density 72 -page letter ${outputFilePath}`,(err,stdout,stderr) => {
if(err) throw err
res.download(outputFilePath,(err) => {
if(err) throw err
// delete the files which is stored
req.files.forEach(file => {
fs.unlinkSync(file.path)
});
fs.unlinkSync(outputFilePath)
})
})
}
})
app.listen(PORT,() => {
console.log(`App is listening on Port ${PORT}`)
})