Procházet zdrojové kódy

新增上传文件

wangliang před 5 roky
rodič
revize
527974e239

+ 16 - 2
themis-backend/src/main/java/com/qmth/themis/backend/api/SysController.java

@@ -1,6 +1,7 @@
 package com.qmth.themis.backend.api;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.qmth.themis.backend.config.DictionaryConfig;
 import com.qmth.themis.backend.util.ServletUtil;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.entity.TBAttachment;
@@ -46,6 +47,9 @@ public class SysController {
     @Resource
     TBAttachmentService tbAttachmentService;
 
+    @Resource
+    DictionaryConfig dictionaryConfig;
+
     @ApiOperation(value = "菜单查询接口")
     @RequestMapping(value = "/getMenu", method = RequestMethod.GET)
     @ApiResponses({@ApiResponse(code = 200, message = "菜单信息", response = TBPrivilege.class)})
@@ -85,9 +89,19 @@ public class SysController {
     @RequestMapping(value = "/file/upload", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "{\"id\":0}", response = Result.class)})
     public Result fileUpload(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file, HttpServletRequest request) throws IOException {
-        TBAttachment tbAttachment = tbAttachmentService.saveAttachment(file, request.getHeader(SystemConstant.MD5), request.getHeader(SystemConstant.PATH));
+        Map<String, Object> mapParameter = new HashMap<>();
+        mapParameter.put(SystemConstant.END_POINT, dictionaryConfig.aliYunOssDomain().getEndpoint());
+        mapParameter.put(SystemConstant.ACCESS_KEY_ID, dictionaryConfig.aliYunOssDomain().getAccessKeyId());
+        mapParameter.put(SystemConstant.ACCESS_KEY_SECRET, dictionaryConfig.aliYunOssDomain().getAccessKeySecret());
+        mapParameter.put(SystemConstant.BUCKET, dictionaryConfig.aliYunOssDomain().getBucket());
+        mapParameter.put(SystemConstant.OSS, dictionaryConfig.sysDomain().isOss());
+        mapParameter.put(SystemConstant.ATTACHMENT_TYPE, dictionaryConfig.sysDomain().getAttachmentType());
+        mapParameter.put(SystemConstant.LOCALHOST_PATH, dictionaryConfig.sysDomain().getLocalhostPath());
+        mapParameter.put(SystemConstant.BASEPHOTO_PATH, dictionaryConfig.sysDomain().getBasePhotoPath());
+        TBUser tbUser = (TBUser) ServletUtil.getRequestAccount(request);
+        TBAttachment tbAttachment = tbAttachmentService.saveAttachment(file, request.getHeader(SystemConstant.MD5), request.getHeader(SystemConstant.PATH), mapParameter, tbUser.getOrgId(), tbUser.getId());
         if (Objects.isNull(tbAttachment)) {
-            throw new BusinessException("保存附件失败");
+            throw new BusinessException("上传文件失败");
         }
         Map map = new HashMap();
         map.put(SystemConstant.ID, tbAttachment.getId());

+ 5 - 4
themis-backend/src/main/resources/application.properties

@@ -118,16 +118,17 @@ aliyun.oss.url=http://${aliyun.oss.bucket}.${aliyun.oss.name}
 
 #\u7CFB\u7EDF\u914D\u7F6E
 sys.config.oss=false
-#sys.config.localhostPath=upload
-#sys.config.attachmentType=.xlsx,.xls,.doc,.docx,.pdf,.jpg,.jpeg,.png,.html
-#sys.config.serverUpload=/Users/king/git/themis-service/
+sys.config.localhostPath=upload
+sys.config.basePhotoPath=base_photo
+sys.config.attachmentType=.xlsx,.xls,.doc,.docx,.pdf,.jpg,.jpeg,.png,.html
+sys.config.serverUpload=/Users/king/git/themis-server/
 #\u7F51\u5173accessKey\u548Csecret,\u6D4B\u8BD5\u7528
 #sys.config.gatewayAccessKey=LTAI4FhEmrrhh27vzPGh25xe
 #sys.config.gatewayAccessSecret=lgnWDUMRAhWBIn4bvAEg2ZC9ECB0Of
 #sys.config.deviceId=1
 #sys.config.fileHost=localhost:7001
 #sys.config.serverHost=localhost:7001
-#spring.resources.static-locations=file:${sys.config.serverUpload},classpath:/META-INF/resources/,classpath:/resources/
+spring.resources.static-locations=file:${sys.config.serverUpload},classpath:/META-INF/resources/,classpath:/resources/
 
 #============================================================================
 # \u914D\u7F6Erocketmq

+ 3 - 0
themis-business/src/main/java/com/qmth/themis/business/constant/SystemConstant.java

@@ -50,6 +50,9 @@ public class SystemConstant {
     public static final String PATH = "path";
     public static final String ID = "id";
     public static final String MD5 = "md5";
+    public static final String ATTACHMENT_TYPE = "attachmentType";
+    public static final String LOCALHOST_PATH = "localhostPath";
+    public static final String BASEPHOTO_PATH = "basePhotoPath";
     /**
      * session过期时间
      */

+ 42 - 0
themis-business/src/main/java/com/qmth/themis/business/domain/SysDomain.java

@@ -1,5 +1,7 @@
 package com.qmth.themis.business.domain;
 
+import java.util.List;
+
 /**
  * @Description: 系统配置
  * @Param:
@@ -11,6 +13,14 @@ public class SysDomain {
 
     boolean oss;
 
+    String localhostPath;
+
+    String basePhotoPath;
+
+    List<String> attachmentType;
+
+    String serverUpload;
+
     public boolean isOss() {
         return oss;
     }
@@ -18,4 +28,36 @@ public class SysDomain {
     public void setOss(boolean oss) {
         this.oss = oss;
     }
+
+    public String getLocalhostPath() {
+        return localhostPath;
+    }
+
+    public void setLocalhostPath(String localhostPath) {
+        this.localhostPath = localhostPath;
+    }
+
+    public String getBasePhotoPath() {
+        return basePhotoPath;
+    }
+
+    public void setBasePhotoPath(String basePhotoPath) {
+        this.basePhotoPath = basePhotoPath;
+    }
+
+    public List<String> getAttachmentType() {
+        return attachmentType;
+    }
+
+    public void setAttachmentType(List<String> attachmentType) {
+        this.attachmentType = attachmentType;
+    }
+
+    public String getServerUpload() {
+        return serverUpload;
+    }
+
+    public void setServerUpload(String serverUpload) {
+        this.serverUpload = serverUpload;
+    }
 }

+ 1 - 1
themis-business/src/main/java/com/qmth/themis/business/entity/TBAttachment.java

@@ -52,7 +52,7 @@ public class TBAttachment implements Serializable {
     private String remark; //备注
 
     @CreatedBy
-    @TableField("create_by")
+    @TableField("create_id")
     @ApiModelProperty(value = "创建人id")
     private Long createId; //创建人id
 

+ 8 - 3
themis-business/src/main/java/com/qmth/themis/business/service/TBAttachmentService.java

@@ -23,22 +23,27 @@ public interface TBAttachmentService extends IService<TBAttachment> {
      * @param file
      * @param md5
      * @param path
+     * @param map
+     * @param orgId
+     * @param userId
      * @return
      * @throws IOException
      */
-    TBAttachment saveAttachment(MultipartFile file, String md5, String path) throws IOException;
+    TBAttachment saveAttachment(MultipartFile file, String md5, String path, Map<String, Object> map, Long orgId, Long userId) throws IOException;
 
     /**
      * 删除附件
      *
+     * @param map
      * @param tbAttachment
      */
-    void deleteAttachment(TBAttachment tbAttachment);
+    void deleteAttachment(Map<String, Object> map, TBAttachment tbAttachment);
 
     /**
      * 批量删除附件
      *
      * @param map
+     * @param list
      */
-    void batchDeleteAttachment(List<Map> map);
+    void batchDeleteAttachment(Map<String, Object> map, List<Map> list);
 }

+ 24 - 28
themis-business/src/main/java/com/qmth/themis/business/service/impl/TBAttachmentServiceImpl.java

@@ -6,10 +6,7 @@ import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.dao.TBAttachmentMapper;
 import com.qmth.themis.business.entity.TBAttachment;
 import com.qmth.themis.business.service.TBAttachmentService;
-import com.qmth.themis.business.util.JacksonUtil;
 import com.qmth.themis.business.util.OssUtil;
-import com.qmth.themis.business.util.RedisUtil;
-import com.qmth.themis.common.enums.ExceptionResultEnum;
 import com.qmth.themis.common.exception.BusinessException;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.io.FileUtils;
@@ -17,7 +14,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.util.FileCopyUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
@@ -47,24 +43,27 @@ public class TBAttachmentServiceImpl extends ServiceImpl<TBAttachmentMapper, TBA
      * @param file
      * @param md5
      * @param path
+     * @param map
+     * @param orgId
+     * @param userId
      * @return
      * @throws IOException
      */
     @Override
     @Transactional
-    public TBAttachment saveAttachment(MultipartFile file, String md5, String path) throws IOException {
+    public TBAttachment saveAttachment(MultipartFile file, String md5, String path, Map<String, Object> map, Long orgId, Long userId) throws IOException {
         TBAttachment tbAttachment = null;
         try {
             int temp = file.getOriginalFilename().indexOf(".");
             String fileName = file.getOriginalFilename().substring(0, temp);
             String format = file.getOriginalFilename().substring(temp, file.getOriginalFilename().length());
-//            List<String> attachmentTypeList = dictionaryConfig.sysDomain().getAttachmentType();
-//            if (Objects.nonNull(format)) {
-//                long count = attachmentTypeList.stream().filter(s -> format.equalsIgnoreCase(s)).count();
-//                if (count == 0) {
-//                    throw new BusinessException("文件格式只能为" + attachmentTypeList.toString());
-//                }
-//            }
+            List<String> attachmentTypeList = (List<String>) map.get(SystemConstant.ATTACHMENT_TYPE);
+            if (Objects.nonNull(format)) {
+                long count = attachmentTypeList.stream().filter(s -> format.equalsIgnoreCase(s)).count();
+                if (count == 0) {
+                    throw new BusinessException("文件格式只能为" + attachmentTypeList.toString());
+                }
+            }
             long size = file.getSize();
             BigDecimal b = new BigDecimal(size);
             BigDecimal num = new BigDecimal(1024);
@@ -92,19 +91,18 @@ public class TBAttachmentServiceImpl extends ServiceImpl<TBAttachmentMapper, TBA
             tbAttachment.setSize(b);
             tbAttachment.setMd5(fileMd5);
 
-            boolean oss = true;
-//            boolean oss = dictionaryConfig.sysDomain().isOss();
+            boolean oss = (boolean) map.get(SystemConstant.OSS);
             LocalDateTime nowTime = LocalDateTime.now();
             StringJoiner stringJoiner = new StringJoiner("")
-//                    .add(dictionaryConfig.sysDomain().getLocalhostPath())
-//                    .add(File.separator).add(String.valueOf(schoolId))
+                    .add(String.valueOf(map.get(SystemConstant.LOCALHOST_PATH)))
+                    .add(File.separator).add(String.valueOf(orgId))
                     .add(File.separator).add(String.valueOf(nowTime.getYear()))
                     .add(File.separator).add(String.valueOf(nowTime.getMonthValue()))
                     .add(File.separator).add(String.valueOf(nowTime.getDayOfMonth()));
             if (oss) {//上传至oss
                 stringJoiner.add(File.separator).add(String.valueOf(UUID.randomUUID()).replaceAll("-", ""))
                         .add(tbAttachment.getType());
-//                ossUtil.ossUpload(stringJoiner.toString(), file.getInputStream());
+                ossUtil.ossUpload(map, stringJoiner.toString(), file.getInputStream());
             } else {//上传至服务器
                 File mkdir = new File(stringJoiner.toString());
                 if (!mkdir.exists()) {
@@ -122,15 +120,11 @@ public class TBAttachmentServiceImpl extends ServiceImpl<TBAttachmentMapper, TBA
             }
             jsonObject.put(SystemConstant.PATH, stringJoiner.toString());
             tbAttachment.setRemark(jsonObject.toJSONString());
-//            TcPBasicUser currentUser = (TcPBasicUser) RedisUtil.getUser(userId);
-//            if (Objects.isNull(currentUser)) {
-//                throw new BusinessException(ExceptionResultEnum.LOGIN_NO);
-//            }
-//            tbAttachment.setCreateId(currentUser.getId());
+            tbAttachment.setCreateId(userId);
             this.save(tbAttachment);
         } catch (Exception e) {
             e.printStackTrace();
-            deleteAttachment(tbAttachment);
+            deleteAttachment(map, tbAttachment);
             if (e instanceof BusinessException) {
                 throw new BusinessException(e.getMessage());
             } else {
@@ -143,15 +137,16 @@ public class TBAttachmentServiceImpl extends ServiceImpl<TBAttachmentMapper, TBA
     /**
      * 删除附件
      *
+     * @param map
      * @param tbAttachment
      */
     @Override
-    public void deleteAttachment(TBAttachment tbAttachment) {
+    public void deleteAttachment(Map<String, Object> map, TBAttachment tbAttachment) {
         if (Objects.nonNull(tbAttachment) && Objects.nonNull(tbAttachment.getRemark())) {
             JSONObject jsonObject = JSONObject.parseObject(tbAttachment.getRemark());
             String type = String.valueOf(jsonObject.get(SystemConstant.TYPE));
             if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.OSS)) {//删除阿里云附件
-//                ossUtil.ossDelete(jsonObject.get(SystemConstant.PATH).toString());
+                ossUtil.ossDelete(map, jsonObject.get(SystemConstant.PATH).toString());
             } else {//删除服务器附件
                 File file = new File(jsonObject.get(SystemConstant.PATH).toString());
                 file.delete();
@@ -163,21 +158,22 @@ public class TBAttachmentServiceImpl extends ServiceImpl<TBAttachmentMapper, TBA
      * 批量删除附件
      *
      * @param map
+     * @param list
      */
     @Override
-    public void batchDeleteAttachment(List<Map> map) {
+    public void batchDeleteAttachment(Map<String, Object> map, List<Map> list) {
         List<String> paths = null;
         String type = null;
         if (Objects.nonNull(map)) {
             paths = new ArrayList<>();
-            for (Map m : map) {
+            for (Map m : list) {
                 JSONObject jsonObject = JSONObject.parseObject((String) m.get("remark"));
                 type = (String) jsonObject.get("type");
                 paths.add((String) jsonObject.get("path"));
             }
         }
         if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.OSS)) {//删除阿里云附件
-//            ossUtil.ossBatchDelete(paths);
+            ossUtil.ossBatchDelete(map, paths);
         } else if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.LOCAL)) {//删除服务器附件
             for (String s : paths) {
                 File file = new File(s);