|
@@ -1,6 +1,8 @@
|
|
|
package com.persagy.dmp.storage.utils;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
|
import com.persagy.dmp.common.constant.ResponseCode;
|
|
|
import com.persagy.dmp.common.exception.BusinessException;
|
|
@@ -12,7 +14,11 @@ import com.persagy.dmp.file.model.FileMd5;
|
|
|
import com.persagy.dmp.file.model.FileMd5Creator;
|
|
|
import com.persagy.dmp.storage.constant.FileCommonConst;
|
|
|
import com.persagy.dmp.storage.service.FileStorageFactory;
|
|
|
+import com.sun.corba.se.impl.encoding.WrapperInputStream;
|
|
|
+import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.springframework.util.StreamUtils;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.util.List;
|
|
@@ -33,32 +39,52 @@ public class FileStorageHelper {
|
|
|
*/
|
|
|
public static String uploadFile(FileInfo fileInfo, InputStream in) {
|
|
|
// 1.获得文件大小
|
|
|
- fileInfo.setFileSize(getStreamSize(in));
|
|
|
+ byte[] streamBytes = getStreamBytes(in);
|
|
|
+ fileInfo.setFileSize((long) streamBytes.length);
|
|
|
// 2.获得文件的md5值
|
|
|
- String fileMd5Str = DigestUtil.md5Hex(in);
|
|
|
+ String fileMd5Str = DigestUtil.md5Hex(streamBytes);
|
|
|
FileMd5 fileMd5 = FileMd5ClientFacade.loadByMd5(fileMd5Str);
|
|
|
- if (null!=fileMd5
|
|
|
- && FileCommonConst.UPLOAD_PART.equals(fileMd5.getUploadStatus())){
|
|
|
- throw new BusinessException(ResponseCode.B0300.getCode(),ResponseCode.B0300.getDesc());
|
|
|
- }
|
|
|
// 3.创建新的存储的fileInfo
|
|
|
- FileInfo saveFileInfo = FileInfoCreator.of(fileInfo.getGroupCode(), fileInfo.getBusinessId(), fileInfo.getFileName(),
|
|
|
- fileMd5Str, fileInfo.getExpireDate());
|
|
|
- // 秒传
|
|
|
+ FileInfo saveFileInfo = FileInfoCreator.of(fileInfo.getGroupCode(),fileInfo.getFileName(),
|
|
|
+ fileInfo.getBusinessId(), fileMd5Str, fileInfo.getExpireDate());
|
|
|
+ // 4.如果文件MD5信息已存在,则直接保存文件信息即可.秒传
|
|
|
saveFileInfo = FileClientFacade.insert(saveFileInfo);
|
|
|
if (null!=fileMd5
|
|
|
&& FileCommonConst.UPLOAD_SUCCESS.equals(fileMd5.getUploadStatus())){
|
|
|
return saveFileInfo.getId();
|
|
|
}
|
|
|
- // 4.没上传过
|
|
|
- fileMd5 = FileMd5Creator.of(fileInfo.getFileBucket(), fileMd5Str, FileCommonConst.UPLOAD_SUCCESS);
|
|
|
- fileMd5.setFileName(fileInfo.getFileName());
|
|
|
- fileMd5.setGroupCode(fileInfo.getGroupCode());
|
|
|
- fileMd5.setFileSize(fileInfo.getFileSize());
|
|
|
- fileMd5 = FileMd5ClientFacade.insert(fileMd5);
|
|
|
- FileStorageFactory.getService().upload(fileMd5.getFileBucket(), fileMd5.getFilePath(), in);
|
|
|
+ if (null!=fileMd5
|
|
|
+ && FileCommonConst.UPLOAD_PART.equals(fileMd5.getUploadStatus())){
|
|
|
+ // 5.文件为断点续传,更改MD5文件信息的文件状态
|
|
|
+ fileMd5.setFileSize(fileInfo.getFileSize());
|
|
|
+ fileMd5.setFileBucket(fileInfo.getFileBucket());
|
|
|
+ fileMd5.setUploadStatus(FileCommonConst.UPLOAD_SUCCESS);
|
|
|
+ fileMd5.setFileName(fileInfo.getFileName());
|
|
|
+ fileMd5.setGroupCode(fileInfo.getGroupCode());
|
|
|
+ fileMd5=FileMd5ClientFacade.ensureFilePath(fileMd5);
|
|
|
+ FileMd5ClientFacade.update(fileMd5);
|
|
|
+ }else {
|
|
|
+ // 6.没上传过,新建MD5文件信息
|
|
|
+ fileMd5 = FileMd5Creator.of(fileInfo.getFileBucket(), fileMd5Str, FileCommonConst.UPLOAD_SUCCESS);
|
|
|
+ fileMd5.setFileName(fileInfo.getFileName());
|
|
|
+ fileMd5.setGroupCode(fileInfo.getGroupCode());
|
|
|
+ fileMd5.setFileSize(fileInfo.getFileSize());
|
|
|
+ fileMd5 = FileMd5ClientFacade.insert(fileMd5);
|
|
|
+ }
|
|
|
+ FileStorageFactory.getService().upload(fileMd5.getFileBucket(), fileMd5.getFilePath(), IoUtil.toStream(streamBytes));
|
|
|
return saveFileInfo.getId();
|
|
|
}
|
|
|
+ /***
|
|
|
+ * Description: 读取流的字节数组
|
|
|
+ * @param stream : 流
|
|
|
+ * @return : byte[]
|
|
|
+ * @author : lijie
|
|
|
+ * @date :2021/10/23 12:22
|
|
|
+ * Update By lijie 2021/10/23 12:22
|
|
|
+ */
|
|
|
+ private static byte[] getStreamBytes(InputStream stream) {
|
|
|
+ return IoUtil.readBytes(stream);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 上传图片
|
|
@@ -81,13 +107,36 @@ public class FileStorageHelper {
|
|
|
*/
|
|
|
public static String replaceFile(String fileId, String fileName, InputStream in) {
|
|
|
FileInfo fileInfo = FileClientFacade.load(fileId);
|
|
|
- // 先删除文件
|
|
|
- FileStorageFactory.getService().delete(fileInfo.getFileBucket(), fileInfo.getFilePath());
|
|
|
- // 更新信息后上传
|
|
|
+ byte[] streamBytes = getStreamBytes(in);
|
|
|
+ // 1.判断文件是否已存在,如果已存在则更新文件信息关联的MD5值即可,秒传
|
|
|
+ String fileMd5Str = DigestUtil.md5Hex(streamBytes);
|
|
|
+ FileMd5 newFileMd5 = FileMd5ClientFacade.loadByMd5(fileMd5Str);
|
|
|
+ if (null!=newFileMd5){
|
|
|
+ fileInfo.setFileMd5(fileMd5Str);
|
|
|
+ fileInfo.setFileName(fileName);
|
|
|
+ FileClientFacade.update(fileInfo);
|
|
|
+ return fileInfo.getId();
|
|
|
+ }
|
|
|
+ // 2.判断md5关联的文件信息是否只有当前一个,如果只有一个则删除历史文件
|
|
|
+ List<FileInfo> fileInfos = FileClientFacade.queryByFileMd5(fileInfo.getFileMd5());
|
|
|
+ FileMd5 fileMd5 = FileMd5ClientFacade.loadByMd5(fileInfo.getFileMd5());
|
|
|
+ if (CollUtil.isNotEmpty(fileInfos) && fileInfos.size()==1){
|
|
|
+ // 删除MD5文件信息
|
|
|
+ FileMd5ClientFacade.delete(fileMd5.getId());
|
|
|
+ // 删除文件
|
|
|
+ FileStorageFactory.getService().delete(fileMd5.getFileBucket(), fileMd5.getFilePath());
|
|
|
+ }
|
|
|
+ // 3.更新文件信息,然后重新上传文件
|
|
|
+ FileMd5 uploadFileMd5 = FileMd5Creator.of(fileMd5.getFileBucket(), fileMd5Str, FileCommonConst.UPLOAD_SUCCESS);
|
|
|
+ uploadFileMd5.setFileName(fileName);
|
|
|
+ uploadFileMd5.setGroupCode(fileInfo.getGroupCode());
|
|
|
+ uploadFileMd5.setFileSize((long) streamBytes.length);
|
|
|
+ uploadFileMd5 = FileMd5ClientFacade.insert(uploadFileMd5);
|
|
|
+ fileInfo.setFileMd5(fileMd5Str);
|
|
|
fileInfo.setFileName(fileName);
|
|
|
- fileInfo.setFileSize(getStreamSize(in));
|
|
|
- fileInfo = FileClientFacade.update(fileInfo);
|
|
|
- FileStorageFactory.getService().upload(fileInfo.getFileBucket(), fileInfo.getFilePath(), in);
|
|
|
+ FileClientFacade.update(fileInfo);
|
|
|
+ FileStorageFactory.getService().upload(uploadFileMd5.getFileBucket(), uploadFileMd5.getFilePath(),
|
|
|
+ IoUtil.toStream(streamBytes));
|
|
|
return fileInfo.getId();
|
|
|
}
|
|
|
|
|
@@ -128,10 +177,14 @@ public class FileStorageHelper {
|
|
|
* @param fileId 文件id
|
|
|
* @return 文件下载地址
|
|
|
*/
|
|
|
- public static String downloadUrl(String fileId) {
|
|
|
+ public static FileInfo downloadUrl(String fileId) {
|
|
|
FileInfo fileInfo = FileClientFacade.load(fileId);
|
|
|
FileMd5 fileMd5 = FileMd5ClientFacade.loadByMd5(fileInfo.getFileMd5());
|
|
|
- return FileStorageFactory.getService().fetchUrl(fileMd5.getFileBucket(), fileMd5.getFilePath());
|
|
|
+ if (null!=fileMd5){
|
|
|
+ String fetchUrl = FileStorageFactory.getService().fetchUrl(fileMd5.getFileBucket(), fileMd5.getFilePath());
|
|
|
+ fileInfo.setFileDownloadUrl(fetchUrl);
|
|
|
+ }
|
|
|
+ return fileInfo;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -160,12 +213,12 @@ public class FileStorageHelper {
|
|
|
* @param stream
|
|
|
* @return
|
|
|
*/
|
|
|
- private static Long getStreamSize(InputStream stream) {
|
|
|
- try {
|
|
|
- return Long.valueOf(stream.available());
|
|
|
- } catch (IOException e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- throw new BusinessException("文件上传失败!");
|
|
|
- }
|
|
|
- }
|
|
|
+// private static Long getStreamSize(InputStream stream) {
|
|
|
+// try {
|
|
|
+// return Long.valueOf(stream.available());
|
|
|
+// } catch (IOException e) {
|
|
|
+// log.error(e.getMessage(), e);
|
|
|
+// throw new BusinessException("文件上传失败!");
|
|
|
+// }
|
|
|
+// }
|
|
|
}
|