-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileController.java
More file actions
127 lines (115 loc) · 6.47 KB
/
FileController.java
File metadata and controls
127 lines (115 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.deepdirect.deepwebide_be.file.controller;
import com.deepdirect.deepwebide_be.file.dto.request.FileCreateRequest;
import com.deepdirect.deepwebide_be.file.dto.request.FileMoveRequest;
import com.deepdirect.deepwebide_be.file.dto.request.FileRenameRequest;
import com.deepdirect.deepwebide_be.file.dto.request.FileContentSaveRequest;
import com.deepdirect.deepwebide_be.file.dto.response.*;
import com.deepdirect.deepwebide_be.file.service.FileService;
import com.deepdirect.deepwebide_be.global.dto.ApiResponseDto;
import com.deepdirect.deepwebide_be.global.security.CustomUserDetails;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/repositories")
@Tag(name = "File", description = "파일/폴더 생성, 조회, 이름 변경, 삭제, 이동 등 기능 API")
public class FileController {
private final FileService fileService;
@Operation(summary = "파일 트리 조회", description = "각 레포지토리의 파일 트리를 조회합니다.")
@GetMapping("/{repositoryId}/files")
public ResponseEntity<ApiResponseDto<List<FileTreeNodeResponse>>> getFileTree(
@AuthenticationPrincipal CustomUserDetails userDetails,
@PathVariable Long repositoryId
) {
List<FileTreeNodeResponse> fileTree = fileService.getFileTree(repositoryId, userDetails.getId());
return ResponseEntity.ok(ApiResponseDto.of(200, "파일 트리 조회 성공했습니다.", fileTree));
}
@Operation(summary = "파일/폴더 생성", description = "파일 또는 폴더를 생성합니다.")
@PostMapping("/{repositoryId}/files")
public ResponseEntity<ApiResponseDto<FileNodeResponse>> createFileOrFolder(
@AuthenticationPrincipal CustomUserDetails userDetails,
@PathVariable Long repositoryId,
@RequestBody FileCreateRequest request
) {
FileNodeResponse response = fileService.createFileOrFolder(repositoryId, userDetails.getId(), request);
return ResponseEntity.ok(ApiResponseDto.of(201, "파일 및 폴더 생성이 성공했습니다.", response));
}
@Operation(summary = "파일/폴더 이름 변경", description = "파일 또는 폴더의 이름을 변경합니다.")
@PatchMapping("/{repositoryId}/files/{fileId}/rename")
public ResponseEntity<ApiResponseDto<FileRenameResponse>> renameFileOrFolder(
@AuthenticationPrincipal CustomUserDetails userDetails,
@PathVariable Long repositoryId,
@PathVariable Long fileId,
@RequestBody FileRenameRequest request
) {
FileRenameResponse response = fileService.renameFileOrFolder(
repositoryId,
fileId,
userDetails.getId(),
request.getNewFileName()
);
return ResponseEntity.ok(ApiResponseDto.of(200, "이름이 변경되었습니다.", response));
}
@Operation(summary = "파일/폴더 삭제", description = "특정 파일/폴더 및 하위 모두 삭제")
@DeleteMapping("/{repositoryId}/files/{fileId}")
public ResponseEntity<ApiResponseDto<Void>> deleteFileOrFolder(
@AuthenticationPrincipal CustomUserDetails userDetails,
@PathVariable Long repositoryId,
@PathVariable Long fileId
) {
fileService.deleteFileOrFolder(repositoryId, fileId, userDetails.getId());
return ResponseEntity.ok(ApiResponseDto.of(200, "삭제가 완료되었습니다.", null));
}
@Operation(summary = "파일/폴더 이동", description = "파일 또는 폴더를 다른 위치로 이동합니다.")
@PatchMapping("/{repositoryId}/files/{fileId}/move")
public ResponseEntity<ApiResponseDto<FileNodeResponse>> moveFileOrFolder(
@AuthenticationPrincipal CustomUserDetails userDetails,
@PathVariable Long repositoryId,
@PathVariable Long fileId,
@RequestBody FileMoveRequest req
) {
FileNodeResponse result = fileService.moveFileOrFolder(
repositoryId, fileId, userDetails.getId(), req.getNewParentId());
return ResponseEntity.ok(ApiResponseDto.of(200, "이동이 완료되었습니다.", result));
}
@Operation(summary = "파일 내용 조회", description = "파일의 내용을 조회합니다.")
@GetMapping("/{repositoryId}/files/{fileId}/content")
public ResponseEntity<ApiResponseDto<FileContentResponse>> getFileContent(
@AuthenticationPrincipal CustomUserDetails userDetails,
@PathVariable Long repositoryId,
@PathVariable Long fileId
) {
FileContentResponse response = fileService.getFileContent(repositoryId, fileId, userDetails.getId());
return ResponseEntity.ok(ApiResponseDto.of(200, "파일 내용 조회 성공했습니댜.", response));
}
@Operation(summary = "파일 내용 저장(실시간/레디스)", description = "파일 내용(코드 등)을 1분마다 레디스에 저장")
@PutMapping("/{repositoryId}/files/{fileId}/content")
public ResponseEntity<ApiResponseDto<FileContentSaveResponse>> saveFileContent(
@AuthenticationPrincipal CustomUserDetails userDetails,
@PathVariable Long repositoryId,
@PathVariable Long fileId,
@RequestBody FileContentSaveRequest req
) {
FileContentSaveResponse response = fileService.saveFileContent(
repositoryId, fileId, userDetails.getId(), req.getContent()
);
return ResponseEntity.ok(ApiResponseDto.of(200, "파일 내용 저장 완료했습니다.", response));
}
@PostMapping("/{repositoryId}/files/upload")
public ResponseEntity<ApiResponseDto<FileNodeResponse>> uploadFile(
@AuthenticationPrincipal CustomUserDetails userDetails,
@PathVariable Long repositoryId,
@RequestParam("parentId") @NotNull Long parentId, // 필수 지정
@RequestParam("file") MultipartFile file
) {
FileNodeResponse response = fileService.uploadFile(repositoryId, userDetails.getId(), parentId, file);
return ResponseEntity.ok(ApiResponseDto.of(201, "파일 업로드 완료", response));
}
}