|
@@ -1,224 +0,0 @@
|
|
|
-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;
|
|
|
-import com.persagy.dmp.file.client.FileClientFacade;
|
|
|
-import com.persagy.dmp.file.client.FileMd5ClientFacade;
|
|
|
-import com.persagy.dmp.file.model.FileInfo;
|
|
|
-import com.persagy.dmp.file.model.FileInfoCreator;
|
|
|
-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;
|
|
|
-
|
|
|
-/**
|
|
|
- * 文件存储助手
|
|
|
- * @author Charlie Yu
|
|
|
- * @date 2021-05-15
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-public class FileStorageHelper {
|
|
|
-
|
|
|
- /**
|
|
|
- * 上传文件
|
|
|
- * @param fileInfo
|
|
|
- * @param in
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String uploadFile(FileInfo fileInfo, InputStream in) {
|
|
|
- // 1.获得文件大小
|
|
|
- byte[] streamBytes = getStreamBytes(in);
|
|
|
- fileInfo.setFileSize((long) streamBytes.length);
|
|
|
- // 2.获得文件的md5值
|
|
|
- String fileMd5Str = DigestUtil.md5Hex(streamBytes);
|
|
|
- FileMd5 fileMd5 = FileMd5ClientFacade.loadByMd5(fileMd5Str);
|
|
|
- // 3.创建新的存储的fileInfo
|
|
|
- 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();
|
|
|
- }
|
|
|
- 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);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 上传图片
|
|
|
- * @param fileInfo
|
|
|
- * @param in
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String uploadImage(FileInfo fileInfo, InputStream in) {
|
|
|
- // 图片压缩
|
|
|
- InputStream cutIn = ImageHelper.cutImage(in);
|
|
|
- return uploadFile(fileInfo, cutIn);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 修改文件
|
|
|
- * @param fileId
|
|
|
- * @param fileName
|
|
|
- * @param in
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String replaceFile(String fileId, String fileName, InputStream in) {
|
|
|
- FileInfo fileInfo = FileClientFacade.load(fileId);
|
|
|
- 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);
|
|
|
- FileClientFacade.update(fileInfo);
|
|
|
- FileStorageFactory.getService().upload(uploadFileMd5.getFileBucket(), uploadFileMd5.getFilePath(),
|
|
|
- IoUtil.toStream(streamBytes));
|
|
|
- return fileInfo.getId();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新图片
|
|
|
- * @param fileId
|
|
|
- * @param fileName
|
|
|
- * @param in
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String replaceImage(String fileId, String fileName, InputStream in) {
|
|
|
- // 图片压缩
|
|
|
- InputStream cutIn = ImageHelper.cutImage(in);
|
|
|
- return replaceFile(fileId, fileName, cutIn);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除文件及图片
|
|
|
- * @param fileId
|
|
|
- */
|
|
|
- public static void deleteFile(String fileId) {
|
|
|
- FileInfo fileInfo = FileClientFacade.load(fileId);
|
|
|
- List<FileInfo> fileInfos = FileClientFacade.queryByFileMd5(fileInfo.getFileMd5());
|
|
|
- if (CollUtil.isNotEmpty(fileInfos) && fileInfos.size()>1){
|
|
|
- // 多余一个绑定到相同的md5文件上,仅删除文件信息即可
|
|
|
- FileClientFacade.delete(fileId);
|
|
|
- return;
|
|
|
- }
|
|
|
- // 只有一个绑定到相同的md5文件上,删除文件信息,文件MD5信息,文件信息
|
|
|
- FileMd5 fileMd5 = FileMd5ClientFacade.loadByMd5(fileInfo.getFileMd5());
|
|
|
- FileClientFacade.delete(fileId);
|
|
|
- FileMd5ClientFacade.delete(fileMd5.getId());
|
|
|
- FileStorageFactory.getService().delete(fileMd5.getFileBucket(), fileMd5.getFilePath());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 下载
|
|
|
- * @param fileId 文件id
|
|
|
- * @return 文件下载地址
|
|
|
- */
|
|
|
- public static FileInfo downloadUrl(String fileId) {
|
|
|
- FileInfo fileInfo = FileClientFacade.load(fileId);
|
|
|
- FileMd5 fileMd5 = FileMd5ClientFacade.loadByMd5(fileInfo.getFileMd5());
|
|
|
- if (null!=fileMd5){
|
|
|
- String fetchUrl = FileStorageFactory.getService().fetchUrl(fileMd5.getFileBucket(), fileMd5.getFilePath());
|
|
|
- fileInfo.setFileDownloadUrl(fetchUrl);
|
|
|
- }
|
|
|
- return fileInfo;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 下载
|
|
|
- * @param fileId 文件id
|
|
|
- * @return 文件流
|
|
|
- */
|
|
|
- public static InputStream downloadStream(String fileId) {
|
|
|
- FileInfo fileInfo = FileClientFacade.load(fileId);
|
|
|
- FileMd5 fileMd5 = FileMd5ClientFacade.loadByMd5(fileInfo.getFileMd5());
|
|
|
- return FileStorageFactory.getService().download(fileMd5.getFileBucket(), fileMd5.getFilePath());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 下载
|
|
|
- * @param fileId 文件id
|
|
|
- * @return 文件内容
|
|
|
- */
|
|
|
- public static byte[] downloadContent(String fileId) {
|
|
|
- // TODO 暂不提供,依赖前端显示规则
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取文件大小
|
|
|
- * @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("文件上传失败!");
|
|
|
-// }
|
|
|
-// }
|
|
|
-}
|