|
@@ -2,16 +2,21 @@ package com.persagy.dmp.file.utils;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpStatus;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import com.persagy.dmp.common.constant.CommonConstant;
|
|
|
+import com.persagy.dmp.common.constant.ResponseCode;
|
|
|
+import com.persagy.dmp.common.context.AppContext;
|
|
|
+import com.persagy.dmp.common.exception.BusinessException;
|
|
|
import com.persagy.dmp.common.helper.SpringHelper;
|
|
|
+import com.persagy.dmp.common.model.response.CommonResult;
|
|
|
import com.persagy.dmp.file.client.FileClientFacade;
|
|
|
-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.file.service.FileMd5Service;
|
|
|
-import com.persagy.dmp.file.service.FileStorageFactory;
|
|
|
-import com.persagy.dmp.file.constant.FileCommonConst;
|
|
|
+import com.persagy.dmp.file.constant.UploadCodeEnum;
|
|
|
+import com.persagy.dmp.file.model.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.io.*;
|
|
@@ -25,74 +30,70 @@ import java.util.List;
|
|
|
@Slf4j
|
|
|
public class FileStorageHelper {
|
|
|
|
|
|
-// /**
|
|
|
-// * 上传文件
|
|
|
-// * @param fileInfo
|
|
|
-// * @param in
|
|
|
-// * @return
|
|
|
-// */
|
|
|
-// public static String uploadFile(FileInfo fileInfo, InputStream in) {
|
|
|
-// // 1.获得文件大小
|
|
|
-// FileMd5Service fileMd5Service = SpringHelper.getBean(FileMd5Service.class);
|
|
|
-// byte[] streamBytes = getStreamBytes(in);
|
|
|
-// fileInfo.setFileSize((long) streamBytes.length);
|
|
|
-// // 2.获得文件的md5值
|
|
|
-// String fileMd5Str = DigestUtil.md5Hex(streamBytes);
|
|
|
-// FileMd5 fileMd5 = fileMd5Service.queryFileMd5ByFileMd5(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=fileMd5Service.ensureFilePath(fileMd5);
|
|
|
-// fileMd5Service.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 = fileMd5Service.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 fileInfo
|
|
|
+ * @param in
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String uploadFile(FileInfo fileInfo, InputStream in) {
|
|
|
+ // 1.获得文件大小
|
|
|
+ byte[] streamBytes = getStreamBytes(in);
|
|
|
+ // 2.获得文件的md5值
|
|
|
+ String fileMd5Str = DigestUtil.md5Hex(streamBytes);
|
|
|
+ FileRequestData requestData = new FileRequestData();
|
|
|
+ requestData.setGroupCode(fileInfo.getGroupCode());
|
|
|
+ requestData.setBusinessId(fileInfo.getBusinessId());
|
|
|
+ requestData.setExpireDate(fileInfo.getExpireDate());
|
|
|
+ requestData.setFileBucket(fileInfo.getFileBucket());
|
|
|
+ requestData.setFileName(fileInfo.getFileName());
|
|
|
+ requestData.setFileSize((long) streamBytes.length);
|
|
|
+ // 3.创建新的存储的fileInfo
|
|
|
+ CommonResult<UploadMes> mesCommonResult = FileClientFacade
|
|
|
+ .initSingleUploadPrototype(fileInfo.getGroupCode(), null, null, null, requestData);
|
|
|
+ if (!CommonConstant.QUERY_SUCCESS.equals(mesCommonResult.getResult())){
|
|
|
+ throw new BusinessException(mesCommonResult.getResult(),mesCommonResult.getMessage());
|
|
|
+ }
|
|
|
+ UploadMes uploadMes = mesCommonResult.getData();
|
|
|
+ List<UploadSubMes> content = uploadMes.getContent();
|
|
|
+ if (UploadCodeEnum.CODE_200.getCode().equals(uploadMes.getUploadCode())){
|
|
|
+ // 秒传
|
|
|
+ return uploadMes.getId();
|
|
|
+ }
|
|
|
+ if (CollUtil.isEmpty(content) || StrUtil.isBlank(content.get(0).getUploadUrl())){
|
|
|
+ throw new BusinessException(ResponseCode.B0300.getCode(),ResponseCode.B0300.getDesc());
|
|
|
+ }
|
|
|
+ HttpResponse response = HttpRequest.put(content.get(0).getUploadUrl()).form("file", streamBytes).execute();
|
|
|
+ if (!response.isOk()){
|
|
|
+ log.error("上传文件出错:{}",response.body());
|
|
|
+ throw new BusinessException(ResponseCode.B0001.getCode(),ResponseCode.B0001.getDesc());
|
|
|
+ }
|
|
|
+ return uploadMes.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
|
|
@@ -192,16 +193,6 @@ public class FileStorageHelper {
|
|
|
// 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;
|
|
|
-// }
|
|
|
|
|
|
/**
|
|
|
* 获取文件大小
|