Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,40 +14,43 @@
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 {

@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<Map<String, String>> 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));
}
Expand All @@ -53,7 +59,7 @@ public ResponseEntity<Map<String, String>> uploadImage(
// GET /api/images/{filename}
@GetMapping("/{filename}")
public ResponseEntity<byte[]> 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();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading