Skip to content

Commit

Permalink
[#44] Fix: multer 중복 세팅값 제거, 호출시 미들웨어 적용으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
meoraeng committed Oct 13, 2022
1 parent 7aa8573 commit f2c8fc7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
8 changes: 4 additions & 4 deletions backend/src/api/routes/image.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Router } from "express";
import { uploadAvatar, uploadPostImage } from "../../loaders/multer";
import { upload } from "../../loaders/multer";

const router = Router();

// POST /image/upload-avatar
// Content-Type: multipart/form-data
// Requires field(name=avatar) with image data
router.post("/upload-avatar", uploadAvatar, (req, res) => {
router.post("/upload-avatar", upload.single('avatar'), (req, res) => {
const { fieldname, originalname, encoding, mimetype, destination, filename, path, size } = req.file;

res.json({ path: `/public/${filename}` });
});

// POST /image/upload
// Content-Type: multipart/form-data
// Requires field(name=image) with image data
router.post("/upload", uploadPostImage, (req, res) => {
// Requires field(name=postImages) with image data
router.post("/upload", upload.array('postImages'), (req, res) => {
const { fieldname, originalname, encoding, mimetype, destination, filename, path, size } = req.file;

res.json({ path: `/public/${filename}` });
Expand Down
22 changes: 3 additions & 19 deletions backend/src/loaders/multer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const storage = multer.diskStorage({
}
})
/**
* 유저 프로필 사진 업로드에 대한 multer 세팅
* 이미지 파일 업로드에 대한 multer 세팅
*/
const multerAvatar = multer({
export const upload = multer({
storage: storage,
limits: { fileSize: 5 * 1024 * 1024 }, // 파일 크기 5MB 제한
fileFilter: function (req, file, done) { // 이미지 형식 파일만 필터링
Expand All @@ -42,20 +42,4 @@ const multerAvatar = multer({
}
},

});

const multerPostImage = multer({
storage: storage,
limits: { fileSize: 5 * 1024 * 1024 }, // 파일 크기 5MB 제한
fileFilter: function (req, file, done) { // 이미지 형식 파일만 필터링
if (file.mimetype.includes("image")) {
done(null, true); // 파일 허용
} else {
done(null, false); // 파일 거부
}
},

});

export const uploadAvatar = multerAvatar.single('avatar'); // 'avatar'는 front에서 받아오는 field 명
export const uploadPostImage = multerPostImage.single('image'); // 'image'는 front에서 받아오는 field 명
});

0 comments on commit f2c8fc7

Please sign in to comment.