-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex1.js
executable file
·90 lines (82 loc) · 2.22 KB
/
index1.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
const express = require("express");
const AWS = require("aws-sdk");
const multer = require("multer");
const multerS3 = require("multer-s3");
const multipart = require('connect-multiparty');
const multipartyMiddleware=multipart();
const app = express();
//S3 instance
const s3 = new AWS.S3({
accessKeyId: "AKIASWTZKPGGYK7PLOWR",
secretAccessKey: "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",
bucket: "s3orionbucket"
});
/**
* Single Upload
*/
const profileImgUpload = multer({
storage: multerS3({
s3: s3,
bucket: "s3orionbucket",
acl: "public-read",
key: function(req, file, cb) {
cb(
null,
path.basename(file.originalname, path.extname(file.originalname)) +
"-" +
Date.now() +
path.extname(file.originalname)
);
}
}),
limits: { fileSize: 2000000 }, // In bytes: 2000000 bytes = 2 MB
fileFilter: function(req, file, cb) {
checkFileType(file, cb);
}
}).single("profileImage");
/**
* Check File Type
* @param file
* @param cb
* @return {*}
*/
function checkFileType(file, cb) {
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb("Error: Images Only!");
}
}
app.post("/upload_file", multipartyMiddleware,(req, res) => {
// console.log(req.files);
profileImgUpload(req, res, error => {
console.log(req.files.profileImage)
if (error) {
console.log("errors", error);
res.json({ error: error });
} else {
// If File not found
if (req.files === undefined) {
console.log("Error: No File Selected!");
res.json("Error: No File Selected");
} else {
// If Success
const imageName = req.files.profileImage.name;
const location=req.files.profileImage.path;
// Save the file name into database into profile model
res.json({
image: imageName,
imagePath:location
});
}
}
});
});
const PORT = process.env.PORT || 9001;
app.listen(PORT, () => console.log(`Server is up and running on port ${PORT}`));