-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfileUpload.js
70 lines (63 loc) · 1.89 KB
/
fileUpload.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
var express = require("express");
var router = express.Router();
var multer = require("multer");
var ImageModel = require("./api/images/image.model").ImageModel;
var path = require("path");
function addImage(data, res) {
ImageModel.create(data, function(err, image) {
if (err) {
res.json({ status: "error", image: "" });
} else {
res.json({ status: "success", image: image });
}
});
}
function makeRandomString() {
var text = "";
var possible =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
const store = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, "public/uploads/");
},
filename: (req, file, callback) => {
const tag = req.query.entityId ? makeRandomString() : "logo";
const id =
req.query.entityId || req.query.id
? req.query.entityId
? req.query.entityId
: req.query.id
: makeRandomString();
callback(null, id + "-" + tag + path.extname(file.originalname));
}
});
var upload = multer({ storage: store }).single("file");
router.post("/upload", function(req, res) {
upload(req, res, function(err) {
console.log("in upload function");
if (err) {
console.log("in upload err");
res.status(501).json({ status: "error", err: err });
} else {
console.log("in upload, success function");
const entityId = req.query.entityId;
if (entityId) {
addImage(
{ entityId: entityId, path: `/uploads/${req.file.filename}` },
res
);
} else {
console.log(req.query.id);
console.log(req.file.filename);
res
.status(200)
.json({ status: "success", path: `/uploads/${req.file.filename}` });
}
}
});
});
module.exports = router;