-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFileController.java
More file actions
49 lines (39 loc) · 1.93 KB
/
FileController.java
File metadata and controls
49 lines (39 loc) · 1.93 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
package umc.codeplay.controller;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import io.swagger.v3.oas.annotations.Operation;
import software.amazon.awssdk.http.SdkHttpMethod;
import umc.codeplay.apiPayLoad.ApiResponse;
import umc.codeplay.dto.FileResponseDTO;
import umc.codeplay.service.FileService;
import static umc.codeplay.service.FileService.buildFilename;
@RestController
@RequestMapping("/files")
@RequiredArgsConstructor
public class FileController {
private final FileService fileService;
@Operation(
summary = "Download용 Presigned URL 생성",
description = "다운로드를 위한 Presigned URL 생성 - 유효시간 존재")
@GetMapping("/download")
public ApiResponse<FileResponseDTO.DownloadFile> getUrl(
@RequestParam(value = "fileName") String fileName) {
String downloadUrl = fileService.generatePreSignedUrl(fileName, SdkHttpMethod.GET);
FileResponseDTO.DownloadFile result = new FileResponseDTO.DownloadFile(downloadUrl);
return ApiResponse.onSuccess(result);
}
@Operation(
summary = "Upload용 Presigned URL 생성",
description = "업로드를 위한 Presigned URL 생성 - 유효시간 존재")
@PostMapping("/upload")
public ApiResponse<FileResponseDTO.UploadFile> generateUrl(
@RequestParam(value = "fileName") String fileName) {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
String newFileName = buildFilename(fileName);
Long musicId = fileService.uploadMusic(newFileName, username);
String uploadUrl = fileService.generatePreSignedUrl(newFileName, SdkHttpMethod.PUT);
FileResponseDTO.UploadFile result = new FileResponseDTO.UploadFile(uploadUrl, musicId);
return ApiResponse.onSuccess(result);
}
}