shp 파일 geosjon 등록추가, 파일다운로드 파일명 오류 수정

This commit is contained in:
2026-01-20 15:58:27 +09:00
parent 2916ec594d
commit d3fc6d7ba1
5 changed files with 75 additions and 30 deletions

View File

@@ -6,17 +6,50 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Log4j2 @Log4j2
@Component @Component
@RequiredArgsConstructor
public class ExternalJarRunner { public class ExternalJarRunner {
public void run(String jarPath, String batchIds, String inferenceId, String mapIds) { private static final long TIMEOUT_MINUTES = 30;
/**
* shp 파일 생성
*
* @param jarPath jar 경로
* @param batchIds 배치 아이디
* @param inferenceId uid
* @param mapIds 도엽 Id
*/
public void run(String jarPath, String batchIds, String inferenceId, String mapIds) {
List<String> args = new ArrayList<>();
addArg(args, "converter.inference-id", inferenceId);
addArg(args, "converter.map-ids", mapIds);
addArg(args, "converter.batch-ids", batchIds);
execJar(jarPath, args);
}
/**
* geoserver 등록
*
* @param jarPath jar 파일경로
* @param register shp 경로
* @param layer geoserver에 등록될 레이어 이름
*/
public void run(String jarPath, String register, String layer) {
List<String> args = new ArrayList<>();
addArg(args, "register", register);
addArg(args, "layer", layer);
execJar(jarPath, args);
}
private void execJar(String jarPath, List<String> args) {
StringBuilder out = new StringBuilder(); StringBuilder out = new StringBuilder();
try { try {
@@ -24,16 +57,7 @@ public class ExternalJarRunner {
cmd.add("java"); cmd.add("java");
cmd.add("-jar"); cmd.add("-jar");
cmd.add(jarPath); cmd.add(jarPath);
cmd.addAll(args);
if (inferenceId != null && !inferenceId.isBlank()) {
cmd.add("--converter.inference-id=" + inferenceId);
}
if (mapIds != null && !mapIds.isBlank()) {
cmd.add("--converter.map-ids=" + mapIds);
}
if (batchIds != null && !batchIds.isBlank()) {
cmd.add("--converter.batch-ids=" + batchIds);
}
ProcessBuilder pb = new ProcessBuilder(cmd); ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true); pb.redirectErrorStream(true);
@@ -49,7 +73,7 @@ public class ExternalJarRunner {
} }
} }
boolean finished = p.waitFor(30, TimeUnit.MINUTES); boolean finished = p.waitFor(TIMEOUT_MINUTES, TimeUnit.MINUTES);
if (!finished) { if (!finished) {
p.destroyForcibly(); p.destroyForcibly();
throw new RuntimeException("jar timeout\n" + out); throw new RuntimeException("jar timeout\n" + out);
@@ -66,4 +90,10 @@ public class ExternalJarRunner {
log.error("jar execution error. output=\n{}", out, e); log.error("jar execution error. output=\n{}", out, e);
} }
} }
private void addArg(List<String> args, String key, String value) {
if (value != null && !value.isBlank()) {
args.add("--" + key + "=" + value);
}
}
} }

View File

