Ver código fonte

认证通后台【云资料库】上传文件夹功能优化

JiangChongBo 2 anos atrás
pai
commit
ddc2d5f876

+ 8 - 46
src/main/java/com/caimei/controller/admin/data/DatabaseApi.java

@@ -1,9 +1,11 @@
 package com.caimei.controller.admin.data;
 
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.caimei.annotation.CurrentUser;
 import com.caimei.model.ResponseJson;
 import com.caimei.model.po.SysUser;
+import com.caimei.model.vo.DyVideoInfoVo;
 import com.caimei.model.vo.FileTreeVo;
 import com.caimei.service.data.DatabaseService;
 import io.swagger.annotations.Api;
@@ -223,46 +225,13 @@ public class DatabaseApi {
         return databaseService.ossTokenGet();
     }
 
+
     /**
-     * 上传文件夹
-     * @param multipartFiles
+     * 上传文件夹(同一目录可以存在相同命名文件,没有相同名字文件夹)
+     * @param sysUser
+     * @param params
      * @return
-     * @throws IOException
      */
-    @PostMapping("/upload/dictionary")
-    public  ResponseJson uploadDictionary(@RequestParam("file")MultipartFile[] multipartFiles) throws IOException {
-
-//        for (MultipartFile f: multipartFiles) {
-//            //如果是文件夹,创建该文件夹并获取文件夹所有文件
-//            String name="";
-//            File file1;
-//            if (f instanceof CommonsMultipartFile) {
-//
-//                //转换成这个对象,然后我们需要通过里面的FileItem来获得相对路径
-//
-//                CommonsMultipartFile f2 = (CommonsMultipartFile) f;
-//
-//                name = f2.getName();
-//
-//                System.out.println(name + "        ---------相对路径");
-//
-//                file1 = new File("e:" + "/" + name);
-//
-//                file1.mkdirs();
-//
-//                file1.createNewFile();
-//
-//                f.transferTo(file1);
-//
-//            }
-//            System.out.println(f.getOriginalFilename() + "   iii         --------");
-//            //如果是文件,直接添加
-//
-//        }
-        databaseService.uploadDictionary(multipartFiles);
-        return ResponseJson.success();
-    }
-
     @PostMapping("/upload/dictionary")
     public ResponseJson uploadPackage(@CurrentUser SysUser sysUser,@RequestBody String params){
         if (null == sysUser) {
@@ -274,14 +243,7 @@ public class DatabaseApi {
         if (null == authUserId) {
             return ResponseJson.error("供应商用户id不能为空", null);
         }
-        JSONObject parseObject = JSONObject.parseObject(params);
-        String fileName = parseObject.getString("fileName");
-        String ossName = parseObject.getString("ossName");
-        String ossUrl = parseObject.getString("ossUrl");
-        String fileSize = parseObject.getString("fileSize");
-        Integer parentId = parseObject.getInteger("parentId");
-        String mime = parseObject.getString("mime");
-        String filePath=parseObject.getString("filePath");
-        return databaseService.uploadPackage(authUserId, fileName, ossName, ossUrl, fileSize, parentId,mime,filePath);
+        List<FileTreeVo> fileTreeVos = JSONArray.parseArray(params, FileTreeVo.class);
+        return databaseService.uploadPackage(authUserId,fileTreeVos);
     }
 }

+ 4 - 1
src/main/java/com/caimei/mapper/cmMapper/FileMapper.java

@@ -81,7 +81,10 @@ public interface FileMapper {
 
     FileTreeVo findFileByArticleId(Integer id);
 
-    List<Integer> findDictionaryById(Integer fileId);
+    List<Integer> findDictionaryById(Integer fileId,@Param("fileName") String fileName);
 
     List<Integer> getNewestInfoById();
+
+    List<Integer> findSecondDictionaryById(Integer fileId,@Param("fileName") String fileName);
+
 }

+ 4 - 0
src/main/java/com/caimei/model/vo/FileTreeVo.java

@@ -76,4 +76,8 @@ public class FileTreeVo implements Serializable {
      * 文章类型1自主2第三方
      */
     private Integer articleType;
+    /**
+     * 文件路径(上传文件夹使用)
+     */
+    private String filePath;
 }

+ 7 - 6
src/main/java/com/caimei/service/data/DatabaseService.java

@@ -12,17 +12,17 @@ import java.util.List;
  * @author zzj
  */
 public interface DatabaseService {
-    ResponseJson<List<FileTreeVo>> getFileById(Integer fileId,Integer authUserId);
+    ResponseJson<List<FileTreeVo>> getFileById(Integer fileId, Integer authUserId);
 
-    ResponseJson createPackage(Integer authUserId,Integer fileId, String packageName);
+    ResponseJson createPackage(Integer authUserId, Integer fileId, String packageName);
 
-    ResponseJson downLoadZip(Integer fileId, String packageName, Integer authUserId,HttpServletResponse response);
+    ResponseJson downLoadZip(Integer fileId, String packageName, Integer authUserId, HttpServletResponse response);
 
-    ResponseJson insertNewFile(String fileName, String filePath, String previewUrl, Integer parentId,String fileType);
+    ResponseJson insertNewFile(String fileName, String filePath, String previewUrl, Integer parentId, String fileType);
 
     ResponseJson<HashMap<String, String>> ossTokenGet();
 
-    ResponseJson getOssUploadResult(Integer authUserId,String fileName, String ossName, String ossUrl, String fileSize,Integer parentId,String mime);
+    ResponseJson getOssUploadResult(Integer authUserId, String fileName, String ossName, String ossUrl, String fileSize, Integer parentId, String mime);
 
     ResponseJson updatePackage(Integer fileId, String packageName, Integer packageType);
 
@@ -40,5 +40,6 @@ public interface DatabaseService {
 
     ResponseJson uploadDictionary(MultipartFile[] multipartFile);
 
-    ResponseJson uploadPackage(Integer authUserId,String fileName, String ossName, String ossUrl, String fileSize,Integer parentId,String mime,String filePath);
+    ResponseJson uploadPackage(Integer authUserId, List<FileTreeVo> fileTreeVos);
 }
+

+ 44 - 27
src/main/java/com/caimei/service/data/impl/DatabaseServiceImpl.java

@@ -425,38 +425,55 @@ public class DatabaseServiceImpl implements DatabaseService {
     }
 
     @Override
-    public ResponseJson uploadPackage(Integer authUserId,String fileName, String ossName, String ossUrl, String fileSize,Integer parentId,String mime,String filePath){
+    public ResponseJson uploadPackage(Integer authUserId,List<FileTreeVo> fileTreeVos){
         //如果文件夹没有文件则直接返回
-        if(StringUtils.isNotEmpty(filePath)){
-            if(!filePath.contains(".")){
-                ResponseJson.error("-1","文件夹为空");
-            }
-            String[] filePathArr = filePath.split("/");
-            for (int i=0 ;i<filePathArr.length;i++){
-                if(filePathArr[i].contains(".")){
-                    //新增文件
-                    getOssUploadResult(authUserId,fileName,ossName,ossUrl,fileSize,parentId,mime);
-                    System.out.println("文件---------"+filePathArr[i]);
-                }else{
-                    //创建文件夹
-                    //判断文件夹是否存在(不存在则新建,存在则跳过)
-                    List<Integer> dictionaryById = fileMapper.findDictionaryById(parentId);
-                    if(null!=dictionaryById&&dictionaryById.size()>0){
-                        continue;
-                    }else{
-                        //不存在创建文件夹
-                        fileMapper.creatPackage(authUserId, parentId, filePathArr[i]);
-                        //查询最新文件夹id作为下一次的父id
-                        List<Integer> newestInfoById = fileMapper.getNewestInfoById();
-                        if(null!=newestInfoById&&newestInfoById.size()>0){
-                            parentId = newestInfoById.get(0);
+        for (FileTreeVo fileTreeVo:fileTreeVos) {
+            if (StringUtils.isNotEmpty(fileTreeVo.getFilePath())) {
+                if (!fileTreeVo.getFilePath().contains(".")) {
+                    ResponseJson.error("-1", "文件夹为空");
+                }
+                String[] filePathArr = fileTreeVo.getFilePath().split("/");
+                for (int i = 0; i < filePathArr.length; i++) {
+                    if (filePathArr[i].contains(".")) {
+                        //新增文件
+                        getOssUploadResult(authUserId, filePathArr[i], fileTreeVo.getOssName(), fileTreeVo.getOssUrl(), fileTreeVo.getFileSize(), fileTreeVo.getParentId(), fileTreeVo.getMime());
+                        System.out.println("文件---------" + filePathArr[i]);
+                    } else {
+                        //判断是否根目录
+                        List<Integer> dictionaryById=new ArrayList<>();
+                        if(null!=fileTreeVo.getParentId()&&0==fileTreeVo.getParentId()){
+                            dictionaryById=fileMapper.findDictionaryById(null,filePathArr[i]);
+                        }else{
+                             dictionaryById = fileMapper.findDictionaryById(fileTreeVo.getParentId(),filePathArr[i]);
+                             if(null==dictionaryById||dictionaryById.size()<=0){
+                                 //判断次级目录是否存在
+                                 List<Integer> secondDictionaryById = fileMapper.findSecondDictionaryById(fileTreeVo.getParentId(), filePathArr[i]);
+                                 if(null!=secondDictionaryById&&secondDictionaryById.size()>0){
+                                     dictionaryById.add(secondDictionaryById.get(0));
+                                 }
+                             }
+                        }
+                        //创建文件夹
+                        //判断文件夹是否存在(不存在则新建,存在则跳过)
+                        if (null != dictionaryById && dictionaryById.size() > 0) {
+                            fileTreeVo.setParentId(dictionaryById.get(0));
+                            continue;
+                        } else {
+                            //不存在创建文件夹
+                            fileMapper.creatPackage(authUserId, fileTreeVo.getParentId(), filePathArr[i]);
+                            //查询最新文件夹id作为下一次的父id
+                            List<Integer> newestInfoById = fileMapper.getNewestInfoById();
+                            if (null != newestInfoById && newestInfoById.size() > 0) {
+                                fileTreeVo.setParentId(newestInfoById.get(0));
+//                                parentId = newestInfoById.get(0);
+                            }
                         }
+                        System.out.println("文件夹---------" + filePathArr[i]);
                     }
-                    System.out.println("文件夹---------"+filePathArr[i]);
                 }
+            } else {
+                ResponseJson.error("-1", "文件夹为空");
             }
-        }else{
-            ResponseJson.error("-1","文件夹为空");
         }
         return ResponseJson.success();
     }

+ 19 - 1
src/main/resources/mapper/FileMapper.xml

@@ -327,11 +327,29 @@
     <select id="findDictionaryById" resultType="java.lang.Integer">
         select id
         from cm_tree_file
-        where id = #{fileId}
+        where 1=1
+        <if test="null != fileId">
+            and  id = #{fileId}
+        </if>
+        <if test="null != fileName and fileName != ''">
+            and fileName=#{fileName}
+        </if>
     </select>
     <select id="getNewestInfoById" resultType="java.lang.Integer">
         select id
         from cm_tree_file
         order by id desc
     </select>
+
+    <select id="findSecondDictionaryById" resultType="java.lang.Integer">
+        select id
+        from cm_tree_file
+        where 1=1
+        <if test="null != fileId">
+            and  parentId = #{fileId}
+        </if>
+        <if test="null != fileName and fileName != ''">
+            and fileName=#{fileName}
+        </if>
+    </select>
 </mapper>