11package com .dmu .debug_visual .file_upload ;
22
3+ import com .dmu .debug_visual .file_upload .service .S3Uploader ;
34import com .dmu .debug_visual .security .CustomUserDetails ;
45import io .swagger .v3 .oas .annotations .Operation ;
6+ import io .swagger .v3 .oas .annotations .media .ArraySchema ;
7+ import io .swagger .v3 .oas .annotations .media .Content ;
8+ import io .swagger .v3 .oas .annotations .media .Schema ;
9+ import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
10+ import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
11+ import io .swagger .v3 .oas .annotations .tags .Tag ;
512import lombok .RequiredArgsConstructor ;
613import org .springframework .http .HttpStatus ;
714import org .springframework .http .MediaType ;
815import org .springframework .http .ResponseEntity ;
916import org .springframework .security .core .annotation .AuthenticationPrincipal ;
10- import org .springframework .web .bind .annotation .PostMapping ;
11- import org .springframework .web .bind .annotation .RequestMapping ;
12- import org .springframework .web .bind .annotation .RequestParam ;
13- import org .springframework .web .bind .annotation .RestController ;
17+ import org .springframework .web .bind .annotation .*;
1418import org .springframework .web .multipart .MultipartFile ;
1519
1620import java .io .IOException ;
21+ import java .util .List ;
1722
23+ @ Tag (name = "파일 관리 API" , description = "S3 파일 업로드 및 사용자별 파일 목록 조회를 제공하는 컨트롤러" )
1824@ RestController
1925@ RequiredArgsConstructor
20- @ RequestMapping ("/api/files" ) // 공통되는 URL 경로 설정
26+ @ RequestMapping ("/api/files" )
2127public class FileController {
2228
2329 private final S3Uploader s3Uploader ;
2430
25- @ Operation (summary = "파일 업로드" , description = "form-data 형식으로 파일을 업로드합니다." )
31+ @ Operation (summary = "파일 업로드" , description = "form-data 형식으로 단일 파일을 AWS S3에 업로드하고, 저장된 파일의 URL을 반환합니다. JWT 인증이 필요합니다." )
32+ @ ApiResponses ({
33+ @ ApiResponse (responseCode = "200" , description = "업로드 성공" ,
34+ content = @ Content (mediaType = "text/plain" , schema = @ Schema (type = "string" , example = "https://your-bucket.s3.amazonaws.com/.../file.txt" ))),
35+ @ ApiResponse (responseCode = "401" , description = "인증 실패 (JWT 토큰 누락 또는 유효하지 않음)" ,
36+ content = @ Content ),
37+ @ ApiResponse (responseCode = "500" , description = "서버 내부 오류 (파일 업로드 실패)" ,
38+ content = @ Content )
39+ })
2640 @ PostMapping (value = "/upload" , consumes = MediaType .MULTIPART_FORM_DATA_VALUE )
2741 public ResponseEntity <String > uploadFile (
2842 @ RequestParam ("file" ) MultipartFile file ,
29- @ AuthenticationPrincipal CustomUserDetails userDetails ) { // <-- String 대신 CustomUserDetails로 변경
43+ @ AuthenticationPrincipal CustomUserDetails userDetails ) {
3044
31- // userDetails 객체에서 userId를 직접 꺼내서 사용합니다.
32- String userId = userDetails .getUsername (); // 또는 userDetails.getUser().getUserId()
45+ String userId = userDetails .getUsername ();
3346
3447 try {
3548 String fileUrl = s3Uploader .upload (file , userId + "-codes" );
@@ -39,4 +52,23 @@ public ResponseEntity<String> uploadFile(
3952 return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).body ("파일 업로드에 실패했습니다." );
4053 }
4154 }
55+
56+ @ Operation (summary = "사용자 파일 목록 조회" , description = "특정 사용자가 업로드한 모든 파일의 목록을 조회합니다. 본인의 파일 목록만 조회할 수 있습니다." )
57+ @ ApiResponses ({
58+ @ ApiResponse (responseCode = "200" , description = "조회 성공" ,
59+ content = @ Content (mediaType = "application/json" , array = @ ArraySchema (schema = @ Schema (implementation = S3FileDTO .class )))),
60+ @ ApiResponse (responseCode = "401" , description = "인증 실패 (JWT 토큰 누락 또는 유효하지 않음)" ,
61+ content = @ Content ),
62+ @ ApiResponse (responseCode = "403" , description = "접근 권한 없음 (다른 사용자의 파일 목록에 접근 시도)" ,
63+ content = @ Content )
64+ })
65+ @ GetMapping ("/my" )
66+ public ResponseEntity <List <S3FileDTO >> listFilesForUser (
67+ @ AuthenticationPrincipal CustomUserDetails userDetails ) {
68+
69+ String userId = userDetails .getUsername ();
70+
71+ List <S3FileDTO > files = s3Uploader .listFiles (userId );
72+ return ResponseEntity .ok (files );
73+ }
4274}
0 commit comments