[UPDATE] 파일 업로드 로직 개선 및 DB 메타데이터 저장 로직 추가

main
bjkim 9 months ago
parent 59043ee678
commit 9b3c83fc9a

@ -126,21 +126,28 @@ public class MinIOController {
} }
@Operation(summary = "다중 파일 업로드", description = "MultipartFile 배열을 MinIO 버킷에 업로드합니다. path를 지정하면 하위 폴더에 저장 가능합니다.") @Operation(summary = "다중 파일 업로드", description = "MultipartFile들을 MinIO 버킷에 업로드하고 DB에 기록합니다.")
@PostMapping(value = "/upload-multiple", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PostMapping(value = "/upload-multiple", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public List<String> uploadMultipleFiles( public ResponseEntity<List<DatasetAttachmentEntity>> uploadFiles(
@Parameter(description = "업로드할 파일들") @Parameter(description = "업로드할 파일들")
@RequestPart("files") MultipartFile[] files, @RequestPart("files") List<MultipartFile> files,
@Parameter(description = "저장 경로 (선택)") @Parameter(description = "저장 경로 (선택)")
@RequestPart(value = "path", required = false) String path @RequestPart(value = "path", required = false) String path,
@RequestParam(value = "title", required = false, defaultValue = "배터리 퍼센트 데이터 셋") String title,
@RequestParam(value = "description", required = false, defaultValue = "배터리 퍼센트 데이터 모음") String description,
@RequestParam(value = "version", required = false, defaultValue = "1") Integer version,
@RequestParam(value = "regUserId") String regUserId
) { ) {
List<String> uploadedUrls = new ArrayList<>(); List<DatasetAttachmentEntity> savedAttachments = new ArrayList<>();
for (MultipartFile file : files) { for (MultipartFile file : files) {
try (InputStream is = file.getInputStream()) { try (InputStream is = file.getInputStream()) {
String storedName = UUID.randomUUID() + "-" + file.getOriginalFilename();
String objectName = (path == null || path.isEmpty()) String objectName = (path == null || path.isEmpty())
? file.getOriginalFilename() ? storedName
: path + "/" + file.getOriginalFilename(); : path + "/" + storedName;
// MinIO 업로드
minioClient.putObject( minioClient.putObject(
PutObjectArgs.builder() PutObjectArgs.builder()
.bucket(bucketName) .bucket(bucketName)
@ -150,17 +157,36 @@ public class MinIOController {
.build() .build()
); );
String minioUrl = String.format("%s/%s/%s", minioEndpoint, bucketName, objectName); // DB에 저장
uploadedUrls.add(minioUrl); DatasetAttachmentEntity attachment = DatasetAttachmentEntity.builder()
.originalName(file.getOriginalFilename())
.storedName(storedName)
.contentType(file.getContentType())
.size(file.getSize())
.storagePath(objectName)
.title(title != null ? title : file.getOriginalFilename())
.version(version)
.description(description)
.regUserId(regUserId)
.regDt(LocalDateTime.now())
.build();
DatasetAttachmentEntity saved = datasetAttachmentRepository.save(attachment);
savedAttachments.add(saved);
} catch (Exception e) { } catch (Exception e) {
log.error("파일 업로드 실패: {}", file.getOriginalFilename(), e); log.error("파일 업로드 실패: " + file.getOriginalFilename(), e);
uploadedUrls.add("업로드 실패: " + file.getOriginalFilename() + " (" + e.getMessage() + ")");
} }
} }
return uploadedUrls;
if (savedAttachments.isEmpty()) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
} }
return ResponseEntity.ok(savedAttachments);
}
@Operation(summary = "파일 다운로드", description = "MinIO에서 파일을 다운로드합니다.") @Operation(summary = "파일 다운로드", description = "MinIO에서 파일을 다운로드합니다.")
@GetMapping("/download") @GetMapping("/download")

Loading…
Cancel
Save