|
@@ -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;
|
|
|
+ }
|
|
|
+}
|