학습데이터 업로드, unzip 로직 진행중
This commit is contained in:
@@ -9,6 +9,7 @@ import io.swagger.v3.oas.annotations.media.Content;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import java.util.UUID;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -53,24 +54,26 @@ public class UploadApiController {
|
||||
})
|
||||
@PostMapping(value = "/chunk-upload-dataset", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public ApiResponseDto<UploadDto.UploadRes> uploadChunkDataSetFile(
|
||||
@RequestParam("datasetUid") long datasetUid,
|
||||
// @RequestParam("datasetUid") long datasetUid,
|
||||
@RequestParam("fileName") String fileName,
|
||||
@RequestParam("fileSize") long fileSize,
|
||||
// @RequestParam("fileHash") String fileHash,
|
||||
@RequestParam("chunkIndex") Integer chunkIndex,
|
||||
@RequestParam("chunkTotalIndex") Integer chunkTotalIndex,
|
||||
@RequestPart("chunkFile") MultipartFile chunkFile) {
|
||||
@RequestPart("chunkFile") MultipartFile chunkFile,
|
||||
@RequestParam("uuid") UUID uuid) {
|
||||
|
||||
String uploadDivi = "dataset";
|
||||
|
||||
UploadDto.UploadAddReq upAddReqDto = new UploadDto.UploadAddReq();
|
||||
upAddReqDto.setDatasetId(datasetUid);
|
||||
// upAddReqDto.setDatasetId(datasetUid);
|
||||
upAddReqDto.setFileName(fileName);
|
||||
upAddReqDto.setFileSize(fileSize);
|
||||
upAddReqDto.setChunkIndex(chunkIndex);
|
||||
upAddReqDto.setChunkTotalIndex(chunkTotalIndex);
|
||||
upAddReqDto.setUploadDivi(uploadDivi);
|
||||
// upAddReqDto.setFileHash(fileHash);
|
||||
upAddReqDto.setUuid(uuid);
|
||||
|
||||
return ApiResponseDto.ok(uploadService.uploadChunk(upAddReqDto, chunkFile));
|
||||
}
|
||||
|
||||
@@ -195,6 +195,8 @@ public class UploadDto {
|
||||
private String uuid;
|
||||
private int chunkIndex;
|
||||
private int chunkTotalIndex;
|
||||
private String filePath;
|
||||
private String fileName;
|
||||
|
||||
public double getUploadRate() {
|
||||
if (this.chunkTotalIndex == 0) {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user