-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
149 lines (125 loc) · 3.6 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// express
// jimp
const express = require("express")
const exphbs = require("express-handlebars")
const multer = require("multer")
const fs = require('fs');
const path = require("path")
const app = express()
const {spawn} = require('child_process');
const fileSizeLimit = 10000000; // The limit on the file's size in bytes
const fileLifeTime = 1000 * 60 * 60; // The time until the file is deleted from the server in milliseconds
const uploadsPath = './public/uploads/';
app.use(express.static(__dirname + "/public"));
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "hbs");
/**
* Source code for multer configuration
*
* function(req, file, cb) {
cb(null, path.join(__dirname, "/uploads"))
}
*/
var storage = multer.diskStorage({
destination: uploadsPath,
filename: function (req, file, cb) {
fileExtension = file.originalname.split(".")[1]
cb(null, file.fieldname + "-" + Date.now() + "." + fileExtension)
},
})
const upload = multer({
storage: storage,
limits: { fileSize: fileSizeLimit },
fileFilter: function (req, file, cb) {
checkFileType(file, cb);
}
}).single('img');
var fileName;
var localFileName;
var mareFileName;
// Only allow image files
function checkFileType(file, cb) {
const filetypes = /jpeg|jpg|png/;
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
const minetype = filetypes.test(file.mimetype);
if (minetype && extname) {
return cb(null, true);
} else {
cb("Error: Images Only!");
}
}
// On startup, delete all files that have been uploaded
fs.readdir(uploadsPath, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(uploadsPath, file), err => {
if (err) throw err;
});
}
});
app.get("/", (req, res) => {
res.render("home")
})
// app.post("/upload", upload.single("img"), (req, res) => {
// let uploadedfile = req.file.fieldname
// if (uploadedfile) {
// res.json("file uploaded successfully")
// }
// })
var prev = undefined
app.post('/upload', (req, res) => {
upload(req, res, (err) => {
// If there is an error, show the message
if (err) {
res.render('home', {
msg: err
});
}
else {
// If the user didn't upload a file and hasn't uploaded one in the past
if (fileName == undefined && req.file == undefined) {
res.render('home', {
msg: "Error: Upload a file first!"
});
}
// If the file uploaded successfully
else {
if (req.file != undefined) {
fileName = `public/uploads/${req.file.filename}`;
localFileName = `uploads/${req.file.filename}`;
mareFileName = `public/uploads/mare_${req.file.filename}`;
// Delete the files after one hour
setTimeout(function () {
fs.unlink(fileName, (err) => {
if (err) {
console.error(err)
return;
}
});
fs.unlink(mareFileName, (err) => {
if (err) {
console.error(err)
return;
}
});
}, fileLifeTime);
}
else if(req.file == undefined){
localFileName = prev;
}
//manipulateImage(fileName);
const python = spawn('python', ['convert.py', fileName]);
python.stdout.on('data',(data) => {
console.log(data);
});
prev = localFileName;
res.render('home', {imgPath: localFileName})
}
}
});
});
// function manipulateImage(fileName)
// {
// // do stuff
// }
app.listen(process.env.PORT||5000);