[ADD] MinIO 파일 삭제 로직 추가 및 데이터그룹 삭제 시 첨부파일 정리 처리

main
bjkim 8 months ago
parent f1a98f858c
commit 2122aaf91b

@ -30,6 +30,11 @@ public class DataGroupEntity {
@Comment("ID")
private Long id;
// @Schema(description = "첨부파일 종류 (DATASET / SCRIPT)", example = "DATASET")
// @Comment("첨부파일 종류")
// @Column(nullable = false, length = 50)
// private String refType; // 구분자 (예: WORKFLOW_STEP,DATASET, TRAINING_SCRIPT)
@Schema(description = "데이터셋 이름", example = "배터리 상태 데이터 셋")
@Comment("데이터셋 이름")
private String dsNm;

@ -12,4 +12,5 @@ import java.util.Optional;
public interface MinioAttachmentRepository extends JpaRepository<MinioAttachmentEntity, Long>, JpaSpecificationExecutor<MinioAttachmentEntity> {
//최신버전 파일 가져오기
Optional<MinioAttachmentEntity> findTopByRefIdAndRefTypeOrderByVersionDesc(Long refId, String refType);
List<MinioAttachmentEntity> findAllByRefId(Long refId);
}

@ -1,12 +1,18 @@
package kr.re.etri.autoflow.service;
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import kr.re.etri.autoflow.entity.DataGroupEntity;
import kr.re.etri.autoflow.entity.MinioAttachmentEntity;
import kr.re.etri.autoflow.payload.request.ProjectBaseSearchRequest;
import kr.re.etri.autoflow.payload.request.ProjectRequest;
import kr.re.etri.autoflow.repository.DataGroupRepository;
import kr.re.etri.autoflow.repository.MinioAttachmentRepository;
import kr.re.etri.autoflow.specification.DataGroupSpecification;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
@ -24,10 +30,13 @@ import java.util.Optional;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class DataGroupService {
private final DataGroupRepository dataGroupRepository;
private final MinioAttachmentRepository minioAttachmentRepository;
private final DataGroupSpecification dataGroupSpecification;
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
@ -40,6 +49,12 @@ public class DataGroupService {
return dataGroupRepository.findById(id);
}
private final MinioClient minioClient;
@Value("${minio.bucket}")
private String bucketName;
public Page<DataGroupEntity> search(ProjectBaseSearchRequest request) {
int pageIndex = request.getPage() > 0 ? request.getPage() - 1 : 0;
@ -93,11 +108,41 @@ public class DataGroupService {
}
@Transactional
public boolean delete(Long id) {
if (!dataGroupRepository.existsById(id)) {
public boolean delete(Long dataGroupId) {
try {
// 1. 데이터그룹 존재 여부 확인
Optional<DataGroupEntity> dataGroupOpt = dataGroupRepository.findById(dataGroupId);
if (dataGroupOpt.isEmpty()) {
return false;
}
// 2. refId 기준으로 MinIO 첨부파일 조회
List<MinioAttachmentEntity> attachments =
minioAttachmentRepository.findAllByRefId(dataGroupId);
// 3. MinIO에서 파일 삭제
for (MinioAttachmentEntity attachment : attachments) {
try {
minioClient.removeObject(
RemoveObjectArgs.builder()
.bucket(bucketName)
.object(attachment.getStoragePath())
.build()
);
// DB에서도 첨부파일 삭제
minioAttachmentRepository.delete(attachment);
} catch (Exception e) {
log.error("MinIO 파일 삭제 실패: {}", attachment.getStoragePath(), e);
}
}
// 4. 데이터그룹 삭제
dataGroupRepository.delete(dataGroupOpt.get());
return true;
} catch (Exception e) {
log.error("데이터그룹 삭제 중 오류 발생 (id={})", dataGroupId, e);
return false;
}
dataGroupRepository.deleteById(id);
return true;
}
}

Loading…
Cancel
Save