Skip to content

Commit

Permalink
Merge pull request #7 from pbcccBeatBoard/main
Browse files Browse the repository at this point in the history
repository edit
  • Loading branch information
pbcccbeatboard-strato authored Aug 28, 2024
2 parents b4bcc46 + 65217df commit e56ed16
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@Tag(name = "CommonComponentController - 컴포넌트 API 관련")
Expand Down Expand Up @@ -53,10 +55,19 @@ public ResponseEntity<ResponseWrapper<String>> deleteComponent(
public ResponseEntity<ResponseWrapper<String>> 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<MultipartFile> files
) {
@RequestPart(value = "directory") String directory,
@RequestPart(value = "assets", required = false) List<MultipartFile> files) {
moduleComponentService.createComponent(module, name, directory, files);
return ResponseEntity.ok(new ResponseWrapper<>("Component create completed"));
}

@Operation(summary = "컴포넌트 등록 - 텍스트")
@PostMapping("/{module}/create/{name}/text")
public ResponseEntity<ResponseWrapper<String>> 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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CommonUploadComponent {

@Schema(title = "파일", required = true)
@Valid
private List<FilesDto> asset;
private List<FilesDto> assets;

@Getter
@Builder
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,4 +15,6 @@ public interface CommonComponentService {
void deleteComponent(String id);

void createComponent(String name, String directory, List<MultipartFile> files);

void createComponentByText(String name, CommonUploadComponent.TextComponentDto textComponent);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,6 +34,11 @@ public void createComponent(String module, String name, String directory, List<M
componentService.createComponent(name, directory, files);
}

public void createComponentByText(String module, String name, CommonUploadComponent.TextComponentDto textComponent) {
CommonComponentService componentService = getComponentService(module);
componentService.createComponentByText(name, textComponent);
}

private CommonComponentService getComponentService(String module) {
return componentFactory.generatedComponentService(module);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ public void deleteComponent(String id) {
}

public void createComponent(String name, CommonUploadComponent uploadComponent) {
CommonRepository.RepositoryDto repositoryByName = nexusRepositoryAdapterService.getRepositoryByName(name);
String format = repositoryByName.getFormat();

MultiValueMap<String, Object> uploadComponentMap = getUploadComponentMap(uploadComponent, format);

CommonRepository.RepositoryDto repositoryDto = nexusRepositoryAdapterService.getRepositoryByName(name);
MultiValueMap<String, Object> uploadComponentMap = getUploadComponentMap(uploadComponent, repositoryDto.getFormat());
componentAdapterClient.createComponent(name, uploadComponentMap);
}

Expand All @@ -45,13 +42,13 @@ private static MultiValueMap<String, Object> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,23 +39,42 @@ public void deleteComponent(String id) {
@Override
public void createComponent(String name, String directory, List<MultipartFile> files) {

List<CommonUploadComponent.FilesDto> uploadFileList = new ArrayList<>();

for (MultipartFile file : files) {
CommonUploadComponent.FilesDto filesDto = CommonUploadComponent.FilesDto.builder()
.file(file)
.filename(file.getOriginalFilename())
.build();

uploadFileList.add(filesDto);
}
List<CommonUploadComponent.FilesDto> 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<CommonUploadComponent.FilesDto> 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();
}
}

49 changes: 49 additions & 0 deletions src/main/java/kr/co/mcmp/util/FileUtil.java
Original file line number Diff line number Diff line change
@@ -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";
}
}
}

0 comments on commit e56ed16

Please sign in to comment.