[MODIFY] EdgeSWVO 필드 수정 및 ToString 어노테이션 추가, 외부 Edge 패키지 등록 API 수정, 파일 업로드 로직 개선, registerWithFile API 추가

main
bjkim 8 months ago
parent d4f3620933
commit c8ceed469c

@ -27,6 +27,7 @@ import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.cert.X509Certificate;
@ -94,46 +95,111 @@ public class ExternalAuthController {
return ResponseEntity.ok(ApiResponse.failure(e.getMessage()));
}
}
// @Operation(
// summary = "S3 업로드 + 외부 DB 등록",
// description = "파일을 S3에 업로드하고 외부 DB에 등록합니다.",
// security = @SecurityRequirement(name = "bearerAuth") // 여기서 SecurityScheme 연결
// )
// @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
// public ResponseEntity<String> uploadEdgeSW(
// @RequestParam("files") List<MultipartFile> files,
// @RequestParam("sw_id") String swId,
// @RequestParam("sw_version") String swVersion,
// @RequestParam("sw_name") String swName,
// @RequestParam("creation_datetime") String creationDatetime,
// @RequestParam("auth_id") String authId
// ) {
// try {
// EdgeSWVO edgeSWVO = new EdgeSWVO();
// edgeSWVO.setFiles(files);
// edgeSWVO.setSw_id(swId);
// edgeSWVO.setSw_version(swVersion);
// edgeSWVO.setSw_name(swName);
// edgeSWVO.setCreation_datetime(creationDatetime);
// edgeSWVO.setAuth_id(authId);
//
// String result = edgeSWUploadService.uploadAndSend(edgeSWVO);
// return ResponseEntity.ok(result);
//
// } catch (Exception e) {
// return ResponseEntity.internalServerError().body("업로드 실패: " + e.getMessage());
// }
// }
@Operation(
summary = "S3 업로드 + 외부 DB 등록",
description = "파일을 S3에 업로드하고 외부 DB에 등록합니다.",
security = @SecurityRequirement(name = "bearerAuth") // 여기서 SecurityScheme 연결
summary = "외부 DB 등록 + 파일 업로드",
description = "S3 업로드된 파일 정보를 외부 DB에 등록합니다. 파일도 함께 업로드 가능",
security = @SecurityRequirement(name = "bearerAuth")
)
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> uploadEdgeSW(
@RequestParam("files") List<MultipartFile> files,
@PostMapping(value = "/register-with-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> registerWithFile(
@RequestParam("sw_id") String swId,
@RequestParam("sw_version") String swVersion,
@RequestParam("sw_version") int swVersion,
@RequestParam("sw_name") String swName,
@RequestParam("creation_datetime") String creationDatetime,
@RequestParam("auth_id") String authId
@RequestParam("auth_id") String authId,
@RequestParam
@Schema(description = "엣지 패키지 정보", example = "14", required = true)
Integer edPkgSerial,
@RequestParam
@Schema(description = "압축여부(0:단일,1:묶음)", example = "1", required = true)
Integer archiveType,
@RequestParam
@Schema(description = "비밀글 여부 (true/false)", example = "false", required = true)
String secretAt,
@RequestParam
@Schema(description = "설치 위치", example = "/etc/test/", required = true)
String downloadLocation,
@RequestParam("user_id")
@Schema(description = "사용자 ID", example = "admin", required = true) String userId,
@RequestParam("creation_datetime")
@Schema(
description = "생성 일시 (ISO-8601 형식, UTC)",
example = "2025-10-20T11:46:49.848Z",
required = true
)
String creationDatetime,
@RequestPart(value = "file", required = false) MultipartFile file
) {
try {
// VO 생성
EdgeSWVO edgeSWVO = new EdgeSWVO();
edgeSWVO.setFiles(files);
edgeSWVO.setSw_id(swId);
edgeSWVO.setSw_version(swVersion);
edgeSWVO.setSw_name(swName);
edgeSWVO.setCreation_datetime(creationDatetime);
edgeSWVO.setAuth_id(authId);
edgeSWVO.setUser_id(userId);
edgeSWVO.setCreation_datetime(creationDatetime);
edgeSWVO.setEd_pkg_serial(edPkgSerial);
edgeSWVO.setArchive_type(archiveType);
edgeSWVO.setDownload_location(downloadLocation);
String result = edgeSWUploadService.uploadAndSend(edgeSWVO);
return ResponseEntity.ok(result);
} catch (Exception e) {
return ResponseEntity.internalServerError().body("업로드 실패: " + e.getMessage());
}
}
@Operation(summary = "외부 DB 등록", description = "S3 업로드된 파일 정보를 외부 DB에 등록합니다.",
security = @SecurityRequirement(name = "bearerAuth"))
@PostMapping("/register")
public ResponseEntity<String> registerMetadata(
@RequestBody EdgeSWVO edgeSWVO
) {
try {
edgeSWVO.setSecret_at(secretAt);
// 파일이 있으면 S3 업로드
if (file != null && !file.isEmpty()) {
edgeSWUploadService.uploadFilesToS3Only(edgeSWVO, file);
}
// DB 등록 (파일 업로드 성공 후)
String result = edgeSWUploadService.registerMetadata(edgeSWVO);
return ResponseEntity.ok(result);
} catch (IOException ioe) {
log.error("S3 파일 업로드 실패", ioe);
return ResponseEntity.internalServerError().body("파일 업로드 실패: " + ioe.getMessage());
} catch (Exception e) {
log.error("DB 등록 실패", e);
return ResponseEntity.internalServerError().body("DB 등록 실패: " + e.getMessage());

@ -4,12 +4,15 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.web.multipart.MultipartFile;
import java.io.Serializable;
import java.util.List;
@Getter
@Setter
@ToString
@Schema(description = "Edge SW 등록용 데이터")
public class EdgeSWVO implements Serializable {
@ -63,5 +66,5 @@ public class EdgeSWVO implements Serializable {
@Schema(description = "파일 업로드용 리스트 (JSON 전송 시 무시)", hidden = true)
@JsonIgnore
private List<?> files;
private List<MultipartFile> files;
}

@ -34,54 +34,40 @@ public class EdgeSWUploadService {
this.restTemplate = restTemplate;
}
// public String uploadAndSend(EdgeSWVO edgeSWVO) throws IOException {
// // 1⃣ S3 업로드
// for (MultipartFile file : edgeSWVO.getFiles()) {
// File tempFile = convertMultiPartToFile(file);
// String key = file.getOriginalFilename();
// s3Client.putObject(
// PutObjectRequest.builder()
// .bucket(s3Bucket)
// .key(key)
// .build(),
// RequestBody.fromFile(tempFile)
// );
// edgeSWVO.setFile_name(file.getOriginalFilename());
// edgeSWVO.setFile_size(file.getSize());
// edgeSWVO.setFile_location(key);
// tempFile.delete();
// }
//
// // 2⃣ 외부 API 전송
// MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
// for (MultipartFile file : edgeSWVO.getFiles()) {
// File tempFile = convertMultiPartToFile(file);
// body.add("files", new FileSystemResource(tempFile));
// }
//
// // EdgeSWVO의 다른 필드들을 body에 추가
// body.add("sw_id", edgeSWVO.getSw_id());
// body.add("sw_version", edgeSWVO.getSw_version());
// body.add("sw_name", edgeSWVO.getSw_name());
// body.add("creation_datetime", edgeSWVO.getCreation_datetime());
// body.add("file_location", edgeSWVO.getFile_location());
// body.add("auth_id", edgeSWVO.getAuth_id());
//
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//
// if (edgeSWVO.getAuth_id() != null && !edgeSWVO.getAuth_id().isEmpty()) {
// headers.setBearerAuth(edgeSWVO.getAuth_id());
// }
//
// HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
//
// ResponseEntity<String> response = restTemplate.postForEntity(externalApiUrl, requestEntity, String.class);
//
// return response.getBody();
// }
// 단일 파일 업로드
public String uploadFilesToS3Only(EdgeSWVO edgeSWVO, MultipartFile file) throws IOException {
// 임시 파일 생성
File tempFile = convertMultiPartToFile(file);
// S3 경로 지정: files/edge_sw/<원본파일명>
String key = "files/edge_sw/" + file.getOriginalFilename();
// S3 업로드
s3Client.putObject(
PutObjectRequest.builder()
.bucket(s3Bucket)
.key(key)
.build(),
RequestBody.fromFile(tempFile)
);
// 실제 저장된 S3 경로를 VO에 세팅
edgeSWVO.setFile_location(key); // S3 경로
log.info("testtest"+edgeSWVO.toString());
edgeSWVO.setFile_type(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1));
edgeSWVO.setFile_name(key); // 실제 S3 경로를 파일명에도 사용
edgeSWVO.setFile_size(file.getSize());
tempFile.delete();
return key; // S3 경로 반환
}
public String registerMetadata(EdgeSWVO edgeSWVO) {
log.info("registerMetadata : " + edgeSWVO.toString());
// JSON Body 구성
Map<String, Object> jsonBody = new HashMap<>();
jsonBody.put("creation_datetime", edgeSWVO.getCreation_datetime());

Loading…
Cancel
Save