11package com .dmu .debug_visual .file_upload ;
22
3- import com .dmu .debug_visual .file_upload .service .S3Uploader ;
3+ import com .dmu .debug_visual .file_upload .dto .FileResponseDTO ;
4+ import com .dmu .debug_visual .file_upload .dto .UserFileDTO ;
5+ import com .dmu .debug_visual .file_upload .service .FileService ;
46import com .dmu .debug_visual .security .CustomUserDetails ;
57import io .swagger .v3 .oas .annotations .Operation ;
6- import io .swagger .v3 .oas .annotations .media . ArraySchema ;
8+ import io .swagger .v3 .oas .annotations .Parameter ;
79import io .swagger .v3 .oas .annotations .media .Content ;
810import io .swagger .v3 .oas .annotations .media .Schema ;
911import io .swagger .v3 .oas .annotations .responses .ApiResponse ;
1012import io .swagger .v3 .oas .annotations .responses .ApiResponses ;
1113import io .swagger .v3 .oas .annotations .tags .Tag ;
1214import lombok .RequiredArgsConstructor ;
13- import org .springframework .http .HttpStatus ;
1415import org .springframework .http .MediaType ;
1516import org .springframework .http .ResponseEntity ;
1617import org .springframework .security .core .annotation .AuthenticationPrincipal ;
2021import java .io .IOException ;
2122import java .util .List ;
2223
23- @ Tag (name = "파일 관리 API" , description = "S3 파일 업로드 및 사용자별 파일 목록 조회를 제공하는 컨트롤러 " )
24+ @ Tag (name = "파일 관리 API" , description = "S3 파일 생성, 수정(덮어쓰기) 및 사용자별 파일 목록 조회를 제공합니다. " )
2425@ RestController
2526@ RequiredArgsConstructor
26- @ RequestMapping ("/api/files" )
27+ @ RequestMapping ("/api/file" ) // 경로를 단수로 통일
2728public class FileController {
2829
29- private final S3Uploader s3Uploader ;
30+ private final FileService fileService ;
3031
31- @ Operation (summary = "파일 업로드" , description = "form-data 형식으로 단일 파일을 AWS S3에 업로드하고, 저장된 파일의 URL을 반환합니다. JWT 인증이 필요합니다." )
32+ @ Operation (summary = "파일 저장 또는 수정 (덮어쓰기)" ,
33+ description = "form-data로 파일을 업로드합니다. `fileUUID` 파라미터 유무에 따라 동작이 달라집니다.\n \n " +
34+ "- **`fileUUID`가 없으면**: 신규 파일로 저장하고 새로운 `fileUUID`를 발급합니다.\n " +
35+ "- **`fileUUID`가 있으면**: 해당 `fileUUID`를 가진 기존 파일을 덮어씁니다." )
3236 @ 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 )
37+ @ ApiResponse (responseCode = "200" , description = "요청 성공 (생성 또는 수정 완료)" ,
38+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = FileResponseDTO .class ))),
39+ @ ApiResponse (responseCode = "401" , description = "인증 실패 (JWT 토큰 누락 또는 유효하지 않음)" , content = @ Content ),
40+ @ ApiResponse (responseCode = "403" , description = "권한 없음 (타인의 파일 수정을 시도)" , content = @ Content ),
41+ @ ApiResponse (responseCode = "404" , description = "존재하지 않는 `fileUUID`로 수정 요청" , content = @ Content )
3942 })
4043 @ PostMapping (value = "/upload" , consumes = MediaType .MULTIPART_FORM_DATA_VALUE )
41- public ResponseEntity <String > uploadFile (
44+ public ResponseEntity <FileResponseDTO > uploadOrUpdateFile (
45+ @ Parameter (description = "업로드할 파일" )
4246 @ RequestParam ("file" ) MultipartFile file ,
43- @ AuthenticationPrincipal CustomUserDetails userDetails ) {
4447
45- String userId = userDetails .getUsername ();
48+ @ Parameter (description = "수정할 파일의 고유 ID. 신규 업로드 시에는 생략합니다." )
49+ @ RequestParam (value = "fileUUID" , required = false ) String fileUUID ,
50+
51+ @ AuthenticationPrincipal CustomUserDetails userDetails ) throws IOException {
4652
47- try {
48- String fileUrl = s3Uploader .upload (file , userId + "-codes" );
49- return ResponseEntity .ok (fileUrl );
50- } catch (IOException e ) {
51- e .printStackTrace ();
52- return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).body ("파일 업로드에 실패했습니다." );
53- }
53+ // CustomUserDetails에서 사용자 ID (Long 타입)를 가져오는 메소드가 있다고 가정합니다.
54+ // 예: userDetails.getId()
55+ String currentUserId = userDetails .getUsername ();
56+
57+ FileResponseDTO fileResponse = fileService .saveOrUpdateFile (fileUUID , file , currentUserId );
58+ return ResponseEntity .ok (fileResponse );
5459 }
5560
56- @ Operation (summary = "사용자 파일 목록 조회" , description = "특정 사용자가 업로드한 모든 파일의 목록을 조회합니다. 본인의 파일 목록만 조회할 수 있습니다 ." )
61+ @ Operation (summary = "내 파일 목록 조회" , description = "현재 로그인한 사용자가 생성한 모든 파일의 목록을 조회합니다." )
5762 @ ApiResponses ({
5863 @ 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+ content = @ Content (mediaType = "application/json" , schema = @ Schema (implementation = UserFileDTO .class ))),
65+ @ ApiResponse (responseCode = "401" , description = "인증 실패 (JWT 토큰 누락 또는 유효하지 않음)" , content = @ Content )
6466 })
6567 @ GetMapping ("/my" )
66- public ResponseEntity <List <S3FileDTO >> listFilesForUser (
68+ public ResponseEntity <List <UserFileDTO >> getMyFiles (
6769 @ AuthenticationPrincipal CustomUserDetails userDetails ) {
6870
69- String userId = userDetails .getUsername ();
70-
71- List <S3FileDTO > files = s3Uploader .listFiles (userId );
72- return ResponseEntity .ok (files );
71+ String currentUserId = userDetails .getUsername ();
72+ List <UserFileDTO > myFiles = fileService .getUserFiles (currentUserId );
73+ return ResponseEntity .ok (myFiles );
7374 }
7475}
0 commit comments