|
@@ -36,6 +36,7 @@ public class FileUploadService {
|
|
|
private final FileTypeService fileTypeService;
|
|
private final FileTypeService fileTypeService;
|
|
|
private final FileRepository fileRepository;
|
|
private final FileRepository fileRepository;
|
|
|
private Map<String, Set<Integer>> map = new HashMap<>();
|
|
private Map<String, Set<Integer>> map = new HashMap<>();
|
|
|
|
|
+ private Map<String, PathUrl> pathUrlMap = new HashMap<>();
|
|
|
|
|
|
|
|
public FileUploadService(FileUrlService fileUrlService, FileStoreService fileStoreService,
|
|
public FileUploadService(FileUrlService fileUrlService, FileStoreService fileStoreService,
|
|
|
FileTypeService fileTypeService, FileRepository fileRepository) {
|
|
FileTypeService fileTypeService, FileRepository fileRepository) {
|
|
@@ -132,9 +133,11 @@ public class FileUploadService {
|
|
|
*/
|
|
*/
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public synchronized FilePartRet putFilePart(UploadFilePart uploadFilePart) throws Exception {
|
|
public synchronized FilePartRet putFilePart(UploadFilePart uploadFilePart) throws Exception {
|
|
|
|
|
+ int totalParts = uploadFilePart.getTotalChunks();
|
|
|
int partIndex = uploadFilePart.getChunkNumber();
|
|
int partIndex = uploadFilePart.getChunkNumber();
|
|
|
- int partNum = uploadFilePart.getTotalChunks();
|
|
|
|
|
|
|
+ int currentPartSize = uploadFilePart.getCurrentChunkSize();
|
|
|
MultipartFile multipartFile = uploadFilePart.getFile();
|
|
MultipartFile multipartFile = uploadFilePart.getFile();
|
|
|
|
|
+
|
|
|
String uploadId = uploadFilePart.getIdentifier();
|
|
String uploadId = uploadFilePart.getIdentifier();
|
|
|
FileInfo fileInfo = fileRepository.getFileInfoByFileId(uploadId);
|
|
FileInfo fileInfo = fileRepository.getFileInfoByFileId(uploadId);
|
|
|
// 没有检查文件的 sha256sum, 存在安全隐患
|
|
// 没有检查文件的 sha256sum, 存在安全隐患
|
|
@@ -149,33 +152,35 @@ public class FileUploadService {
|
|
|
String suffix = fileInfo.getSuffix();
|
|
String suffix = fileInfo.getSuffix();
|
|
|
String contentType = fileInfo.getContentType();
|
|
String contentType = fileInfo.getContentType();
|
|
|
long size = fileInfo.getSize();
|
|
long size = fileInfo.getSize();
|
|
|
- PathUrl pathUrl = fileUrlService.genPathUrl(contentType, sha256sum, size, fileId, suffix, uploadId);
|
|
|
|
|
Set<Integer> set = map.computeIfAbsent(fileId, k -> new HashSet<>());
|
|
Set<Integer> set = map.computeIfAbsent(fileId, k -> new HashSet<>());
|
|
|
if (set.isEmpty()) {
|
|
if (set.isEmpty()) {
|
|
|
|
|
+ PathUrl pathUrl = fileUrlService.genPathUrl(contentType, sha256sum, size, fileId, suffix, uploadId);
|
|
|
fileStoreService.createSparseFile(pathUrl.getAbsolutePath(), size);
|
|
fileStoreService.createSparseFile(pathUrl.getAbsolutePath(), size);
|
|
|
|
|
+ pathUrlMap.put(fileId, pathUrl);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!set.contains(partIndex)) {
|
|
if (!set.contains(partIndex)) {
|
|
|
set.add(partIndex);
|
|
set.add(partIndex);
|
|
|
long pos = (partIndex-1) * PART_SIZE;
|
|
long pos = (partIndex-1) * PART_SIZE;
|
|
|
- String absolutePath = pathUrl.getAbsolutePath();
|
|
|
|
|
|
|
+ String absolutePath = pathUrlMap.get(fileId).getAbsolutePath();
|
|
|
fileStoreService.writeToFile(multipartFile.getInputStream(), absolutePath, pos);
|
|
fileStoreService.writeToFile(multipartFile.getInputStream(), absolutePath, pos);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- long len = set.size();
|
|
|
|
|
- if (len != partNum) {
|
|
|
|
|
|
|
+ if (set.size() != totalParts) {
|
|
|
return new FilePartRet(fileId);
|
|
return new FilePartRet(fileId);
|
|
|
} else {
|
|
} else {
|
|
|
- String absolutePath = pathUrl.getAbsolutePath();
|
|
|
|
|
|
|
+ String absolutePath = pathUrlMap.get(fileId).getAbsolutePath();
|
|
|
FileInputStream fis = new FileInputStream(absolutePath);
|
|
FileInputStream fis = new FileInputStream(absolutePath);
|
|
|
String sha256sumMerged = DigestUtil.sha256sum(fis);
|
|
String sha256sumMerged = DigestUtil.sha256sum(fis);
|
|
|
if (!sha256sum.equals(sha256sumMerged)) {
|
|
if (!sha256sum.equals(sha256sumMerged)) {
|
|
|
- throw new Exception("分片合并文件的 sha256sum 与原文件一致!");
|
|
|
|
|
|
|
+ throw new Exception("分片合并文件的 sha256sum 与原文件不一致!");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ PathUrl pathUrl = pathUrlMap.get(fileId);
|
|
|
FileContentType fileType = fileTypeService.getFileType(fileInfo.getContentType(), absolutePath);
|
|
FileContentType fileType = fileTypeService.getFileType(fileInfo.getContentType(), absolutePath);
|
|
|
fileRepository.saveFile(fileId, fileType, pathUrl);
|
|
fileRepository.saveFile(fileId, fileType, pathUrl);
|
|
|
map.remove(fileId);
|
|
map.remove(fileId);
|
|
|
|
|
+ pathUrlMap.remove(fileId);
|
|
|
return new FilePartRet(uploadId, pathUrl, fileId);
|
|
return new FilePartRet(uploadId, pathUrl, fileId);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|