|
|
@@ -5,9 +5,8 @@ import cn.reghao.oss.api.dto.ObjectChannel;
|
|
|
import cn.reghao.oss.api.rest.*;
|
|
|
import cn.reghao.oss.store.db.repository.FilePartRepository;
|
|
|
import cn.reghao.oss.store.db.repository.ObjectRepository;
|
|
|
-import cn.reghao.oss.store.model.dto.PathUrl;
|
|
|
import cn.reghao.oss.store.model.po.FilePart;
|
|
|
-import cn.reghao.oss.store.model.po.FileParts;
|
|
|
+import cn.reghao.oss.store.model.po.FileMultipart;
|
|
|
import cn.reghao.oss.store.model.vo.ObjectProp;
|
|
|
import cn.reghao.oss.store.util.FileType;
|
|
|
import cn.reghao.oss.store.util.StringUtil;
|
|
|
@@ -31,8 +30,6 @@ public class ObjectMultipartUploadService {
|
|
|
private final static long PART_SIZE = 1024*1024*20;
|
|
|
private final ObjectRepository objectRepository;
|
|
|
private final FileStoreService fileStoreService;
|
|
|
- private final Map<String, Set<Long>> map = new HashMap<>();
|
|
|
- private final Map<String, PathUrl> pathMap = new HashMap<>();
|
|
|
private final ObjectNameService objectNameService;
|
|
|
private final PutObjectService putObjectService;
|
|
|
private final FilePartRepository filePartRepository;
|
|
|
@@ -81,7 +78,14 @@ public class ObjectMultipartUploadService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public UploadedPart getUploadedPart() {
|
|
|
+ public UploadedPart getUploadedPart(String sha256sum) {
|
|
|
+ FileMultipart fileMultipart = filePartRepository.getFileMultipart(sha256sum);
|
|
|
+ if (fileMultipart == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String contentId = fileMultipart.getContentId();
|
|
|
+ List<FilePart> list = filePartRepository.getFileParts(contentId);
|
|
|
UploadedPart uploadedPart = new UploadedPart();
|
|
|
uploadedPart.setSkipUpload(false);
|
|
|
uploadedPart.setNeedMerge(false);
|
|
|
@@ -107,7 +111,6 @@ public class ObjectMultipartUploadService {
|
|
|
int currentPartSize = uploadFilePart.getCurrentChunkSize();
|
|
|
long totalParts = uploadFilePart.getTotalChunks();
|
|
|
long chunkNumber = uploadFilePart.getChunkNumber();
|
|
|
- log.info("{} -> {}:{}", currentPartSize, totalParts, chunkNumber);
|
|
|
|
|
|
String sha256sum = uploadFilePart.getIdentifier();
|
|
|
FileMeta fileMeta = objectRepository.getBySha256sum(sha256sum);
|
|
|
@@ -123,49 +126,51 @@ public class ObjectMultipartUploadService {
|
|
|
return new UploadFileRet(sha256sum, url);
|
|
|
}
|
|
|
|
|
|
- Set<Long> set = map.computeIfAbsent(sha256sum, k -> new HashSet<>());
|
|
|
- if (set.isEmpty()) {
|
|
|
- String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ List<FilePart> list = new ArrayList<>();
|
|
|
+ String contentId = filePartRepository.getFileMultipart(sha256sum).getContentId();
|
|
|
+ if (contentId == null) {
|
|
|
+ contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
String absolutePath = fileStoreService.genFilePath(contentId, totalSize, "");
|
|
|
fileStoreService.createSparseFile(absolutePath, totalSize);
|
|
|
|
|
|
- String objectId = "";
|
|
|
- FileParts fileParts = new FileParts(objectId, absolutePath, uploadFilePart);
|
|
|
- FilePart filePart = new FilePart(objectId, uploadFilePart);
|
|
|
- filePartRepository.save(fileParts, filePart);
|
|
|
-
|
|
|
- PathUrl pathUrl = new PathUrl(contentId, absolutePath);
|
|
|
- pathMap.put(sha256sum, pathUrl);
|
|
|
- }
|
|
|
-
|
|
|
- if (set.add(chunkNumber)) {
|
|
|
- long pos = (chunkNumber-1) * chunkSize;
|
|
|
- String absolutePath = pathMap.get(sha256sum).getAbsolutePath();
|
|
|
- fileStoreService.writeToFile(inputStream, absolutePath, pos);
|
|
|
+ FileMultipart fileMultipart = new FileMultipart(contentId, uploadFilePart, absolutePath);
|
|
|
+ filePartRepository.saveFileParts(fileMultipart);
|
|
|
+ } else {
|
|
|
+ list = filePartRepository.getFileParts(contentId);
|
|
|
}
|
|
|
|
|
|
- if (set.size() != totalParts) {
|
|
|
+ FilePart filePart = new FilePart(contentId, uploadFilePart);
|
|
|
+ filePartRepository.saveFilePart(filePart);
|
|
|
+ long pos = (chunkNumber-1) * chunkSize;
|
|
|
+ String absolutePath = filePartRepository.getFileMultipart(sha256sum).getAbsolutePath();
|
|
|
+ fileStoreService.writeToFile(inputStream, absolutePath, pos);
|
|
|
+ if (list.size()+1 != totalParts) {
|
|
|
return new UploadFileRet(sha256sum);
|
|
|
} else {
|
|
|
- String contentId = pathMap.get(sha256sum).getContentId();
|
|
|
- String absolutePath = pathMap.get(sha256sum).getAbsolutePath();
|
|
|
- FileInputStream fis = new FileInputStream(absolutePath);
|
|
|
- String sha256sumMerged = DigestUtil.sha256sum(fis);
|
|
|
- if (!sha256sum.equals(sha256sumMerged)) {
|
|
|
- throw new Exception("分片合并文件的 sha256sum 与原文件不一致!");
|
|
|
- }
|
|
|
-
|
|
|
- log.info("合并的文件 {}", absolutePath);
|
|
|
- String suffix = StringUtil.getSuffix(filename);
|
|
|
- ObjectProp objectProp = objectNameService.getObjectProp(objectChannel, suffix);
|
|
|
- File savedFile = new File(absolutePath);
|
|
|
- putObjectService.putObject(objectProp, contentId, savedFile, filename, sha256sum);
|
|
|
+ return mergeFileParts(absolutePath, sha256sum, objectChannel, filename);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- map.remove(sha256sum);
|
|
|
- pathMap.remove(sha256sum);
|
|
|
- String host = ServletUtil.getHeader("host");
|
|
|
- String url = String.format("//%s/%s", host, objectProp);
|
|
|
- return new UploadFileRet(sha256sum, url);
|
|
|
+ private UploadFileRet mergeFileParts(String absolutePath,
|
|
|
+ String sha256sum,
|
|
|
+ ObjectChannel objectChannel,
|
|
|
+ String filename) throws Exception {
|
|
|
+ FileInputStream fis = new FileInputStream(absolutePath);
|
|
|
+ String sha256sumMerged = DigestUtil.sha256sum(fis);
|
|
|
+ if (!sha256sum.equals(sha256sumMerged)) {
|
|
|
+ throw new Exception("分片合并文件的 sha256sum 与原文件不一致!");
|
|
|
}
|
|
|
+
|
|
|
+ log.info("合并的文件 {}", absolutePath);
|
|
|
+ String contentId = filePartRepository.getFileMultipart(sha256sum).getContentId();
|
|
|
+ String suffix = StringUtil.getSuffix(filename);
|
|
|
+ ObjectProp objectProp = objectNameService.getObjectProp(objectChannel, suffix);
|
|
|
+ File savedFile = new File(absolutePath);
|
|
|
+ putObjectService.putObject(objectProp, contentId, savedFile, filename, sha256sum);
|
|
|
+
|
|
|
+ filePartRepository.updateSetUploaded(sha256sum);
|
|
|
+ String host = ServletUtil.getHeader("host");
|
|
|
+ String url = String.format("//%s/%s", host, objectProp);
|
|
|
+ return new UploadFileRet(sha256sum, url);
|
|
|
}
|
|
|
}
|