From 9b3c83fc9a7fbc943d29d2ee85a797ff46ac6de1 Mon Sep 17 00:00:00 2001 From: bjkim Date: Wed, 17 Sep 2025 18:31:26 +0900 Subject: [PATCH] =?UTF-8?q?[UPDATE]=20=ED=8C=8C=EC=9D=BC=20=EC=97=85?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=20=EB=A1=9C=EC=A7=81=20=EA=B0=9C=EC=84=A0=20?= =?UTF-8?q?=EB=B0=8F=20DB=20=EB=A9=94=ED=83=80=EB=8D=B0=EC=9D=B4=ED=84=B0?= =?UTF-8?q?=20=EC=A0=80=EC=9E=A5=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../autoflow/controllers/MinIOController.java | 50 ++++++++++++++----- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/src/main/java/kr/re/etri/autoflow/controllers/MinIOController.java b/src/main/java/kr/re/etri/autoflow/controllers/MinIOController.java index 7ecb6e6..1c1e353 100644 --- a/src/main/java/kr/re/etri/autoflow/controllers/MinIOController.java +++ b/src/main/java/kr/re/etri/autoflow/controllers/MinIOController.java @@ -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) - public List uploadMultipleFiles( + public ResponseEntity> uploadFiles( @Parameter(description = "업로드할 파일들") - @RequestPart("files") MultipartFile[] files, + @RequestPart("files") List files, @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 uploadedUrls = new ArrayList<>(); + List savedAttachments = new ArrayList<>(); + for (MultipartFile file : files) { try (InputStream is = file.getInputStream()) { + String storedName = UUID.randomUUID() + "-" + file.getOriginalFilename(); String objectName = (path == null || path.isEmpty()) - ? file.getOriginalFilename() - : path + "/" + file.getOriginalFilename(); + ? storedName + : path + "/" + storedName; + // MinIO 업로드 minioClient.putObject( PutObjectArgs.builder() .bucket(bucketName) @@ -150,18 +157,37 @@ public class MinIOController { .build() ); - String minioUrl = String.format("%s/%s/%s", minioEndpoint, bucketName, objectName); - uploadedUrls.add(minioUrl); + // DB에 저장 + 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) { - log.error("파일 업로드 실패: {}", file.getOriginalFilename(), e); - uploadedUrls.add("업로드 실패: " + file.getOriginalFilename() + " (" + e.getMessage() + ")"); + log.error("파일 업로드 실패: " + file.getOriginalFilename(), e); } } - return uploadedUrls; + + if (savedAttachments.isEmpty()) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + + return ResponseEntity.ok(savedAttachments); } + @Operation(summary = "파일 다운로드", description = "MinIO에서 파일을 다운로드합니다.") @GetMapping("/download") public ResponseEntity downloadFile(@RequestParam String objectName) {