From f2c8fc73ff8fe51c1dad894264af6b85e21205d1 Mon Sep 17 00:00:00 2001 From: SeonWoo KIm Date: Thu, 13 Oct 2022 14:55:25 +0000 Subject: [PATCH] =?UTF-8?q?[#44]=20Fix:=20multer=20=EC=A4=91=EB=B3=B5=20?= =?UTF-8?q?=EC=84=B8=ED=8C=85=EA=B0=92=20=EC=A0=9C=EA=B1=B0,=20=ED=98=B8?= =?UTF-8?q?=EC=B6=9C=EC=8B=9C=20=EB=AF=B8=EB=93=A4=EC=9B=A8=EC=96=B4=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/api/routes/image.js | 8 ++++---- backend/src/loaders/multer.js | 22 +++------------------- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/backend/src/api/routes/image.js b/backend/src/api/routes/image.js index 8369365..099154c 100644 --- a/backend/src/api/routes/image.js +++ b/backend/src/api/routes/image.js @@ -1,12 +1,12 @@ 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}` }); @@ -14,8 +14,8 @@ router.post("/upload-avatar", uploadAvatar, (req, res) => { // 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}` }); diff --git a/backend/src/loaders/multer.js b/backend/src/loaders/multer.js index 45b492b..4dbbf15 100644 --- a/backend/src/loaders/multer.js +++ b/backend/src/loaders/multer.js @@ -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) { // 이미지 형식 파일만 필터링 @@ -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 명 +}); \ No newline at end of file