Pārlūkot izejas kodu

功能:向前兼容文件服务,实现部分接口

lijie 3 gadi atpakaļ
vecāks
revīzija
0f598cb85e

+ 2 - 2
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/config/AccountConfig.java

@@ -1,6 +1,7 @@
 package com.persagy.dmp.file.config;
 
 import lombok.Data;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Configuration;
 
@@ -17,7 +18,6 @@ import java.util.Map;
 @Configuration
 @ConfigurationProperties(prefix = "persagy.common.token")
 public class AccountConfig {
-
-    public static Map<String,String> accountMap;
+    private Map<String,String> accountMap;
 
 }

+ 0 - 1
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/config/FileWebConfigurer.java

@@ -11,7 +11,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
 @Configuration
 @Order(100)
-@ConditionalOnProperty(value = "persagy.common.file.storage", havingValue = "2")
 public class FileWebConfigurer implements WebMvcConfigurer {
     /***
      * Description: 旧文件的上下文处理器

+ 4 - 0
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/context/OldFileContext.java

@@ -21,6 +21,8 @@ public class OldFileContext {
     public static final String SECRET = "secret";
     /** 文件key */
     public static final String KEY = "key";
+    /** 文件key */
+    public static final String OVERWRITE = "overwrite";
     /** 常用的systemId */
     public static final String DATA_PLATFORM = "dataPlatform";
     /** 常用的systemId */
@@ -32,5 +34,7 @@ public class OldFileContext {
     private String secret;
     /** 数据源名称 */
     private String key;
+    /** 是否覆盖,true-覆盖,false-非覆盖 */
+    private Boolean overwrite=false;
 
 }

+ 52 - 22
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/controller/CompatibleOldFileController.java

@@ -1,20 +1,19 @@
 package com.persagy.dmp.file.controller;
 
 import cn.hutool.core.io.FileUtil;
-import cn.hutool.core.io.IoUtil;
-import cn.hutool.core.text.CharSequenceUtil;
-import cn.hutool.core.util.CharsetUtil;
 import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSONObject;
 import com.persagy.dmp.common.utils.ResourceUtil;
+import com.persagy.dmp.common.utils.ResultHelper;
+import com.persagy.dmp.file.service.CompatibleOldFileService;
 import lombok.RequiredArgsConstructor;
-import org.apache.commons.io.IOUtils;
 import org.springframework.core.io.Resource;
 import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.core.MediaType;
 import java.io.IOException;
-import java.io.StringWriter;
 import java.nio.charset.Charset;
 
 /***
@@ -32,6 +31,8 @@ public class CompatibleOldFileController {
     private static final String ERROR_HTML="<html><head>" +
             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>" +
             "<title>image-service</title></head></html><body>{}</body>";
+    private final CompatibleOldFileService compatibleOldFileService;
+
     /***
      * Description: 获取接口文档
      * @param response : 响应体
@@ -67,23 +68,52 @@ public class CompatibleOldFileController {
      * Update By lijie 2021/11/19 9:58
      */
     @PostMapping(value = "/common/file_upload",produces = {MediaType.APPLICATION_JSON})
-    public void fileUpload(HttpServletResponse response) throws IOException {
-        Resource[] docResources = ResourceUtil.getResource(DOCUMENTATION, DOCUMENTATION);
-        Resource[] templateResources = ResourceUtil.getResource(TEMPLATE, TEMPLATE);
-        if (templateResources.length<=0
-                || docResources.length<=0
-                || !templateResources[0].exists()
-                || !docResources[0].exists()){
-            response.getWriter()
-                    .write(StrUtil.format(ERROR_HTML,"暂无内容"));
-            return;
-        }
-        Resource docResource = docResources[0];
-        Resource templateResource = templateResources[0];
-        String tempStr = FileUtil.readString(templateResource.getFile(), Charset.defaultCharset());
-        String docContentStr = FileUtil.readString(docResource.getFile(), Charset.defaultCharset());
-        response.getWriter()
-                .write(StrUtil.replace(tempStr, "#replaceMePlease#", docContentStr));
+    public void fileUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
+        response.getWriter().write(compatibleOldFileService.uploadFile(request.getInputStream(),response));
+    }
+    /***
+     * Description: 图片上传
+     * @return : void
+     * @author : lijie
+     * @date :2021/11/19 9:58
+     * Update By lijie 2021/11/19 9:58
+     */
+    @PostMapping(value = "/common/image_upload",produces = {MediaType.APPLICATION_JSON},consumes = {"image/jpg"})
+    public void imageUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
+        response.getWriter().write(compatibleOldFileService.uploadFile(request.getInputStream(),response));
+    }
+    /***
+     * Description: 图片下载
+     * @return : void
+     * @author : lijie
+     * @date :2021/11/19 9:58
+     * Update By lijie 2021/11/19 9:58
+     */
+    @GetMapping(value = "/common/image_get",produces = {"image/jpg"})
+    public void imageGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+        compatibleOldFileService.getFile(request,response);
+    }
+    /***
+     * Description: 图片下载
+     * @return : void
+     * @author : lijie
+     * @date :2021/11/19 9:58
+     * Update By lijie 2021/11/19 9:58
+     */
+    @GetMapping(value = "/common/svg_get",produces = {"image/svg+xml"})
+    public void imageSvgGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+        compatibleOldFileService.getFile(request,response);
+    }
+    /***
+     * Description: 图片下载
+     * @return : void
+     * @author : lijie
+     * @date :2021/11/19 9:58
+     * Update By lijie 2021/11/19 9:58
+     */
+    @GetMapping(value = "/common/images_list",produces = {MediaType.APPLICATION_JSON})
+    public void imagesList(@RequestBody(required = false) JSONObject param, HttpServletResponse response) throws IOException {
+        compatibleOldFileService.listFiles(param,response);
     }
 
 