@@ -25,10 +25,11 @@ 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 jakarta.validation.Valid; import jakarta.validation.Valid;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
@@ -45,7 +46,6 @@ 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.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriUtils;
@Tag(name = "추론관리", description = "추론관리 API") @Tag(name = "추론관리", description = "추론관리 API")
@RequestMapping("/api/inference") @RequestMapping("/api/inference")
@@ -423,30 +423,32 @@ public class InferenceResultApiController {
throws IOException { throws IOException {
String path; String path;
String uid;
try { try {
path = String.valueOf(inferenceResultService.shpDownloadPath(uuid)); Map<String, Object> map = inferenceResultService.shpDownloadPath(uuid);
path = String.valueOf(map.get("path"));
uid = String.valueOf(map.get("uid"));
} catch (CustomApiException e) { } catch (CustomApiException e) {
// 데이터 없음 등 404
return ResponseEntity.status(e.getStatus()).build(); return ResponseEntity.status(e.getStatus()).build();
} }
Path zipPath = Path.of(path); Path zipPath = Path.of(path);
FileSystemResource resource = new FileSystemResource(zipPath);
if (!resource.exists() || !resource.isReadable()) { if (!Files.exists(zipPath) || !Files.isReadable(zipPath)) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
String filename = zipPath.getFileName().toString(); FileSystemResource resource = new FileSystemResource(zipPath);
String encodedFilename = UriUtils.encode(filename, StandardCharsets.UTF_8);
String filename = uid + ".zip";
long fileSize = Files.size(zipPath);
return ResponseEntity.ok() return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM) .contentType(MediaType.APPLICATION_OCTET_STREAM)
.header( .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
HttpHeaders.CONTENT_DISPOSITION, .contentLength(fileSize)
"attachment; filename=\"" + filename + "\"; filename*=UTF-8''" + encodedFilename) .body(resource);
.contentLength(resource.contentLength())
.body((Resource) resource);
} }
@Operation(summary = "shp 파일 다운로드 이력", description = "추론관리 분석결과 shp 파일 다운로드 이력") @Operation(summary = "shp 파일 다운로드 이력", description = "추론관리 분석결과 shp 파일 다운로드 이력")

View File

@@ -41,6 +41,7 @@ import jakarta.validation.constraints.NotNull;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@@ -578,11 +579,16 @@ public class InferenceResultService {
* @param uuid * @param uuid
* @return * @return
*/ */
public Path shpDownloadPath(UUID uuid) { public Map<String, Object> shpDownloadPath(UUID uuid) {
InferenceLearnDto dto = inferenceResultCoreService.getInferenceUid(uuid); InferenceLearnDto dto = inferenceResultCoreService.getInferenceUid(uuid);
String uid = dto.getUid(); String uid = dto.getUid();
Path path = Path.of(datasetDir).resolve(uid).resolve("merge").resolve(uid + ".zip");
return Path.of(datasetDir).resolve(uid).resolve("merge").resolve(uid + ".zip"); Map<String, Object> downloadMap = new HashMap<>();
downloadMap.put("uid", uid);
downloadMap.put("path", path);
return downloadMap;
} }
/** /**

View File

@@ -187,7 +187,7 @@ public class MapSheetLearnEntity {
private int m3FailedJobs = 0; private int m3FailedJobs = 0;
@Column(name = "uid", nullable = false) @Column(name = "uid", nullable = false)
private String uid = UUID.randomUUID().toString().replace("-", ""); private String uid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
public InferenceResultDto.ResultList toDto() { public InferenceResultDto.ResultList toDto() {
return new InferenceResultDto.ResultList( return new InferenceResultDto.ResultList(

View File

@@ -53,6 +53,9 @@ public class MapSheetInferenceJobService {
@Value("${inference.jar-path}") @Value("${inference.jar-path}")
private String jarPath; private String jarPath;
@Value("${file.dataset-dir}")
private String datasetDir;
/** 추론 진행 배치 1분 60_000 */ /** 추론 진행 배치 1분 60_000 */
@Scheduled(fixedDelay = 30_000) @Scheduled(fixedDelay = 30_000)
public void runBatch() { public void runBatch() {
@@ -242,6 +245,10 @@ public class MapSheetInferenceJobService {
// uid 기준 merge shp, geojson 파일 생성 // uid 기준 merge shp, geojson 파일 생성
externalJarRunner.run(jarPath, batchId, inferenceId, ""); externalJarRunner.run(jarPath, batchId, inferenceId, "");
// uid 기준 도엽별 shp 파일 geoserver 등록
String register = datasetDir + "/" + inferenceId + "/" + "merge" + "/" + inferenceId + ".shp";
externalJarRunner.run(jarPath, register, inferenceId);
} }
/** /**