-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
38 lines (37 loc) · 1.8 KB
/
functions.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
const fs = require('fs');
const path = require('path');
module.exports = {
storePhoto : function(req,res) {
let myRegister = JSON.parse(fs.readFileSync('./photo-register.json'));
let tempPath=req.file.path;
let targetPath = "./public/photos/photo" + myRegister.myPhotos.length + path.extname(req.file.originalname);
if (path.extname(targetPath).toLowerCase() === ".jpg" || path.extname(targetPath).toLowerCase() === ".jpeg" || path.extname(targetPath).toLowerCase() === ".png") {
fs.rename(tempPath, targetPath , err => {
if (err){
console.log(err)
} else {
// Add photo to register
let myDate= new Date(0);
myDate.setUTCMilliseconds(req.body.date)
let myPhotoInfos = {
originalName: req.file.originalname,
actualName: "photo" + myRegister.myPhotos.length,
photoUrl: "http://localhost:3000"+targetPath.slice(1),
uploadEpoch: req.body.date,
uploadDate: myDate,
uploadLocation: ""
}
// Add the the photo at the beggining of the array so that the photos are organized from newest to oldest
myRegister.myPhotos.unshift(myPhotoInfos);
fs.writeFileSync('./photo-register.json', JSON.stringify(myRegister))
res.send({"message":"Image sent successfully!"})
}
});
} else {
fs.unlink(tempPath, err => {
if (err) return handleError(err, res);
res.send({"message":"Sorry, only .jpg and .png files are accepted"})
});
}
}
}