TIF 파일 유효성 검증 및 중복 파일 삭제 기능 적용

This commit is contained in:
DanielLee
2025-12-12 15:03:37 +09:00
parent 9bf84e109d
commit ac3bdcb431
2 changed files with 66 additions and 0 deletions

View File

@@ -15,10 +15,14 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@Tag(name = "영상 관리", description = "영상 관리 API") @Tag(name = "영상 관리", description = "영상 관리 API")
@RestController @RestController
@@ -67,6 +71,20 @@ public class MapSheetMngFileCheckerApiController {
return ApiResponseDto.createOK(mapSheetMngFileCheckerService.getFilesAll(srchDto)); return ApiResponseDto.createOK(mapSheetMngFileCheckerService.getFilesAll(srchDto));
} }
@Operation(summary = "파일 업로드", description = "파일 업로드 및 TIF 검증")
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ApiResponseDto<String> uploadFile(
@RequestPart("file") MultipartFile file,
@RequestParam("targetPath") String targetPath) {
return ApiResponseDto.createOK(mapSheetMngFileCheckerService.uploadFile(file, targetPath));
}
@Operation(summary = "파일 삭제", description = "중복 파일 등 파일 삭제")
@PostMapping("/delete")
public ApiResponseDto<Boolean> deleteFile(@RequestBody SrchFoldersDto dto) {
return ApiResponseDto.createOK(mapSheetMngFileCheckerService.deleteFile(dto.getDirPath()));
}
/* /*
@Operation(summary = "지정폴더(하위폴더포함) 파일목록 조회", description = "지정폴더(하위폴더포함) 파일목록 조회") @Operation(summary = "지정폴더(하위폴더포함) 파일목록 조회", description = "지정폴더(하위폴더포함) 파일목록 조회")
@ApiResponses( @ApiResponses(

View File

@@ -2,6 +2,7 @@ package com.kamco.cd.kamcoback.mapsheet.service;
import static java.lang.String.CASE_INSENSITIVE_ORDER; import static java.lang.String.CASE_INSENSITIVE_ORDER;
import com.kamco.cd.kamcoback.common.utils.FIleChecker;
import com.kamco.cd.kamcoback.common.utils.NameValidator; import com.kamco.cd.kamcoback.common.utils.NameValidator;
import com.kamco.cd.kamcoback.config.FileConfig; import com.kamco.cd.kamcoback.config.FileConfig;
import com.kamco.cd.kamcoback.mapsheet.dto.FileDto; import com.kamco.cd.kamcoback.mapsheet.dto.FileDto;
@@ -32,6 +33,7 @@ import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@@ -304,4 +306,50 @@ public class MapSheetMngFileCheckerService {
public ImageryDto.SyncReturn syncProcess(ImageryDto.searchReq searchReq) { public ImageryDto.SyncReturn syncProcess(ImageryDto.searchReq searchReq) {
return mapSheetMngFileCheckerCoreService.syncProcess(searchReq); return mapSheetMngFileCheckerCoreService.syncProcess(searchReq);
} }
@Transactional
public String uploadFile(MultipartFile file, String targetPath) {
try {
Path path = Paths.get(targetPath);
// If targetPath is a directory, append the original filename
if (Files.isDirectory(path)) {
path = path.resolve(file.getOriginalFilename());
} else if (Files.notExists(path) && Files.isDirectory(path.getParent())) {
// If path doesn't exist but parent is a directory, assume it's the full path
// (No change needed)
}
// Ensure parent directory exists
if (path.getParent() != null) {
Files.createDirectories(path.getParent());
}
// Save file
file.transferTo(path.toFile());
// Check TIF using Gdal functionality
String ext = FilenameUtils.getExtension(path.getFileName().toString());
if ("tif".equalsIgnoreCase(ext) || "tiff".equalsIgnoreCase(ext)) {
boolean isValid = FIleChecker.cmmndGdalInfo(path.toString());
if (!isValid) {
Files.delete(path); // Delete invalid file
throw new RuntimeException("유효하지 않은 TIF 파일입니다 (Gdal 검증 실패).");
}
}
return "업로드 성공";
} catch (IOException e) {
throw new RuntimeException("파일 업로드 실패: " + e.getMessage());
}
}
@Transactional
public Boolean deleteFile(String filePath) {
try {
Path path = Paths.get(filePath);
return Files.deleteIfExists(path);
} catch (IOException e) {
throw new RuntimeException("파일 삭제 실패: " + e.getMessage());
}
}
} }