[ADD] 파일 업데이트 기능 추가 (Service, Controller, Repository) 및 버전 관리 로직 구현

main
bjkim 9 months ago
parent 676866db84
commit 6c44c47705

@ -3,7 +3,6 @@ package kr.re.etri.autoflow.controllers;
import io.minio.*; import io.minio.*;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import kr.re.etri.autoflow.entity.MinioAttachmentEntity; import kr.re.etri.autoflow.entity.MinioAttachmentEntity;
import kr.re.etri.autoflow.payload.request.BaseSearchRequest; import kr.re.etri.autoflow.payload.request.BaseSearchRequest;
@ -12,7 +11,6 @@ import kr.re.etri.autoflow.service.MinioAttachmentService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject; import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -96,4 +94,33 @@ public class AttachmentController {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
} }
} }
@Operation(summary = "파일 업데이트", description = "파일을 새 버전으로 업로드합니다. 기존 파일은 그대로 보존되고, 버전은 +1 증가합니다.")
@PutMapping(value = "/{id}/update", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Map<String, Object>> updateFile(
@Parameter(description = "기존 첨부파일 ID", required = true)
@PathVariable("id") Long id,
@Parameter(description = "새 파일") @RequestPart("file") MultipartFile file,
@RequestPart(value = "path", required = false) String path,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "description", required = false) String description,
@RequestParam(value = "regUserId") String regUserId
) {
try {
MinioAttachmentEntity updated = minioAttachmentService.updateFile(
id, file, path, title, description, regUserId
);
Map<String, Object> response = new HashMap<>();
response.put("attachment", updated);
response.put("minioUrl", minioAttachmentService.getFileUrl(updated.getStoragePath()));
return ResponseEntity.ok(response);
} catch (Exception e) {
log.error("파일 업데이트 실패", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
} }

@ -6,8 +6,10 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
import java.util.Optional;
@Repository @Repository
public interface MinioAttachmentRepository extends JpaRepository<MinioAttachmentEntity, Long>, JpaSpecificationExecutor<MinioAttachmentEntity> { public interface MinioAttachmentRepository extends JpaRepository<MinioAttachmentEntity, Long>, JpaSpecificationExecutor<MinioAttachmentEntity> {
//최신버전 파일 가져오기
Optional<MinioAttachmentEntity> findTopByRefIdAndRefTypeOrderByVersionDesc(Long refId, String refType);
} }

@ -91,7 +91,6 @@ public class MinioAttachmentService {
/** /**
* *
*/ */
@Transactional
public List<MinioAttachmentEntity> findAll() { public List<MinioAttachmentEntity> findAll() {
return minioAttachmentRepository.findAll(); return minioAttachmentRepository.findAll();
} }
@ -99,7 +98,6 @@ public class MinioAttachmentService {
/** /**
* ID * ID
*/ */
@Transactional
public Optional<MinioAttachmentEntity> findById(Long id) { public Optional<MinioAttachmentEntity> findById(Long id) {
return minioAttachmentRepository.findById(id); return minioAttachmentRepository.findById(id);
} }
@ -141,10 +139,65 @@ public class MinioAttachmentService {
} }
} }
public MinioAttachmentEntity updateFile(
Long id,
MultipartFile file,
String path,
String title,
String description,
String regUserId
) throws Exception {
// 기존 엔티티 조회
MinioAttachmentEntity existing = minioAttachmentRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("첨부파일을 찾을 수 없습니다. ID=" + id));
// 최신 버전 조회
Integer latestVersion = minioAttachmentRepository
.findTopByRefIdAndRefTypeOrderByVersionDesc(existing.getRefId(), existing.getRefType())
.map(MinioAttachmentEntity::getVersion)
.orElse(0);
int newVersion = latestVersion + 1;
// 새 파일 업로드
String storedName = UUID.randomUUID() + "-" + file.getOriginalFilename();
String objectName = (path == null || path.isEmpty())
? storedName
: path + "/" + storedName;
try (InputStream is = file.getInputStream()) {
minioClient.putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.stream(is, is.available(), -1)
.contentType(file.getContentType())
.build()
);
}
// 새로운 엔티티 생성 (이전 데이터는 그대로 두고, 새로운 버전 생성)
MinioAttachmentEntity newAttachment = MinioAttachmentEntity.builder()
.refId(existing.getRefId())
.refType(existing.getRefType())
.originalName(file.getOriginalFilename())
.storedName(storedName)
.contentType(file.getContentType())
.size(file.getSize())
.storagePath(objectName)
.title(title != null ? title : existing.getTitle())
.description(description != null ? description : existing.getDescription())
.version(newVersion)
.regUserId(regUserId)
.build();
return minioAttachmentRepository.save(newAttachment);
}
/** /**
* (DB , ) * (DB , )
*/ */
@Transactional
public MinioAttachmentEntity create(MinioAttachmentEntity entity) { public MinioAttachmentEntity create(MinioAttachmentEntity entity) {
return minioAttachmentRepository.save(entity); return minioAttachmentRepository.save(entity);
} }
@ -152,7 +205,6 @@ public class MinioAttachmentService {
/** /**
* *
*/ */
@Transactional
public Optional<MinioAttachmentEntity> update(Long id, MinioAttachmentEntity dto) { public Optional<MinioAttachmentEntity> update(Long id, MinioAttachmentEntity dto) {
return minioAttachmentRepository.findById(id) return minioAttachmentRepository.findById(id)
.map(entity -> { .map(entity -> {
@ -164,7 +216,6 @@ public class MinioAttachmentService {
/** /**
* *
*/ */
@Transactional
public boolean delete(Long id) { public boolean delete(Long id) {
if (!minioAttachmentRepository.existsById(id)) { if (!minioAttachmentRepository.existsById(id)) {
return false; return false;

Loading…
Cancel
Save