init
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
package com.kamco.cd.training.upload.service;
|
||||
|
||||
import com.kamco.cd.training.common.utils.FIleChecker;
|
||||
import com.kamco.cd.training.postgres.core.UploadSessionCoreService;
|
||||
import com.kamco.cd.training.upload.dto.UploadDto;
|
||||
import com.kamco.cd.training.upload.dto.UploadDto.DmlReturn;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
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;
|
||||
|
||||
@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 = upAddReqDto.getDatasetId();
|
||||
String uploadId = System.currentTimeMillis()+"";
|
||||
UUID uuid = UUID.randomUUID();
|
||||
String tmpDataSetDir = "";
|
||||
String fianlDir = "";
|
||||
String uploadDivi = upAddReqDto.getUploadDivi();
|
||||
//String fileName = file.getOriginalFilename();
|
||||
String fileName = upAddReqDto.getFileName();
|
||||
Integer chunkIndex = upAddReqDto.getChunkIndex();
|
||||
Integer chunkTotalIndex = upAddReqDto.getChunkTotalIndex();
|
||||
String status = "UPLOADING";
|
||||
|
||||
if( uploadDivi.equals("dataset"))
|
||||
{
|
||||
tmpDataSetDir = datasetTmpDir+uuid+"/";
|
||||
fianlDir = datasetDir+uuid+"/";
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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+" 업로드 애러");
|
||||
}
|
||||
|
||||
//chunk완료시 merge 및 폴더에 저장
|
||||
if( chunkIndex == chunkTotalIndex ) {
|
||||
|
||||
//upAddReqDto.setUploadId(dto.getUploadId());
|
||||
//upAddReqDto.setStatus("MERGING");
|
||||
//uploadSessionCoreService.updateUploadSessionStatus(upAddReqDto);
|
||||
|
||||
/*
|
||||
try {
|
||||
this.mergeChunks(tmpDataSetDir, fianlDir, fileName, chunkTotalIndex);
|
||||
} catch (IOException e) {
|
||||
//throw new RuntimeException(e);
|
||||
upRes.setRes("fail");
|
||||
upRes.setResMsg("파일 저장 완료(merge) 애러");
|
||||
return upRes;
|
||||
}
|
||||
*/
|
||||
|
||||
upAddReqDto.setUploadId(dto.getUploadId());
|
||||
upAddReqDto.setStatus("COMPLETE");
|
||||
uploadSessionCoreService.updateUploadSessionStatus(upAddReqDto);
|
||||
|
||||
}
|
||||
|
||||
return upRes;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public DmlReturn completeUpload(String uuid) {
|
||||
|
||||
UploadDto.uploadDto dto = uploadSessionCoreService.findByUuid(uuid);
|
||||
|
||||
try {
|
||||
this.mergeChunks(dto.getTempPath(), dto.getFinalPath(), dto.getFileName(), dto.getChunkTotalIndex());
|
||||
} catch (IOException e) {
|
||||
return new DmlReturn("mergingfail", "chunk파일 merge 애러");
|
||||
}
|
||||
|
||||
return new DmlReturn("success", "병합(merge) 정상처리되었습니다.");
|
||||
|
||||
}
|
||||
|
||||
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.findByDatasetUid(upAddReqDto.getDatasetId(), upAddReqDto.getUploadDivi());
|
||||
|
||||
if( upAddReqDto.getChunkIndex() == 0 ) {
|
||||
if( dto != null )
|
||||
{
|
||||
upRes.setRes("duplicate");
|
||||
upRes.setResMsg("이미 진행중인 업로드세션입니다.");
|
||||
return dto;
|
||||
}
|
||||
|
||||
upAddReqDto.setStatus("UPLOADING");
|
||||
upRes.setUuid( upAddReqDto.getUuid().toString() );
|
||||
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().toString() );
|
||||
|
||||
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