-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (52 loc) · 1.57 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
const express = require('express');
const app = express();
const router = express.Router();
const bodyParser = require('body-parser');
const multer = require('multer');
const aes = require('./client/src/utils/aes_utils.js');
const fs = require('fs');
//for body parsing
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : true}));
app.use('/api',router);
const port = 3001;
//for multer storage
const storage = multer.diskStorage({
destination : function(req , file, callback){
callback(null, './uploads');
},
filename : function(req, file, callback){
callback(null,file.originalname +"_" + Date.now());
}
});
const upload = multer({storage : storage}).single('encryptedImg');
app.get('/', (req, res) =>{
res.send("welcome to the server");
});
router.post('/upload',upload, (req, res) =>{
console.log(req.file);
fs.readFile(`./uploads/${req.file.filename}`, 'base64', function(err, data){
if(err) throw err;
let decrypted = aes.decrypt(data);
let buf = Buffer.from(decrypted,"base64");
fs.writeFile(`./uploads/${req.file.originalname}`, buf, function(err){
if(err) throw err;
fs.unlink(`./uploads/${req.file.filename}`, function(err){
if(err){
res.status(400).json({
success : false
});
};
console.log('file saved succesfully');
res.json({
success : true
});
}) ;
});
});
});
//listening to a port
app.listen(port,()=>{
console.log('server is running on port '+port);
});
module.exports = app ;