From 761cd3f6c8416dce381eb9f99461c8d7addddc69 Mon Sep 17 00:00:00 2001 From: pbccc Date: Wed, 28 Aug 2024 09:15:07 +0900 Subject: [PATCH 1/2] repository edit --- .../component/CommonComponentController.java | 17 +++++-- .../oss/component/CommonUploadComponent.java | 25 +++++++++- .../oss/component/CommonComponentService.java | 3 ++ .../CommonModuleComponentService.java | 6 +++ .../nexus/NexusComponentAdapterService.java | 13 ++--- .../nexus/NexusComponentService.java | 48 ++++++++++++++----- 6 files changed, 88 insertions(+), 24 deletions(-) diff --git a/src/main/java/kr/co/mcmp/api/oss/component/CommonComponentController.java b/src/main/java/kr/co/mcmp/api/oss/component/CommonComponentController.java index c2ec997..27d622a 100644 --- a/src/main/java/kr/co/mcmp/api/oss/component/CommonComponentController.java +++ b/src/main/java/kr/co/mcmp/api/oss/component/CommonComponentController.java @@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import kr.co.mcmp.dto.oss.component.CommonComponent; +import kr.co.mcmp.dto.oss.component.CommonUploadComponent; import kr.co.mcmp.response.ResponseWrapper; import kr.co.mcmp.service.oss.component.CommonModuleComponentService; import lombok.RequiredArgsConstructor; @@ -11,6 +12,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import javax.validation.Valid; import java.util.List; @Tag(name = "CommonComponentController - 컴포넌트 API 관련") @@ -53,10 +55,19 @@ public ResponseEntity> deleteComponent( public ResponseEntity> createComponent( @Parameter(description = "모듈 타입", required = true, example = "nexus") @PathVariable("module") String module, @Parameter(description = "레포지토리 이름", required = true) @PathVariable("name") String name, - @RequestPart(value = "directory", required = false) String directory, - @RequestPart(value = "asset", required = false) List files - ) { + @RequestPart(value = "directory") String directory, + @RequestPart(value = "assets", required = false) List files) { moduleComponentService.createComponent(module, name, directory, files); return ResponseEntity.ok(new ResponseWrapper<>("Component create completed")); } + + @Operation(summary = "컴포넌트 등록 - 텍스트") + @PostMapping("/{module}/create/{name}/text") + public ResponseEntity> createComponentByText( + @Parameter(description = "모듈 타입", required = true, example = "nexus") @PathVariable("module") String module, + @Parameter(description = "레포지토리 이름", required = true) @PathVariable("name") String name, + @RequestBody @Valid CommonUploadComponent.TextComponentDto textComponent) { + moduleComponentService.createComponentByText(module, name, textComponent); + return ResponseEntity.ok(new ResponseWrapper<>("Component create completed")); + } } diff --git a/src/main/java/kr/co/mcmp/dto/oss/component/CommonUploadComponent.java b/src/main/java/kr/co/mcmp/dto/oss/component/CommonUploadComponent.java index f1a56fa..44e64cf 100644 --- a/src/main/java/kr/co/mcmp/dto/oss/component/CommonUploadComponent.java +++ b/src/main/java/kr/co/mcmp/dto/oss/component/CommonUploadComponent.java @@ -23,7 +23,7 @@ public class CommonUploadComponent { @Schema(title = "파일", required = true) @Valid - private List asset; + private List assets; @Getter @Builder @@ -37,4 +37,27 @@ public static class FilesDto { @Schema(title = "파일 이름", example = "test") private String filename; } + + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class TextComponentDto { + + @Schema(title = "파일명", required = true, example = "test") + @NotBlank + private String filename; + + @Schema(title = "파일 확장자", required = true, example = "txt, sh, yaml") + @NotBlank + private String extension; + + @Schema(title = "파일 경로", required = true, example = "/") + @NotBlank + private String directory; + + @Schema(title = "텍스트 내용", required = true, example = "test") + @NotBlank + private String text; + } } diff --git a/src/main/java/kr/co/mcmp/service/oss/component/CommonComponentService.java b/src/main/java/kr/co/mcmp/service/oss/component/CommonComponentService.java index f0358f9..e3306f1 100644 --- a/src/main/java/kr/co/mcmp/service/oss/component/CommonComponentService.java +++ b/src/main/java/kr/co/mcmp/service/oss/component/CommonComponentService.java @@ -1,6 +1,7 @@ package kr.co.mcmp.service.oss.component; import kr.co.mcmp.dto.oss.component.CommonComponent; +import kr.co.mcmp.dto.oss.component.CommonUploadComponent; import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -14,4 +15,6 @@ public interface CommonComponentService { void deleteComponent(String id); void createComponent(String name, String directory, List files); + + void createComponentByText(String name, CommonUploadComponent.TextComponentDto textComponent); } diff --git a/src/main/java/kr/co/mcmp/service/oss/component/CommonModuleComponentService.java b/src/main/java/kr/co/mcmp/service/oss/component/CommonModuleComponentService.java index b255060..17cb635 100644 --- a/src/main/java/kr/co/mcmp/service/oss/component/CommonModuleComponentService.java +++ b/src/main/java/kr/co/mcmp/service/oss/component/CommonModuleComponentService.java @@ -1,6 +1,7 @@ package kr.co.mcmp.service.oss.component; import kr.co.mcmp.dto.oss.component.CommonComponent; +import kr.co.mcmp.dto.oss.component.CommonUploadComponent; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -33,6 +34,11 @@ public void createComponent(String module, String name, String directory, List uploadComponentMap = getUploadComponentMap(uploadComponent, format); - + CommonRepository.RepositoryDto repositoryDto = nexusRepositoryAdapterService.getRepositoryByName(name); + MultiValueMap uploadComponentMap = getUploadComponentMap(uploadComponent, repositoryDto.getFormat()); componentAdapterClient.createComponent(name, uploadComponentMap); } @@ -45,13 +42,13 @@ private static MultiValueMap getUploadComponentMap(CommonUploadC if ("raw".equals(format)) { uploadComponentMap.add(format + ".directory", uploadComponent.getDirectory()); - for (int i = 0; i < uploadComponent.getAsset().size(); i ++) { - CommonUploadComponent.FilesDto filesDto = uploadComponent.getAsset().get(i); + for (int i = 0; i < uploadComponent.getAssets().size(); i ++) { + CommonUploadComponent.FilesDto filesDto = uploadComponent.getAssets().get(i); uploadComponentMap.add(format + ".asset" + (i + 1), filesDto.getFile().getResource()); uploadComponentMap.add(format + ".asset" + (i + 1) + ".filename", filesDto.getFilename()); } } else if ("docker".equals(format) || "helm".equals(format)) { - CommonUploadComponent.FilesDto filesDto = uploadComponent.getAsset().get(0); + CommonUploadComponent.FilesDto filesDto = uploadComponent.getAssets().get(0); uploadComponentMap.add(format + ".asset", filesDto.getFile().getResource()); } return uploadComponentMap; diff --git a/src/main/java/kr/co/mcmp/service/oss/component/nexus/NexusComponentService.java b/src/main/java/kr/co/mcmp/service/oss/component/nexus/NexusComponentService.java index 6125bd7..0e3c555 100644 --- a/src/main/java/kr/co/mcmp/service/oss/component/nexus/NexusComponentService.java +++ b/src/main/java/kr/co/mcmp/service/oss/component/nexus/NexusComponentService.java @@ -3,13 +3,18 @@ import kr.co.mcmp.dto.oss.component.CommonComponent; import kr.co.mcmp.dto.oss.component.CommonUploadComponent; import kr.co.mcmp.service.oss.component.CommonComponentService; +import kr.co.mcmp.util.FileUtil; import lombok.RequiredArgsConstructor; +import lombok.extern.log4j.Log4j2; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import java.util.ArrayList; +import java.io.IOException; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; +@Log4j2 @Service @RequiredArgsConstructor public class NexusComponentService implements CommonComponentService { @@ -34,23 +39,42 @@ public void deleteComponent(String id) { @Override public void createComponent(String name, String directory, List files) { - List uploadFileList = new ArrayList<>(); - - for (MultipartFile file : files) { - CommonUploadComponent.FilesDto filesDto = CommonUploadComponent.FilesDto.builder() - .file(file) - .filename(file.getOriginalFilename()) - .build(); - - uploadFileList.add(filesDto); - } + List uploadFiles = files.stream() + .map(this::convertToFileDto) + .collect(Collectors.toList()); CommonUploadComponent uploadComponent = CommonUploadComponent.builder() .directory(directory) - .asset(uploadFileList) + .assets(uploadFiles) .build(); componentAdapterService.createComponent(name, uploadComponent); } + + @Override + public void createComponentByText(String name, CommonUploadComponent.TextComponentDto textComponent) { + try { + MultipartFile file = FileUtil.generatedMultipartFile(textComponent); + + CommonUploadComponent.FilesDto filesDto = convertToFileDto(file); + List uploadFiles = Collections.singletonList(filesDto); + + CommonUploadComponent uploadComponent = CommonUploadComponent.builder() + .directory(textComponent.getDirectory()) + .assets(uploadFiles) + .build(); + + componentAdapterService.createComponent(name, uploadComponent); + } catch (IOException e) { + log.error(e.getMessage()); + } + } + + private CommonUploadComponent.FilesDto convertToFileDto(MultipartFile file) { + return CommonUploadComponent.FilesDto.builder() + .file(file) + .filename(file.getOriginalFilename()) + .build(); + } } From 65217df80711fd9afbfd60d8bcc498dcecf12c62 Mon Sep 17 00:00:00 2001 From: pbccc Date: Wed, 28 Aug 2024 09:15:33 +0900 Subject: [PATCH 2/2] repository edit --- src/main/java/kr/co/mcmp/util/FileUtil.java | 49 +++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/kr/co/mcmp/util/FileUtil.java diff --git a/src/main/java/kr/co/mcmp/util/FileUtil.java b/src/main/java/kr/co/mcmp/util/FileUtil.java new file mode 100644 index 0000000..e77ce2b --- /dev/null +++ b/src/main/java/kr/co/mcmp/util/FileUtil.java @@ -0,0 +1,49 @@ +package kr.co.mcmp.util; + +import kr.co.mcmp.dto.oss.component.CommonUploadComponent; +import org.apache.commons.fileupload.FileItem; +import org.apache.commons.fileupload.disk.DiskFileItem; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.commons.CommonsMultipartFile; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +public class FileUtil { + + public static MultipartFile generatedMultipartFile(CommonUploadComponent.TextComponentDto textComponent) throws IOException { + byte[] content = textComponent.getText().getBytes(); + String fileName = textComponent.getFilename() + "." + textComponent.getExtension(); + String mimeType = getMimeType(textComponent.getExtension()); + + FileItem fileItem = new DiskFileItem( + "file", + mimeType, + false, + fileName, + content.length, + null + ); + + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(content)) { + fileItem.getOutputStream().write(inputStream.readAllBytes()); + } + + return new CommonsMultipartFile(fileItem); + } + + private static String getMimeType(String extension) { + switch (extension.toLowerCase()) { + case "txt": + return "text/plain"; + case "html": + return "text/html"; + case "sh": + return "application/x-sh"; + case "yaml": + return "text/x-yaml"; + default: + return "application/octet-stream"; + } + } +}