[ADD] TrainingScript 및 Dataset 관리 기능 추가 (Controller) and API endpoints 구현, MinIO 업로드 로직 수정 및 관련 설명 업데이트
parent
7d87b8a029
commit
fcd4f6b1f6
@ -0,0 +1,134 @@
|
|||||||
|
package kr.re.etri.autoflow.controllers;
|
||||||
|
|
||||||
|
import io.minio.*;
|
||||||
|
import io.minio.messages.Item;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
|
import io.swagger.v3.oas.annotations.enums.ParameterIn;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import kr.re.etri.autoflow.entity.DatasetAttachmentEntity;
|
||||||
|
import kr.re.etri.autoflow.repository.DatasetAttachmentRepository;
|
||||||
|
import kr.re.etri.autoflow.service.DataGroupService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/dataset")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
@Tag(name = "데이터셋 업로드", description = "MinIO 버킷/파일 관리 API")
|
||||||
|
public class DatasetController {
|
||||||
|
private final MinioClient minioClient;
|
||||||
|
private final DatasetAttachmentRepository datasetAttachmentRepository;
|
||||||
|
private final DataGroupService dataGroupService;
|
||||||
|
|
||||||
|
|
||||||
|
@Value("${minio.bucket}")
|
||||||
|
private String bucketName;
|
||||||
|
|
||||||
|
@Value("${minio.endpoint}")
|
||||||
|
private String minioEndpoint;
|
||||||
|
|
||||||
|
@Operation(summary = "파일 업로드", description = "MultipartFile을 MinIO 버킷에 업로드하고 DB에 기록합니다.")
|
||||||
|
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||||
|
public ResponseEntity<Map<String, Object>> uploadFile(
|
||||||
|
@Parameter(description = "업로드할 파일")
|
||||||
|
@RequestPart("file") MultipartFile file,
|
||||||
|
@Parameter(description = "저장 경로 (선택)")
|
||||||
|
@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,
|
||||||
|
@RequestParam(value = "refId") Long refId
|
||||||
|
) {
|
||||||
|
try (InputStream is = file.getInputStream()) {
|
||||||
|
String storedName = UUID.randomUUID() + "-" + file.getOriginalFilename();
|
||||||
|
String objectName = (path == null || path.isEmpty())
|
||||||
|
? storedName
|
||||||
|
: path + "/" + storedName;
|
||||||
|
|
||||||
|
// MinIO 업로드
|
||||||
|
minioClient.putObject(
|
||||||
|
PutObjectArgs.builder()
|
||||||
|
.bucket(bucketName)
|
||||||
|
.object(objectName)
|
||||||
|
.stream(is, is.available(), -1)
|
||||||
|
.contentType(file.getContentType())
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
.refId(refId)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
DatasetAttachmentEntity saved = datasetAttachmentRepository.save(attachment);
|
||||||
|
|
||||||
|
// MinIO URL 생성
|
||||||
|
String minioUrl = String.format("%s/%s/%s", minioEndpoint, bucketName, objectName);
|
||||||
|
|
||||||
|
Map<String, Object> response = new HashMap<>();
|
||||||
|
response.put("attachment", saved);
|
||||||
|
response.put("minioUrl", minioUrl);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(response);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("파일 업로드 실패", e);
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "데이터셋 삭제")
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Void> deleteDataset(
|
||||||
|
@Parameter(description = "삭제할 데이터셋 ID", required = true, in = ParameterIn.PATH)
|
||||||
|
@PathVariable("id") Long id) {
|
||||||
|
try {
|
||||||
|
// 1. DB에서 데이터셋 조회
|
||||||
|
Optional<DatasetAttachmentEntity> optional = datasetAttachmentRepository.findById(id);
|
||||||
|
if (optional.isEmpty()) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
DatasetAttachmentEntity attachment = optional.get();
|
||||||
|
|
||||||
|
// 2. MinIO에서 실제 파일 삭제
|
||||||
|
minioClient.removeObject(
|
||||||
|
RemoveObjectArgs.builder()
|
||||||
|
.bucket(bucketName)
|
||||||
|
.object(attachment.getStoragePath())
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
|
||||||
|
// 3. DB 레코드 삭제
|
||||||
|
datasetAttachmentRepository.deleteById(id);
|
||||||
|
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("데이터셋 삭제 실패", e);
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue