From 37d16ad8dabdb1725e4180865427f30fa36ba60a Mon Sep 17 00:00:00 2001 From: love1ace Date: Mon, 18 Nov 2024 10:36:49 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=97=85?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=20=EC=98=A4=EB=B8=8C=EC=A0=9D=ED=8A=B8=20?= =?UTF-8?q?=EB=B0=B0=EC=97=B4=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20(#24)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/imageUpload/image.routes.ts | 50 +++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/api/imageUpload/image.routes.ts b/src/api/imageUpload/image.routes.ts index 3fb947a..587d472 100644 --- a/src/api/imageUpload/image.routes.ts +++ b/src/api/imageUpload/image.routes.ts @@ -7,16 +7,35 @@ const upload = multer({ storage: memoryStorage() }); imageRouter.post( '/upload', - upload.array('images', 10), + upload.fields([ + { name: 'thumbnail', maxCount: 1 }, + { name: 'meetingSpace', maxCount: 5 }, + { name: 'introSrcs', maxCount: 5 }, + ]), async (req: Request, res: Response): Promise => { try { - if (!req.files || req.files.length === 0) { + if (!req.files) { res.status(400).json({ message: '이미지 파일이 없습니다.' }); return; } - const imageUrls = await uploadImages(req.files as Express.Multer.File[]); - res.status(200).json({ imageUrls }); + const files = req.files as { [fieldname: string]: Express.Multer.File[] }; + + const result: { [key: string]: string[] } = { + thumbnail: [], + meetingSpace: [], + introSrcs: [], + }; + + // 각 카테고리별 이미지 업로드 처리 + for (const [category, categoryFiles] of Object.entries(files)) { + if (categoryFiles && categoryFiles.length > 0) { + const urls = await uploadImages(categoryFiles); + result[category as keyof typeof result] = urls; + } + } + + res.status(200).json(result); } catch (error) { console.error('이미지 업로드 오류:', error); res.status(500).json({ message: '이미지 업로드 실패' }); @@ -26,6 +45,23 @@ imageRouter.post( export { imageRouter }; -// curl -X POST -H "Content-Type: multipart/form-data" -F "images=@1.jpeg" -F "images=@1.jpeg" -F "images=@1.jpeg" http://localhost:3000/api/images/upload -// 이렇게 여러개 이미지를 넣으면 이렇게 반환됩니다. -// {"imageUrls":["https://d25zqr3uop6qu8.cloudfront.net/fb5f5d18-61c2-4ede-b90c-4d454526b717.jpeg","https://d25zqr3uop6qu8.cloudfront.net/2dc24822-1e96-424f-b5b5-c93ea98889d3.jpeg","https://d25zqr3uop6qu8.cloudfront.net/b4282321-eb7b-4dcc-82f9-28a26da6f113.jpeg"]}% +// curl -X POST \ +// -H "Content-Type: multipart/form-data" \ +// -F "thumbnail=@image1.jpg" \ +// -F "meetingSpace=@image2.jpg" \ +// -F "meetingSpace=@image3.jpg" \ +// -F "introSrcs=@image4.jpg" \ +// -F "introSrcs=@image5.jpg" \ +// http://localhost:3000/api/images/upload + +// { +// "thumbnail": ["https://cloudfront.url/image1.jpg"], +// "meetingSpace": [ +// "https://cloudfront.url/image2.jpg", +// "https://cloudfront.url/image3.jpg" +// ], +// "introSrcs": [ +// "https://cloudfront.url/image4.jpg", +// "https://cloudfront.url/image5.jpg" +// ] +// }