|
@@ -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) {
|
|
|
+
|
|
|
+ byte[] streamBytes = getStreamBytes(in);
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -192,16 +193,6 @@ public class FileStorageHelper {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
|
|
|
|
|
|
* 获取文件大小
|