chunk업로드 공통, 모델관리 수정
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
package com.kamco.cd.kamcoback.upload.service;
|
||||
|
||||
import com.kamco.cd.kamcoback.common.enums.FileUploadStatus;
|
||||
import com.kamco.cd.kamcoback.common.utils.FIleChecker;
|
||||
import com.kamco.cd.kamcoback.postgres.core.UploadSessionCoreService;
|
||||
import com.kamco.cd.kamcoback.upload.dto.UploadDto;
|
||||
import com.kamco.cd.kamcoback.upload.dto.UploadDto.DmlReturn;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.UUID;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UploadService {
|
||||
|
||||
private final UploadSessionCoreService uploadSessionCoreService;
|
||||
|
||||
@Value("${file.sync-root-dir}")
|
||||
private String syncRootDir;
|
||||
|
||||
@Value("${file.sync-tmp-dir}")
|
||||
private String syncTmpDir;
|
||||
|
||||
@Value("${file.sync-file-extention}")
|
||||
private String syncFileExtention;
|
||||
|
||||
@Value("${file.dataset-dir}")
|
||||
private String datasetDir;
|
||||
|
||||
@Value("${file.dataset-tmp-dir}")
|
||||
private String datasetTmpDir;
|
||||
|
||||
@Value("${file.model-dir}")
|
||||
private String modelDir;
|
||||
|
||||
@Value("${file.model-tmp-dir}")
|
||||
private String modelTmpDir;
|
||||
|
||||
@Transactional
|
||||
public DmlReturn initUpload(UploadDto.InitReq initReq) {
|
||||
|
||||
return new DmlReturn("success", "UPLOAD CHUNK INIT");
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UploadDto.UploadRes uploadChunk(UploadDto.UploadAddReq upAddReqDto, MultipartFile file) {
|
||||
|
||||
UploadDto.UploadRes upRes = new UploadDto.UploadRes();
|
||||
|
||||
long datasetId = 0;
|
||||
if( upAddReqDto.getDatasetId() != null )datasetId = upAddReqDto.getDatasetId();
|
||||
String uploadId = System.currentTimeMillis()+"";
|
||||
//UUID uuid = UUID.randomUUID();
|
||||
UUID uuid = upAddReqDto.getUuid();
|
||||
String tmpDataSetDir = upAddReqDto.getTempPath()+uuid;
|
||||
String fianlDir = upAddReqDto.getFinalPath()+uuid;
|
||||
String uploadDivi = upAddReqDto.getUploadDivi();
|
||||
//String fileName = file.getOriginalFilename();
|
||||
String fileName = upAddReqDto.getFileName();
|
||||
Integer chunkIndex = upAddReqDto.getChunkIndex();
|
||||
Integer chunkTotalIndex = upAddReqDto.getChunkTotalIndex();
|
||||
String status = FileUploadStatus.INIT.name();
|
||||
|
||||
upRes.setUuid(uuid);
|
||||
upRes.setFilePath(fianlDir);
|
||||
upRes.setFileName(fileName);
|
||||
|
||||
upAddReqDto.setUuid(uuid);
|
||||
upAddReqDto.setUploadId(uploadId);
|
||||
upAddReqDto.setStatus(status);
|
||||
upAddReqDto.setFileName(fileName);
|
||||
upAddReqDto.setTempPath(tmpDataSetDir);
|
||||
upAddReqDto.setFinalPath(fianlDir);
|
||||
|
||||
//세션 신규,중복체크(초기화 포함)
|
||||
UploadDto.uploadDto dto = this.checkUploadSession(upAddReqDto, upRes);
|
||||
if( !upRes.getRes().equals("success") )return upRes;
|
||||
|
||||
|
||||
status = FileUploadStatus.UPLOADING.name();
|
||||
upAddReqDto.setStatus(status);
|
||||
|
||||
if( dto != null )
|
||||
{
|
||||
tmpDataSetDir = dto.getTempPath();
|
||||
fianlDir = dto.getFinalPath();
|
||||
}
|
||||
|
||||
//폴더 생성 및 체크
|
||||
if( ! checkChunkFoler(upRes, tmpDataSetDir, fianlDir) )return upRes;
|
||||
|
||||
//chunk저장하기
|
||||
if( ! FIleChecker.multipartChunkSaveTo(file, tmpDataSetDir, chunkIndex ) )
|
||||
{
|
||||
upRes.setRes("fail");
|
||||
upRes.setResMsg("chunkIndex:"+chunkIndex+" 업로드 애러");
|
||||
}
|
||||
|
||||
if( chunkIndex == chunkTotalIndex ) {
|
||||
|
||||
upAddReqDto.setUploadId(dto.getUploadId());
|
||||
upAddReqDto.setStatus(FileUploadStatus.DONE.name());
|
||||
uploadSessionCoreService.updateUploadSessionStatus(upAddReqDto);
|
||||
|
||||
|
||||
try {
|
||||
this.mergeChunks(tmpDataSetDir, fianlDir, fileName, chunkTotalIndex);
|
||||
|
||||
upAddReqDto.setUploadId(dto.getUploadId());
|
||||
upAddReqDto.setStatus("MERGED");
|
||||
uploadSessionCoreService.updateUploadSessionStatus(upAddReqDto);
|
||||
|
||||
} catch (IOException e) {
|
||||
//throw new RuntimeException(e);
|
||||
upRes.setRes("fail");
|
||||
upRes.setResMsg("파일Chunk 병합(merge) 애러");
|
||||
return upRes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
return upRes;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UploadDto.UploadRes completeUpload(UUID uuid) {
|
||||
|
||||
UploadDto.uploadDto dto = uploadSessionCoreService.findByUuid(uuid);
|
||||
UploadDto.UploadRes upRes = new UploadDto.UploadRes();
|
||||
upRes.setRes("success");
|
||||
upRes.setResMsg("병합(merge) 정상처리되었습니다.");
|
||||
upRes.setUuid(uuid);
|
||||
upRes.setFilePath(dto.getFinalPath());
|
||||
upRes.setFileName(dto.getFileName());
|
||||
upRes.setChunkIndex(dto.getChunkIndex());
|
||||
upRes.setChunkTotalIndex(dto.getChunkTotalIndex());
|
||||
|
||||
try {
|
||||
this.mergeChunks(dto.getTempPath(), dto.getFinalPath(), dto.getFileName(), dto.getChunkTotalIndex());
|
||||
} catch (IOException e) {
|
||||
|
||||
upRes.setRes("fail");
|
||||
upRes.setResMsg("병합(merge) 애러");
|
||||
|
||||
return upRes;
|
||||
}
|
||||
|
||||
return upRes;
|
||||
|
||||
}
|
||||
|
||||
public boolean checkChunkFoler(UploadDto.UploadRes upRes, String tmpDataSetDir, String fianlDir)
|
||||
{
|
||||
|
||||
if( ! FIleChecker.mkDir(tmpDataSetDir) )
|
||||
{
|
||||
upRes.setRes("fail");
|
||||
upRes.setRes("CHUNK 폴더 생성 ERROR");
|
||||
return false;
|
||||
}
|
||||
|
||||
if( ! FIleChecker.mkDir(fianlDir) )
|
||||
{
|
||||
upRes.setRes("fail");
|
||||
upRes.setRes("업로드 완료 폴더 생성 ERROR");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public UploadDto.uploadDto checkUploadSession(UploadDto.UploadAddReq upAddReqDto, UploadDto.UploadRes upRes) {
|
||||
|
||||
upRes.setRes("success");
|
||||
upRes.setResMsg("정상처리되었습니다.");
|
||||
|
||||
UploadDto.uploadDto dto = uploadSessionCoreService.findByUuid(upAddReqDto.getUuid());
|
||||
|
||||
if( upAddReqDto.getChunkIndex() == 0 ) {
|
||||
if( dto != null )
|
||||
{
|
||||
upRes.setRes("duplicate");
|
||||
upRes.setResMsg("이미 진행중인 업로드세션입니다.");
|
||||
return dto;
|
||||
}
|
||||
|
||||
upAddReqDto.setStatus("UPLOADING");
|
||||
upRes.setUuid( upAddReqDto.getUuid() );
|
||||
uploadSessionCoreService.createUploadSession(upAddReqDto);
|
||||
}
|
||||
else {
|
||||
if( dto == null ){
|
||||
upRes.setRes("nosession");
|
||||
upRes.setResMsg("업로드 세션이 존재하지 않습니다.");
|
||||
return dto;
|
||||
}
|
||||
|
||||
upAddReqDto.setStatus("UPLOADING");
|
||||
upAddReqDto.setUploadId(dto.getUploadId());
|
||||
uploadSessionCoreService.updateUploadSessionStatus(upAddReqDto);
|
||||
}
|
||||
|
||||
if( dto != null )upRes.setUuid( dto.getUuid() );
|
||||
|
||||
upRes.setChunkIndex(upAddReqDto.getChunkIndex());
|
||||
upRes.setChunkTotalIndex(upAddReqDto.getChunkTotalIndex());
|
||||
|
||||
|
||||
return dto;
|
||||
|
||||
}
|
||||
|
||||
public void mergeChunks(String tmpDir, String fianlDir, String fileName, int chunkTotalIndex) throws IOException {
|
||||
|
||||
Path outputPath = Paths.get(fianlDir, fileName);
|
||||
try (FileChannel outChannel = FileChannel.open(outputPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
|
||||
for (int i = 0; i <= chunkTotalIndex; i++) {
|
||||
Path chunkPath = Paths.get(tmpDir, i+"");
|
||||
|
||||
try (FileChannel inChannel = FileChannel.open(chunkPath, StandardOpenOption.READ)) {
|
||||
long transferred = 0;
|
||||
long size = inChannel.size();
|
||||
while (transferred < size) {
|
||||
transferred += inChannel.transferTo(transferred, size - transferred, outChannel);
|
||||
}
|
||||
}
|
||||
// 병합 후 즉시 삭제하여 디스크 공간 확보
|
||||
Files.delete(chunkPath);
|
||||
}
|
||||
}
|
||||
|
||||
//병합후 임시 폴더 삭제
|
||||
FIleChecker.deleteFolder(tmpDir);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user