middlewares.js
1.28 KB
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
import dotenv from "dotenv";
import multer from "multer";
import multerS3 from "multer-s3";
import aws from "aws-sdk";
import routes from "./routes";
dotenv.config();
const s3 = new aws.S3({
accessKeyId: process.env.AWS_KEY,
secretAccessKey: process.env.AWS_PRIVATEE_KEY,
region: "ap-northeast-2",
});
// const multerVideo = multer({ dest: "uploads/videos/" });
const multerVideo = multer({
storage: multerS3({
s3,
acl: "public-read",
bucket: "khutube/video",
}),
});
// const multerAvatar = multer({ dest: "uploads/avatars/" });
const multerAvatar = multer({
storage: multerS3({
s3,
acl: "public-read",
bucket: "khutube/avatars",
}),
});
export const localsMiddleware = (req, res, next) => {
res.locals.siteName = "my Youtube";
res.locals.routes = routes;
res.locals.loggedUser = req.user || null;
next();
};
export const onlyPublic = (req, res, next) => {
if (req.user) {
res.redirect(routes.home);
} else {
next();
}
};
export const onlyPrivate = (req, res, next) => {
if (req.user) {
next();
} else {
res.redirect(routes.home);
}
};
export const uploadVideo = multerVideo.single("videoFile");
// single에 들어간 videoFile은 upload.pug의 file 부분 input name
export const uploadAvatar = multerAvatar.single("avatar");