학습데이터 업로드, unzip 로직 진행중

This commit is contained in:
2026-02-10 10:43:40 +09:00
parent b4a4486560
commit 89744d2aa1
19 changed files with 621 additions and 43 deletions

View File

@@ -10,6 +10,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Objects;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -51,9 +52,11 @@ public class UploadService {
UploadDto.UploadRes upRes = new UploadDto.UploadRes();
long datasetId = upAddReqDto.getDatasetId();
// long datasetId = upAddReqDto.getDatasetId();
long datasetId = 0;
String uploadId = System.currentTimeMillis() + "";
UUID uuid = UUID.randomUUID();
// UUID uuid = UUID.randomUUID();
UUID uuid = upAddReqDto.getUuid();
String tmpDataSetDir = "";
String fianlDir = "";
String uploadDivi = upAddReqDto.getUploadDivi();
@@ -68,6 +71,10 @@ public class UploadService {
fianlDir = datasetDir + uuid + "/";
}
// 리턴용 파일 값
upRes.setFilePath(fianlDir);
upRes.setFileName(fileName);
upAddReqDto.setUuid(uuid);
upAddReqDto.setUploadId(uploadId);
upAddReqDto.setStatus(status);
@@ -94,24 +101,22 @@ public class UploadService {
}
// chunk완료시 merge 및 폴더에 저장
if (chunkIndex.equals(chunkTotalIndex)) {
if (Objects.equals(chunkIndex, chunkTotalIndex)) {
// upAddReqDto.setUploadId(dto.getUploadId());
// upAddReqDto.setStatus("MERGING");
// uploadSessionCoreService.updateUploadSessionStatus(upAddReqDto);
upAddReqDto.setUploadId(dto != null ? dto.getUploadId() : upAddReqDto.getUploadId());
upAddReqDto.setStatus("MERGING");
uploadSessionCoreService.updateUploadSessionStatus(upAddReqDto);
/*
try {
this.mergeChunks(tmpDataSetDir, fianlDir, fileName, chunkTotalIndex);
} catch (IOException e) {
//throw new RuntimeException(e);
// throw new RuntimeException(e);
upRes.setRes("fail");
upRes.setResMsg("파일 저장 완료(merge) 애러");
return upRes;
}
*/
upAddReqDto.setUploadId(dto.getUploadId());
upAddReqDto.setUploadId(dto != null ? dto.getUploadId() : upAddReqDto.getUploadId());
upAddReqDto.setStatus("COMPLETE");
uploadSessionCoreService.updateUploadSessionStatus(upAddReqDto);
}
@@ -158,7 +163,7 @@ public class UploadService {
UploadDto.uploadDto dto =
uploadSessionCoreService.findByDatasetUid(
upAddReqDto.getDatasetId(), upAddReqDto.getUploadDivi());
upAddReqDto.getUploadDivi(), upAddReqDto.getUuid());
if (upAddReqDto.getChunkIndex() == 0) {
if (dto != null) {
@@ -190,28 +195,64 @@ public class UploadService {
return dto;
}
public void mergeChunks(String tmpDir, String fianlDir, String fileName, int chunkTotalIndex)
public void mergeChunks(String tmpDir, String finalDir, String fileName, int chunkTotalIndex)
throws IOException {
long start = System.currentTimeMillis();
Path outputPath = Paths.get(finalDir, fileName);
log.info(
"mergeChunks start: fileName={}, tmpDir={}, outputPath={}, lastChunkIndex={}",
fileName,
tmpDir,
outputPath,
chunkTotalIndex);
long totalBytes = 0;
Path outputPath = Paths.get(fianlDir, fileName);
try (FileChannel outChannel =
FileChannel.open(outputPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
FileChannel.open(
outputPath,
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
for (int i = 0; i <= chunkTotalIndex; i++) {
Path chunkPath = Paths.get(tmpDir, i + "");
Path chunkPath = Paths.get(tmpDir, String.valueOf(i));
try (FileChannel inChannel = FileChannel.open(chunkPath, StandardOpenOption.READ)) {
long transferred = 0;
long size = inChannel.size();
long transferred = 0;
while (transferred < size) {
transferred += inChannel.transferTo(transferred, size - transferred, outChannel);
}
totalBytes += size;
}
// 병합 후 즉시 삭제하여 디스크 공간 확보
Files.delete(chunkPath);
}
try {
FIleChecker.deleteFolder(tmpDir);
} catch (Exception e) {
log.warn("tmpDir delete failed (merge already succeeded): tmpDir={}", tmpDir, e);
}
} catch (IOException e) {
log.error(
"mergeChunks failed: fileName={}, tmpDir={}, outputPath={}, lastChunkIndex={}",
fileName,
tmpDir,
outputPath,
chunkTotalIndex,
e);
throw e;
}
// 병합후 임시 폴더 삭제
FIleChecker.deleteFolder(tmpDir);
log.info(
"mergeChunks done: fileName={}, outputPath={}, bytes={}, elapsedMs={}",
fileName,
outputPath,
totalBytes,
(System.currentTimeMillis() - start));
}
}