From 48746a113ca1d7a330f256fbcfbb6e8bc6d5f53f Mon Sep 17 00:00:00 2001 From: bjkim Date: Thu, 18 Sep 2025 19:57:23 +0900 Subject: [PATCH] =?UTF-8?q?[ADD]=20projectId=20=ED=95=84=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EB=B0=8F=20Kubeflow=20=EC=97=85=EB=A1=9C?= =?UTF-8?q?=EB=93=9C=20API=20=ED=99=9C=EC=84=B1=ED=99=94=20(Service,=20Con?= =?UTF-8?q?troller)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MinioAttachmentController.java | 100 ++++++++++-------- .../service/MinioAttachmentService.java | 6 +- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/src/main/java/kr/re/etri/autoflow/controllers/MinioAttachmentController.java b/src/main/java/kr/re/etri/autoflow/controllers/MinioAttachmentController.java index e5113ae..dbf8a54 100644 --- a/src/main/java/kr/re/etri/autoflow/controllers/MinioAttachmentController.java +++ b/src/main/java/kr/re/etri/autoflow/controllers/MinioAttachmentController.java @@ -14,7 +14,10 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.InputStreamResource; import org.springframework.data.domain.Page; import org.springframework.http.*; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; @@ -83,11 +86,13 @@ public class MinioAttachmentController { @RequestParam(value = "title", required = false) String title, @RequestParam(value = "description", required = false) String description, @RequestParam(value = "version", required = false, defaultValue = "1") Integer version, - @RequestParam(value = "regUserId") String regUserId + @RequestParam(value = "regUserId") String regUserId, + @RequestParam(value = "projectId") Long projectId + ) { try { MinioAttachmentEntity saved = minioAttachmentService.uploadFile( - file, path, refId, refType, title, description, version, regUserId + file, path, refId, refType, title, description, version, regUserId, projectId ); Map response = new HashMap<>(); @@ -102,51 +107,52 @@ public class MinioAttachmentController { } } -// @Operation(summary = "파일 업로드 및 Kubeflow 업로드", -// description = "MinIO와 DB에 저장하고 Kubeflow REST API에도 업로드합니다. Kubeflow API 경로는 요청 시 지정 가능") -// @PostMapping(value = "/upload-and-kubeflow", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) -// public ResponseEntity> uploadFileAndKubeflow( -// @Parameter(description = "업로드할 파일") @RequestPart("file") MultipartFile file, -// @RequestPart(value = "path", required = false) String path, -// @RequestParam(value = "refId", required = false) Long refId, -// @RequestParam(value = "refType", required = false, defaultValue = "TRAINING_SCRIPT") String refType, -// @RequestParam(value = "title", required = false) String title, -// @RequestParam(value = "description", required = false) String description, -// @RequestParam(value = "version", required = false, defaultValue = "1") Integer version, -// @RequestParam(value = "regUserId") String regUserId, -// @RequestParam(value = "kubeflowApi") String kubeflowApi -// ) { -// Map response = new HashMap<>(); -// try { -// MinioAttachmentEntity saved = minioAttachmentService.uploadFile( -// file, path, refId, refType, title, description, version, regUserId -// ); -// String minioUrl = minioAttachmentService.getFileUrl(saved.getStoragePath()); -// response.put("attachment", saved); -// response.put("minioUrl", minioUrl); -// -// RestTemplate restTemplate = new RestTemplate(); -// MultiValueMap body = new LinkedMultiValueMap<>(); -// body.add("uploadfile", new MultipartInputStreamFileResource(file.getInputStream(), file.getOriginalFilename())); -// -// HttpHeaders headers = new HttpHeaders(); -// headers.setContentType(MediaType.MULTIPART_FORM_DATA); -// -// HttpEntity> requestEntity = new HttpEntity<>(body, headers); -// -// String fullUrl = kubeflowBaseUrl + kubeflowApi; -// ResponseEntity kubeflowResponse = restTemplate.postForEntity(fullUrl, requestEntity, String.class); -// -// response.put("kubeflowResponse", kubeflowResponse.getBody()); -// -// return ResponseEntity.ok(response); -// -// } catch (Exception e) { -// log.error("파일 업로드 실패", e); -// response.put("error", e.getMessage()); -// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); -// } -// } + @Operation(summary = "파일 업로드 및 Kubeflow 업로드", + description = "MinIO와 DB에 저장하고 Kubeflow REST API에도 업로드합니다. Kubeflow API 경로는 요청 시 지정 가능") + @PostMapping(value = "/upload-and-kubeflow", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity> uploadFileAndKubeflow( + @Parameter(description = "업로드할 파일") @RequestPart("file") MultipartFile file, + @RequestPart(value = "path", required = false) String path, + @RequestParam(value = "refId", required = false) Long refId, + @RequestParam(value = "refType", required = false, defaultValue = "TRAINING_SCRIPT") String refType, + @RequestParam(value = "title", required = false) String title, + @RequestParam(value = "description", required = false) String description, + @RequestParam(value = "version", required = false, defaultValue = "1") Integer version, + @RequestParam(value = "regUserId") String regUserId, + @RequestParam(value = "projectId") Long projectId, + @RequestParam(value = "kubeflowApi") String kubeflowApi + ) { + Map response = new HashMap<>(); + try { + MinioAttachmentEntity saved = minioAttachmentService.uploadFile( + file, path, refId, refType, title, description, version, regUserId, projectId + ); + String minioUrl = minioAttachmentService.getFileUrl(saved.getStoragePath()); + response.put("attachment", saved); + response.put("minioUrl", minioUrl); + + RestTemplate restTemplate = new RestTemplate(); + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("uploadfile", new MultipartInputStreamFileResource(file.getInputStream(), file.getOriginalFilename())); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + + String fullUrl = kubeflowBaseUrl + kubeflowApi; + ResponseEntity kubeflowResponse = restTemplate.postForEntity(fullUrl, requestEntity, String.class); + + response.put("kubeflowResponse", kubeflowResponse.getBody()); + + return ResponseEntity.ok(response); + + } catch (Exception e) { + log.error("파일 업로드 실패", e); + response.put("error", e.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response); + } + } // 파일 업로드용 InputStreamResource private static class MultipartInputStreamFileResource extends InputStreamResource { diff --git a/src/main/java/kr/re/etri/autoflow/service/MinioAttachmentService.java b/src/main/java/kr/re/etri/autoflow/service/MinioAttachmentService.java index c6b27ff..89b5e86 100644 --- a/src/main/java/kr/re/etri/autoflow/service/MinioAttachmentService.java +++ b/src/main/java/kr/re/etri/autoflow/service/MinioAttachmentService.java @@ -53,7 +53,10 @@ public class MinioAttachmentService { String title, String description, Integer version, - String regUserId) throws Exception { + String regUserId, + Long projectId + ) throws Exception { + try (InputStream is = file.getInputStream()) { String storedName = UUID.randomUUID() + "-" + file.getOriginalFilename(); String objectName = (path == null || path.isEmpty()) @@ -83,6 +86,7 @@ public class MinioAttachmentService { .version(version) .description(description) .regUserId(regUserId) + .projectId(projectId) .build(); return minioAttachmentRepository.save(attachment);