+ 5 - 2
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/handler/OldFileAuthHandler.java

@@ -2,6 +2,7 @@ package com.persagy.dmp.file.handler;
 
 import cn.hutool.core.util.StrUtil;
 import com.persagy.dmp.common.exception.BusinessException;
+import com.persagy.dmp.common.helper.SpringHelper;
 import com.persagy.dmp.file.config.AccountConfig;
 import com.persagy.dmp.file.context.OldFileAppContext;
 import com.persagy.dmp.file.context.OldFileContext;
@@ -10,6 +11,7 @@ import org.springframework.web.servlet.ModelAndView;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.util.Map;
 
 public class OldFileAuthHandler implements HandlerInterceptor {
 
@@ -20,8 +22,9 @@ public class OldFileAuthHandler implements HandlerInterceptor {
         if (StrUtil.isBlank(context.getSystemId()) || StrUtil.isBlank(context.getSecret())){
             throw new BusinessException("文件服务器密钥有误");
         }
-        if (!AccountConfig.accountMap.containsKey(context.getSystemId())
-                || !context.getSecret().equals(AccountConfig.accountMap.get(context.getSystemId()))){
+        Map<String, String> accountMap = SpringHelper.getBean(AccountConfig.class).getAccountMap();
+        if (!accountMap.containsKey(context.getSystemId())
+                || !context.getSecret().equals(accountMap.get(context.getSystemId()))){
             throw new BusinessException("文件服务器密钥有误");
         }
         return true;

+ 2 - 0
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/handler/OldFileContextHandler.java

@@ -1,5 +1,6 @@
 package com.persagy.dmp.file.handler;
 
+import cn.hutool.core.util.BooleanUtil;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.persagy.dmp.file.context.OldFileAppContext;
@@ -19,6 +20,7 @@ public class OldFileContextHandler implements HandlerInterceptor {
         context.setSystemId(request.getParameter(OldFileContext.SYSTEM_ID));
         context.setSecret(request.getParameter(OldFileContext.SECRET));
         context.setKey(request.getParameter(OldFileContext.KEY));
+        context.setOverwrite(BooleanUtil.toBoolean(request.getParameter(OldFileContext.OVERWRITE)));
         return true;
     }
 

+ 47 - 0
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/CompatibleOldFileService.java

@@ -0,0 +1,47 @@
+package com.persagy.dmp.file.service;
+
+import com.alibaba.fastjson.JSONObject;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.InputStream;
+import java.util.List;
+
+/***
+ * Description: 向前兼容文件服务接口
+ * @author : lijie
+ * @date :2021/12/8 14:27
+ * Update By lijie 2021/12/8 14:27
+ */
+public interface CompatibleOldFileService {
+    /***
+     * Description: 上传文件
+     * @param inputStream : 文件流
+     * @param response : 响应对象
+     * @return : void
+     * @author : lijie
+     * @date :2021/12/8 14:29
+     * Update By lijie 2021/12/8 14:29
+     */
+    String uploadFile(InputStream inputStream, HttpServletResponse response);
+    /***
+     * Description: 下载文件
+     * @param request : 请求参数
+     * @param response : 响应参数
+     * @return : void
+     * @author : lijie
+     * @date :2021/12/8 17:06
+     * Update By lijie 2021/12/8 17:06
+     */
+    void getFile(HttpServletRequest request, HttpServletResponse response);
+    /***
+     * Description: 列出文件key
+     * @param param : 请求参数
+     * @param response : 响应体
+     * @return : void
+     * @author : lijie
+     * @date :2021/12/8 18:22
+     * Update By lijie 2021/12/8 18:22
+     */
+    List<String> listFiles(JSONObject param, HttpServletResponse response);
+}

+ 14 - 0
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/IFileService.java

@@ -1,5 +1,6 @@
 package com.persagy.dmp.file.service;
 
+import com.persagy.dmp.common.lang.PsDate;
 import com.persagy.dmp.file.model.FileInfo;
 import com.persagy.dmp.file.model.FileRequestData;
 import com.persagy.dmp.file.model.UploadMes;
@@ -99,4 +100,17 @@ public interface IFileService {
      * Update By lijie 2021/10/23 13:18
      */
     FileInfo initFileDownload(FileRequestData requestData);
+    /***
+     * Description: 秒传逻辑
+     * @param fileName : 文件名称
+     * @param groupCode : 集团编码
+     * @param businessId : 业务id
+     * @param expireDate : 过期时间
+     * @return : com.persagy.dmp.file.domain.FileView
+     * @author : lijie
+     * @date :2021/10/22 10:03
+     * Update By lijie 2021/10/22 10:03
+     */
+    UploadMes secondTransmission(String fileName, String groupCode,
+                                 String businessId, PsDate expireDate, String fileMd5, String fileId);
 }

+ 179 - 0
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/impl/CompatibleOldFileServiceImpl.java

@@ -0,0 +1,179 @@
+package com.persagy.dmp.file.service.impl;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.crypto.digest.DigestUtil;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
+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.lang.PsDateTime;
+import com.persagy.dmp.file.constant.FileCommonConst;
+import com.persagy.dmp.file.constant.UploadCodeEnum;
+import com.persagy.dmp.file.context.OldFileAppContext;
+import com.persagy.dmp.file.context.OldFileContext;
+import com.persagy.dmp.file.dao.FileMapper;
+import com.persagy.dmp.file.model.*;
+import com.persagy.dmp.file.service.*;
+import com.persagy.dmp.file.utils.FileStorageHelper;
+import com.sun.imageio.plugins.common.ImageUtil;
+import lombok.RequiredArgsConstructor;
+import lombok.SneakyThrows;
+import org.springframework.stereotype.Service;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+/***
+ * Description: 向前兼容旧的文件服务
+ * @author : lijie
+ * @date :2021/12/8 15:17
+ * Update By lijie 2021/12/8 15:17
+ */
+@Service
+@RequiredArgsConstructor
+public class CompatibleOldFileServiceImpl implements CompatibleOldFileService {
+
+    private static final String PREFIX = "prefix";
+    private static final String MAX_COUNT = "maxCount";
+    private static final String FROM = "from";
+
+    private final FileMd5Service fileMd5Service;
+    private final IFileService fileService;
+    private final FileMapper fileMapper;
+
+    /***
+     * Description: 上传文件
+     * @param inputStream : 文件流
+     * @return : void
+     * @author : lijie
+     * @date :2021/12/8 15:20
+     * Update By lijie 2021/12/8 15:20
+     */
+    @Override
+    public String uploadFile(InputStream inputStream, HttpServletResponse response) {
+        if(StrUtil.isBlank(OldFileAppContext.getContext().getKey())){
+            return "Incorrect parameters inputted.";
+        }
+        // 1.如果非覆盖且文件信息已存在则直接返回文件已存在
+        if (!OldFileAppContext.getContext().getOverwrite() &&
+            null!=fileService.load(OldFileAppContext.getContext().getKey())){
+            return "File existed.";
+        }
+        IFileStorageService service = FileStorageFactory.getService();
+        // 1.获得文件大小
+        byte[] streamBytes = FileStorageHelper.getStreamBytes(inputStream);
+        String fileMd5Str = DigestUtil.md5Hex(streamBytes);
+        // 1.根据文件md5值查询数据库
+        FileMd5 fileMd5 = fileMd5Service.queryFileMd5ByFileMd5(fileMd5Str);
+        // 2.历史已上传但未上传完成
+        if (ObjectUtil.isNotNull(fileMd5)
+                && FileCommonConst.UPLOAD_PART.equals(fileMd5.getUploadStatus())) {
+            // 2.1 补充文件md5值
+            fileMd5.setFileBucket(OldFileAppContext.getContext().getSystemId());
+            fileMd5.setUploadStatus(FileCommonConst.UPLOAD_SUCCESS);
+            fileMd5.setFileSize((long)streamBytes.length);
+            fileMd5.setCreationTime(new PsDateTime());
+            fileMd5Service.ensureFilePath(fileMd5,OldFileAppContext.getContext().getKey());
+            // 2.2 上传文件
+            service.upload(fileMd5.getFileBucket(),fileMd5.getFilePath(), IoUtil.toStream(streamBytes));
+            // 2.3 更新文件md5信息
+            fileMd5Service.updateById(fileMd5);
+            // 2.4 创建文件信息
+            fileService.secondTransmission(OldFileAppContext.getContext().getKey(), CommonConstant.DEFAULT_ID,
+                    null, null, fileMd5.getFileMd5(), OldFileAppContext.getContext().getKey());
+            return FileStorageHelper.oldSuccessResultCreate("");
+        }
+        // 3.历史已上传且已完成上传
+        if (ObjectUtil.isNotNull(fileMd5)
+                && FileCommonConst.UPLOAD_SUCCESS.equals(fileMd5.getUploadStatus())
+                && service.exists(fileMd5.getFileBucket(),fileMd5.getFilePath())){
+            // 秒传
+            fileService.secondTransmission(OldFileAppContext.getContext().getKey(),CommonConstant.DEFAULT_ID,
+                    null, null,fileMd5.getFileMd5(),OldFileAppContext.getContext().getKey());
+            return FileStorageHelper.oldSuccessResultCreate("");
+        }
+        // 4.历史已上传且未完成上传
+        if (ObjectUtil.isNotNull(fileMd5)
+                && FileCommonConst.UPLOAD_SUCCESS.equals(fileMd5.getUploadStatus())){
+            // 4.1 上传文件
+            service.upload(fileMd5.getFileBucket(),fileMd5.getFilePath(), IoUtil.toStream(streamBytes));
+            // 4.2 秒传
+            fileService.secondTransmission(OldFileAppContext.getContext().getKey(),CommonConstant.DEFAULT_ID,
+                    null, null,fileMd5.getFileMd5(),OldFileAppContext.getContext().getKey());
+            return FileStorageHelper.oldSuccessResultCreate("");
+        }
+        // 5.新上传文件
+        fileMd5 = FileMd5Creator.of(OldFileAppContext.getContext().getSystemId(), fileMd5Str, FileCommonConst.UPLOAD_SUCCESS);
+        fileMd5.setFileSize((long)streamBytes.length);
+        fileMd5.setCreationTime(new PsDateTime());
+        fileMd5Service.ensureFilePath(fileMd5,OldFileAppContext.getContext().getKey());
+        // 5.1 上传文件
+        service.upload(fileMd5.getFileBucket(),fileMd5.getFilePath(), IoUtil.toStream(streamBytes));
+        // 5.2 保存md5信息
+        fileMd5Service.save(fileMd5);
+        // 5.3 保存文件信息
+        fileService.secondTransmission(OldFileAppContext.getContext().getKey(),CommonConstant.DEFAULT_ID,
+                null, null,fileMd5.getFileMd5(),OldFileAppContext.getContext().getKey());
+        return FileStorageHelper.oldSuccessResultCreate("");
+    }
+
+    /***
+     * Description: 下载文件
+     * @param request : 请求参数
+     * @param response : 响应参数
+     * @return : void
+     * @author : lijie
+     * @date :2021/12/8 17:06
+     * Update By lijie 2021/12/8 17:06
+     */
+    @SneakyThrows
+    @Override
+    public void getFile(HttpServletRequest request, HttpServletResponse response) {
+        // 1.必填参数是否存在
+        if (StrUtil.isBlank(OldFileAppContext.getContext().getSystemId())
+            || StrUtil.isBlank(OldFileAppContext.getContext().getKey())){
+            IoUtil.writeUtf8(response.getOutputStream(),Boolean.FALSE,"Incorrect parameters inputted.");
+            return;
+        }
+        // 2.文件是否存在
+        IFileStorageService service = FileStorageFactory.getService();
+        FileInfo fileInfo = fileMapper.getFileInfoById(OldFileAppContext.getContext().getKey());
+        if (null==fileInfo
+                || StrUtil.isBlank(fileInfo.getFileBucket())
+                || StrUtil.isBlank(fileInfo.getFilePath())
+                || !FileCommonConst.UPLOAD_SUCCESS.equals(fileInfo.getUploadStatus())
+                || !service.exists(fileInfo.getFileBucket(),fileInfo.getFilePath())){
+            IoUtil.writeUtf8(response.getOutputStream(),Boolean.FALSE,"file not existed");
+            return;
+        }
+        // 3.下载文件
+        IoUtil.copy(service.download(fileInfo.getFileBucket(),fileInfo.getFilePath()),response.getOutputStream());
+    }
+    /***
+     * Description: 列出文件key
+     * @param param : 请求参数
+     * @param response : 响应体
+     * @return : void
+     * @author : lijie
+     * @date :2021/12/8 18:22
+     * Update By lijie 2021/12/8 18:22
+     */
+    @Override
+    public List<String> listFiles(JSONObject param, HttpServletResponse response) {
+        if (StrUtil.isBlank(OldFileAppContext.getContext().getSystemId())){
+            return new ArrayList<>();
+        }
+
+
+
+        return null;
+    }
+}

+ 2 - 1
dmp-cloud/dmp-file/src/main/java/com/persagy/dmp/file/service/impl/FileServiceImpl.java

@@ -291,7 +291,8 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, FileInfo> implement
      * @date :2021/10/22 10:03
      * Update By lijie 2021/10/22 10:03
      */
-    private UploadMes secondTransmission(String fileName, String groupCode,
+    @Override
+    public UploadMes secondTransmission(String fileName, String groupCode,
                                          String businessId, PsDate expireDate,String fileMd5,String fileId) {
         // 1.生成文件信息并返回
         FileInfo fileInfo = FileInfoCreator.of(groupCode,fileName,businessId,fileMd5,expireDate);

+ 10 - 1
dmp-comp/dmp-file-starter/src/main/java/com/persagy/dmp/file/utils/FileStorageHelper.java

@@ -8,6 +8,7 @@ import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
 import cn.hutool.http.HttpStatus;
 import cn.hutool.http.HttpUtil;
+import com.alibaba.fastjson.JSONObject;
 import com.persagy.dmp.common.constant.CommonConstant;
 import com.persagy.dmp.common.constant.ResponseCode;
 import com.persagy.dmp.common.context.AppContext;
@@ -84,7 +85,7 @@ public class FileStorageHelper {
      * @date :2021/10/23 12:22
      * Update By lijie 2021/10/23 12:22
      */
-    private static byte[] getStreamBytes(InputStream stream) {
+    public static byte[] getStreamBytes(InputStream stream) {
         return IoUtil.readBytes(stream);
     }
 
@@ -191,4 +192,12 @@ public class FileStorageHelper {
         }
         return IoUtil.toStream(HttpUtil.downloadBytes(info.getFileDownloadUrl()));
     }
+
+    public static String oldSuccessResultCreate(String message){
+        JSONObject json = new JSONObject();
+        json.put("Result",CommonConstant.QUERY_SUCCESS);
+        json.put("ResultMsg",StrUtil.blankToDefault(message,""));
+        return json.toJSONString();
+    }
+
 }