Skip to content

Commit

Permalink
Update : S3 file Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kdh10806 committed Jan 2, 2025
1 parent f52893b commit efeabe2
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class GlobalExceptionHandler {

@ExceptionHandler(BusinessException.class)
public ResponseEntity<APIResponse<?>> handleBusinessException(BusinessException e) {
e.printStackTrace();
return ResponseEntity
.status(e.getErrorCode().getStatus())
.body(APIResponse.fail(e.getErrorCode()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public static <T> APIResponse<T> fail(ErrorCode code, String message) {
}

//생성
// public static <T> APIResponse<T> create(T data) {
// return new APIResponse<>(data);
// }
public static <T> APIResponse<T> create(T data) {
return new APIResponse<>(SuccessCode.CREATED.getStatusCode(), SuccessCode.CREATED.getMessage(), data);
}

public static <T> APIResponse<T> create(int code, String message, T data) {
return new APIResponse<>(code, message, data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seveneleven.devlens.global.util.file.Service;

import com.seveneleven.devlens.global.exception.BusinessException;
import com.seveneleven.devlens.global.response.APIResponse;
import com.seveneleven.devlens.global.response.ErrorCode;
import com.seveneleven.devlens.global.util.file.FileValidator;
import com.seveneleven.devlens.global.util.file.dto.FileMetadataDto;
Expand Down Expand Up @@ -28,7 +29,7 @@ public class FileService {
* @return FileMetadataDto 업로드한 파일 메타데이터
*/
@Transactional
public FileMetadataDto uploadFile(MultipartFile file, Long uploaderId, String fileCategory, Long referenceId) throws Exception {
public APIResponse uploadFile(MultipartFile file, Long uploaderId, String fileCategory, Long referenceId) throws Exception {
//1. 파일 검증
FileValidator.validateFile(file, fileCategory);

Expand Down Expand Up @@ -67,7 +68,7 @@ public FileMetadataDto uploadFile(MultipartFile file, Long uploaderId, String fi
FileMetadata savedMetadata = fileMetadataRepository.save(metadata);

//DTO로 변환 후 반환
return FileMetadataDto.toDto(savedMetadata);
return APIResponse.create(FileMetadataDto.toDto(savedMetadata));

} catch (Exception e){
//저장 실패시 S3에서 삭제
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ public String generateUniqueFileName(String originalFileName) {
* 키 = S3 저장시 파일 경로
*/
public String generateS3Key(Long uploaderId, String category, String fileName) {
return String.format("%s/%s/%s", uploaderId, category, fileName);
return new StringBuilder()
.append(uploaderId)
.append("/")
.append(category)
.append("/")
.append(fileName)
.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public class FileController {
@ApiResponse(responseCode = "400", description = "Invalid file upload request")
}
)
public ResponseEntity<FileMetadataDto> uploadFile(@RequestParam("file") @Schema(type = "string", format = "binary", description = "File to upload") MultipartFile file) throws Exception {
FileMetadataDto fileMetadataDto = fileService.uploadFile(file, 1L, "POST_ATTACHMENT", 1L);
// return APIResponse.success(fileMetadataDto);
return ResponseEntity.status(HttpStatus.CREATED).body(fileMetadataDto);
public ResponseEntity<Object> uploadFile(@RequestParam("file") @Schema(type = "string", format = "binary", description = "File to upload") MultipartFile file) throws Exception {
APIResponse uploadResponse = fileService.uploadFile(file, 1L, "POST_ATTACHMENT", 1L);

return ResponseEntity.status(uploadResponse.getCode()).body(uploadResponse.getData());
}
}

0 comments on commit efeabe2

Please sign in to comment.