|
@@ -1,6 +1,7 @@
|
|
|
package com.persagy.dmp.file.service.impl;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.persagy.dmp.common.exception.BusinessException;
|
|
@@ -19,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -33,9 +35,11 @@ public class MinioStorageServiceImpl implements IFileStorageService, Initializin
|
|
|
|
|
|
private final MinioClient minioClient;
|
|
|
|
|
|
- @Value("${persagy.common.file.minio.bucket.chunk:bdtp-chuck}")
|
|
|
+ @Value("${persagy.common.file.minio.bucket.chunk:bdtp}")
|
|
|
private String chunkBucKet;
|
|
|
|
|
|
+ private static final Long CHUCK_MIN_FILE_SIZE=5242880L;
|
|
|
+
|
|
|
|
|
|
public final static boolean SORT = true;
|
|
|
|
|
@@ -63,12 +67,88 @@ public class MinioStorageServiceImpl implements IFileStorageService, Initializin
|
|
|
if (exists(bucketName,fileName)){
|
|
|
return;
|
|
|
}
|
|
|
+ if (chunkBucKet.equals(bucketName)){
|
|
|
+
|
|
|
+
|
|
|
+ byte[] bytes = IoUtil.readBytes(input);
|
|
|
+ if (bytes.length<CHUCK_MIN_FILE_SIZE){
|
|
|
+
|
|
|
+ mergeMinFile(bucketName,fileName,bytes);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ input = IoUtil.toStream(bytes);
|
|
|
+ }
|
|
|
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).
|
|
|
stream(input, input.available(), -1).build());
|
|
|
} catch (Exception e) {
|
|
|
FileExceptionHandler.handleException(e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ * Description: 上传分片
|
|
|
+ * @param chuckBucketName : bucketName
|
|
|
+ * @param fileMd5 : 文件的md5值
|
|
|
+ * @param inputStream : 文件流
|
|
|
+ * @param totalChunks : 总分片数
|
|
|
+ * @param currentChuckNumber : 当前分片数
|
|
|
+ * return : void
|
|
|
+ * @author : lijie
|
|
|
+ * Update By 2021/12/17 15:18
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void uploadChuck(String chuckBucketName, String fileMd5, InputStream inputStream,Integer totalChunks,
|
|
|
+ Integer currentChuckNumber) {
|
|
|
+ byte[] bytes = IoUtil.readBytes(inputStream);
|
|
|
+
|
|
|
+ upload(chuckBucketName,fileMd5+StrUtil.SLASH+currentChuckNumber+FileCommonConst.CHUCK_FILE_SUFFIX,IoUtil.toStream(bytes));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * Description: 合并小分片
|
|
|
+ * @param bucketName : bucket的名称
|
|
|
+ * @param fileName : 文件名称
|
|
|
+ * @param bytes : 字节内容
|
|
|
+ * return : void
|
|
|
+ * @author : lijie
|
|
|
+ * Update By 2021/12/17 12:23
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ private void mergeMinFile(String bucketName, String fileName, byte[] bytes) {
|
|
|
+ String prefix = fileName.substring(0, fileName.lastIndexOf("/"));
|
|
|
+ List<String> objectNames = listObjectNames(bucketName, prefix, Boolean.TRUE);
|
|
|
+ if (CollUtil.isEmpty(objectNames)){
|
|
|
+ ByteArrayInputStream inputStream = IoUtil.toStream(bytes);
|
|
|
+ minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).
|
|
|
+ stream(inputStream, inputStream.available(), -1).build());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ String lastObjName = objectNames.get(objectNames.size() - 1);
|
|
|
+ StatObjectResponse objectResponse = minioClient
|
|
|
+ .statObject(StatObjectArgs.builder().bucket(bucketName).object(lastObjName).build());
|
|
|
+ if (objectResponse.size()>CHUCK_MIN_FILE_SIZE){
|
|
|
+ ByteArrayInputStream inputStream = IoUtil.toStream(bytes);
|
|
|
+ minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).
|
|
|
+ stream(inputStream, inputStream.available(), -1).build());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ InputStream lastObj = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(lastObjName).build());
|
|
|
+ byte[] lastBytes = IoUtil.readBytes(lastObj);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public InputStream download(String bucketName, String fileName) {
|
|
@@ -297,7 +377,7 @@ public class MinioStorageServiceImpl implements IFileStorageService, Initializin
|
|
|
}
|
|
|
List<String> urlList = new ArrayList<>(chunkCount);
|
|
|
for (int i = 1; i <= chunkCount; i++){
|
|
|
- String objectName = objectMd5 + i + ".chunk";
|
|
|
+ String objectName = objectMd5 + i + FileCommonConst.CHUCK_FILE_SUFFIX;
|
|
|
urlList.add(createUploadUrl(bucketName,objectName,DEFAULT_EXPIRY));
|
|
|
}
|
|
|
return urlList;
|
|
@@ -320,7 +400,7 @@ public class MinioStorageServiceImpl implements IFileStorageService, Initializin
|
|
|
if (StrUtil.isBlank(objectMd5)){
|
|
|
return null;
|
|
|
}
|
|
|
- objectMd5 += "/" + partNumber + ".chunk";
|
|
|
+ objectMd5 += "/" + partNumber + FileCommonConst.CHUCK_FILE_SUFFIX;
|
|
|
return createUploadUrl(bucketName,objectMd5,DEFAULT_EXPIRY);
|
|
|
}
|
|
|
|