diff --git a/backend/src/main/java/com/example/Piroin/project/domain/question/controller/ImageController.java b/backend/src/main/java/com/example/Piroin/project/domain/question/controller/ImageController.java index 8a9f658..1fdac22 100644 --- a/backend/src/main/java/com/example/Piroin/project/domain/question/controller/ImageController.java +++ b/backend/src/main/java/com/example/Piroin/project/domain/question/controller/ImageController.java @@ -1,8 +1,11 @@ package com.example.Piroin.project.domain.question.controller; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.ResponseEntity; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -11,12 +14,6 @@ import java.util.Map; import java.util.UUID; -/* 이미지 업로드/조회 컨트롤러 - -[흐름] -1. POST /api/images 로 이미지 파일 전송 → URL 반환 -2. 반환된 URL을 질문/댓글 등록 요청의 imageUrl 필드에 포함 -3. GET /api/images/{filename} 으로 저장된 이미지 조회 */ @RestController @RequestMapping("/api/images") public class ImageController { @@ -24,27 +21,36 @@ public class ImageController { @Value("${file.upload-dir}") private String uploadDir; - /* 이미지 업로드 - POST /api/images - Content-Type: multipart/form-data */ - @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + // consumes 제거 + // Swagger용 어노테이션 추가 (파일 선택 버튼 표시용) + @PostMapping + @Operation( + requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody( + content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE, + schema = @Schema(type = "object", requiredProperties = {"file"})) + ) + ) public ResponseEntity> uploadImage( @RequestParam("file") MultipartFile file ) throws IOException { - // 저장 폴더 없으면 생성 - File dir = new File(uploadDir); + // 절대 경로로 변환 + File dir = new File(uploadDir).getAbsoluteFile(); if (!dir.exists()) { dir.mkdirs(); } // 파일명 중복 방지: UUID + 원본 확장자 String originalName = file.getOriginalFilename(); - String extension = originalName.substring(originalName.lastIndexOf(".")); + String extension = ""; + if (originalName != null && originalName.contains(".")) { + extension = originalName.substring(originalName.lastIndexOf(".")); + } String savedName = UUID.randomUUID() + extension; - // 파일 저장 - file.transferTo(new File(uploadDir + savedName)); + // 절대 경로로 파일 저장 + File targetFile = new File(dir, savedName); + file.transferTo(targetFile); return ResponseEntity.ok(Map.of("imageUrl", "/api/images/" + savedName)); } @@ -53,7 +59,7 @@ public ResponseEntity> uploadImage( // GET /api/images/{filename} @GetMapping("/{filename}") public ResponseEntity getImage(@PathVariable String filename) throws IOException { - File file = new File(uploadDir + filename); + File file = new File(new File(uploadDir).getAbsoluteFile(), filename); // ← 절대 경로 if (!file.exists()) { return ResponseEntity.notFound().build(); diff --git a/backend/uploads/3ba85e36-14a1-478f-aac7-9d0a1e0bc0a8.png b/backend/uploads/3ba85e36-14a1-478f-aac7-9d0a1e0bc0a8.png new file mode 100644 index 0000000..52a8826 Binary files /dev/null and b/backend/uploads/3ba85e36-14a1-478f-aac7-9d0a1e0bc0a8.png differ diff --git a/backend/uploads/83d5e181-fa7e-43a8-b28c-7d216b12b0bf.png b/backend/uploads/83d5e181-fa7e-43a8-b28c-7d216b12b0bf.png new file mode 100644 index 0000000..52a8826 Binary files /dev/null and b/backend/uploads/83d5e181-fa7e-43a8-b28c-7d216b12b0bf.png differ diff --git a/backend/uploads/cc5db130-976d-4098-8f2a-ecc538ca5a0c.png b/backend/uploads/cc5db130-976d-4098-8f2a-ecc538ca5a0c.png new file mode 100644 index 0000000..52a8826 Binary files /dev/null and b/backend/uploads/cc5db130-976d-4098-8f2a-ecc538ca5a0c.png differ diff --git a/backend/uploads/eb8c0be8-6fce-4cf4-92aa-758914f31699.png b/backend/uploads/eb8c0be8-6fce-4cf4-92aa-758914f31699.png new file mode 100644 index 0000000..52a8826 Binary files /dev/null and b/backend/uploads/eb8c0be8-6fce-4cf4-92aa-758914f31699.png differ