Browse Source

file服务

home 2 years ago
parent
commit
cfaa6ab136
21 changed files with 302 additions and 219 deletions
  1. 0 56
      hx-common/file-tool/src/main/java/com/fjhx/controller/FileInfoController.java
  2. 0 17
      hx-common/file-tool/src/main/java/com/fjhx/params/FileInfoEx.java
  3. 0 17
      hx-common/file-tool/src/main/java/com/fjhx/params/FileInfoVo.java
  4. 0 28
      hx-common/file-tool/src/main/java/com/fjhx/service/FileInfoService.java
  5. 0 55
      hx-common/file-tool/src/main/java/com/fjhx/service/impl/FileInfoServiceImpl.java
  6. 8 0
      hx-common/hx-tool/src/main/java/com/fjhx/base/BaseService.java
  7. 0 33
      hx-common/hx-tool/src/main/java/com/fjhx/constants/FileConstant.java
  8. 0 9
      hx-common/hx-tool/src/main/java/com/fjhx/fileUpload/FileBaseEntity.java
  9. 0 1
      hx-common/pom.xml
  10. 2 2
      hx-serve/file-serve/pom.xml
  11. 18 0
      hx-serve/file-serve/src/main/java/com/fjhx/FileServeApplication.java
  12. 34 0
      hx-serve/file-serve/src/main/java/com/fjhx/controller/FileInfoController.java
  13. 6 1
      hx-serve/file-serve/src/main/java/com/fjhx/entity/FileInfo.java
  14. 0 0
      hx-serve/file-serve/src/main/java/com/fjhx/mapper/FileInfoMapper.java
  15. 0 0
      hx-serve/file-serve/src/main/java/com/fjhx/mapper/FileInfoMapper.xml
  16. 40 0
      hx-serve/file-serve/src/main/java/com/fjhx/service/FileInfoService.java
  17. 151 0
      hx-serve/file-serve/src/main/java/com/fjhx/service/impl/FileInfoServiceImpl.java
  18. 13 0
      hx-serve/file-serve/src/main/resources/application-dev.yml
  19. 12 0
      hx-serve/file-serve/src/main/resources/application-prod.yml
  20. 11 0
      hx-serve/file-serve/src/main/resources/application-test.yml
  21. 7 0
      hx-serve/pom.xml

+ 0 - 56
hx-common/file-tool/src/main/java/com/fjhx/controller/FileInfoController.java

@@ -1,56 +0,0 @@
-package com.fjhx.controller;
-
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import com.fjhx.entity.FileInfo;
-import com.fjhx.params.FileInfoVo;
-import com.fjhx.service.FileInfoService;
-import org.springblade.core.tool.api.R;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-import java.util.Map;
-
-/**
- * <p>
- * 文件 前端控制器
- * </p>
- *
- * @author ${author}
- * @since 2022-07-07
- */
-@RestController
-@RequestMapping("/fileInfo")
-public class FileInfoController {
-
-    @Autowired
-    private FileInfoService fileInfoService;
-
-    @PostMapping("/page")
-    public R page(@RequestBody Map<String, String> condition) {
-        Page<FileInfo> result = fileInfoService.getPage(condition);
-        return R.success(result);
-    }
-
-    @PostMapping("/add")
-    public R add(@RequestBody FileInfoVo fileInfoVo) {
-        fileInfoService.add(fileInfoVo);
-        return R.success();
-    }
-
-    @PostMapping("/edit")
-    public R edit(@RequestBody FileInfoVo fileInfoVo) {
-        fileInfoService.edit(fileInfoVo);
-        return R.success();
-    }
-
-    @PostMapping("/delete")
-    public R delete(@RequestBody FileInfoVo fileInfoVo) {
-        fileInfoService.delete(fileInfoVo);
-        return R.success();
-    }
-
-}
-

+ 0 - 17
hx-common/file-tool/src/main/java/com/fjhx/params/FileInfoEx.java

@@ -1,17 +0,0 @@
-package com.fjhx.params;
-
-import com.fjhx.entity.FileInfo;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-
-/**
- * 文件
- *
- * @author ${author}
- * @since 2022-07-07
- */
-@Data
-@EqualsAndHashCode(callSuper = true)
-public class FileInfoEx extends FileInfo {
-
-}

+ 0 - 17
hx-common/file-tool/src/main/java/com/fjhx/params/FileInfoVo.java

@@ -1,17 +0,0 @@
-package com.fjhx.params;
-
-import com.fjhx.entity.FileInfo;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
-
-/**
- * 文件
- *
- * @author ${author}
- * @since 2022-07-07
- */
-@Data
-@EqualsAndHashCode(callSuper = true)
-public class FileInfoVo extends FileInfo {
-
-}

+ 0 - 28
hx-common/file-tool/src/main/java/com/fjhx/service/FileInfoService.java

@@ -1,28 +0,0 @@
-package com.fjhx.service;
-
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import com.fjhx.base.BaseService;
-import com.fjhx.entity.FileInfo;
-import com.fjhx.params.FileInfoVo;
-
-import java.util.Map;
-
-/**
- * <p>
- * 文件 服务类
- * </p>
- *
- * @author ${author}
- * @since 2022-07-07
- */
-public interface FileInfoService extends BaseService<FileInfo> {
-
-    Page<FileInfo> getPage(Map<String, String> condition);
-
-    void add(FileInfoVo fileInfoVo);
-
-    void edit(FileInfoVo fileInfoVo);
-
-    void delete(FileInfoVo fileInfoVo);
-
-}

+ 0 - 55
hx-common/file-tool/src/main/java/com/fjhx/service/impl/FileInfoServiceImpl.java

@@ -1,55 +0,0 @@
-package com.fjhx.service.impl;
-
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.toolkit.Wrappers;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.fjhx.entity.FileInfo;
-import com.fjhx.mapper.FileInfoMapper;
-import com.fjhx.params.FileInfoVo;
-import com.fjhx.service.FileInfoService;
-import com.fjhx.utils.WrapperUtil;
-import org.springframework.stereotype.Service;
-
-import java.util.Map;
-
-/**
- * <p>
- * 文件 服务实现类
- * </p>
- *
- * @author ${author}
- * @since 2022-07-07
- */
-@Service
-public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {
-
-    @Override
-    public Page<FileInfo> getPage(Map<String, String> condition) {
-
-        QueryWrapper<FileInfo> wrapper = Wrappers.query();
-
-        WrapperUtil.init(condition, wrapper)
-                .eqTenantId()
-                .createTimeDesc();
-
-        Page<FileInfo> page = page(condition, wrapper);
-        return page;
-    }
-
-    @Override
-    public void add(FileInfoVo fileInfoVo) {
-        save(fileInfoVo);
-    }
-
-    @Override
-    public void edit(FileInfoVo fileInfoVo) {
-        updateById(fileInfoVo);
-    }
-
-    @Override
-    public void delete(FileInfoVo fileInfoVo) {
-        removeById(fileInfoVo.getId());
-    }
-
-}

+ 8 - 0
hx-common/hx-tool/src/main/java/com/fjhx/base/BaseService.java

@@ -2,6 +2,7 @@ package com.fjhx.base;
 
 import cn.hutool.core.util.ObjectUtil;
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.IService;
@@ -77,6 +78,13 @@ public interface BaseService<T> extends IService<T> {
         return lambdaQuery().eq(column, val).one();
     }
 
+    /**
+     * 按指定规则删除
+     */
+    default boolean remove(SFunction<T, ?> column, Object val) {
+        return remove(Wrappers.<T>lambdaQuery().eq(column, val));
+    }
+
     @Override
     default boolean save(T entity) {
         setCreateInfo(entity);

+ 0 - 33
hx-common/hx-tool/src/main/java/com/fjhx/constants/FileConstant.java

@@ -1,33 +0,0 @@
-package com.fjhx.constants;
-
-/**
- * 文件常量
- */
-public interface FileConstant {
-
-    // 文件类型
-    interface FileType {
-        // 图片
-        Integer IMAGE = 1;
-        // 视频
-        Integer VIDEO = 2;
-        // 音频
-        Integer AUDIO = 3;
-        // word
-        Integer WORD = 4;
-        // excel
-        Integer EXCEL = 5;
-        // ppt
-        Integer PPT = 6;
-        // 文本
-        Integer TXT = 7;
-        // zip
-        Integer ZIP = 8;
-    }
-
-    // 文件后缀
-    interface FileSuffix {
-
-    }
-
-}

+ 0 - 9
hx-common/hx-tool/src/main/java/com/fjhx/fileUpload/FileBaseEntity.java

@@ -1,9 +0,0 @@
-package com.fjhx.fileUpload;
-
-import com.fjhx.base.BaseEntity;
-
-public class FileBaseEntity extends BaseEntity {
-
-
-
-}

+ 0 - 1
hx-common/pom.xml

@@ -15,7 +15,6 @@
     <modules>
         <module>code-generator</module>
         <module>hx-tool</module>
-        <module>file-tool</module>
     </modules>
 
     <properties>

+ 2 - 2
hx-common/file-tool/pom.xml → hx-serve/file-serve/pom.xml

@@ -3,13 +3,13 @@
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <parent>
-        <artifactId>hx-common</artifactId>
+        <artifactId>hx-serve</artifactId>
         <groupId>com.fjhx</groupId>
         <version>3.2.0</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
 
-    <artifactId>file-tool</artifactId>
+    <artifactId>iot-management</artifactId>
 
     <properties>
         <maven.compiler.source>8</maven.compiler.source>

+ 18 - 0
hx-serve/file-serve/src/main/java/com/fjhx/FileServeApplication.java

@@ -0,0 +1,18 @@
+package com.fjhx;
+
+import org.springblade.core.launch.BladeApplication;
+import org.springframework.cloud.client.SpringCloudApplication;
+
+/**
+ * 杰生模块启动器
+ */
+@SpringCloudApplication
+public class FileServeApplication {
+
+    private static final String APP_NAME = "file-serve";
+
+    public static void main(String[] args) {
+        BladeApplication.run(APP_NAME, FileServeApplication.class, args);
+    }
+
+}

+ 34 - 0
hx-serve/file-serve/src/main/java/com/fjhx/controller/FileInfoController.java

@@ -0,0 +1,34 @@
+package com.fjhx.controller;
+
+import com.fjhx.service.FileInfoService;
+import org.springblade.core.tool.api.R;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.Map;
+
+/**
+ * <p>
+ * 文件 前端控制器
+ * </p>
+ *
+ * @author ${author}
+ * @since 2022-07-07
+ */
+@RestController
+public class FileInfoController {
+
+    @Autowired
+    private FileInfoService fileInfoService;
+
+    @PostMapping("/uploadFile")
+    public R uploadFile(@RequestParam("file") MultipartFile file) {
+        Map<String, String> result = fileInfoService.uploadFile(file);
+        return R.success(result);
+    }
+
+}
+

+ 6 - 1
hx-common/file-tool/src/main/java/com/fjhx/entity/FileInfo.java → hx-serve/file-serve/src/main/java/com/fjhx/entity/FileInfo.java

@@ -33,7 +33,7 @@ public class FileInfo extends BaseEntity {
     /**
      * 文件类型
      */
-    private Integer fileType;
+    private String contentType;
 
     /**
      * 文件大小
@@ -41,6 +41,11 @@ public class FileInfo extends BaseEntity {
     private Long fileSize;
 
     /**
+     * 文件大小
+     */
+    private String filePath;
+
+    /**
      * 业务id
      */
     private Long businessId;

+ 0 - 0
hx-common/file-tool/src/main/java/com/fjhx/mapper/FileInfoMapper.java → hx-serve/file-serve/src/main/java/com/fjhx/mapper/FileInfoMapper.java


+ 0 - 0
hx-common/file-tool/src/main/java/com/fjhx/mapper/FileInfoMapper.xml → hx-serve/file-serve/src/main/java/com/fjhx/mapper/FileInfoMapper.xml


+ 40 - 0
hx-serve/file-serve/src/main/java/com/fjhx/service/FileInfoService.java

@@ -0,0 +1,40 @@
+package com.fjhx.service;
+
+import com.fjhx.base.BaseService;
+import com.fjhx.entity.FileInfo;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * <p>
+ * 文件 服务类
+ * </p>
+ *
+ * @author ${author}
+ * @since 2022-07-07
+ */
+public interface FileInfoService extends BaseService<FileInfo> {
+
+    /**
+     * 文件上传
+     */
+    Map<String, String> uploadFile(MultipartFile file);
+
+    /**
+     * 新增文件
+     */
+    void addFile(Long businessId, Integer businessType, List<FileInfo> fileInfoList);
+
+    /**
+     * 编辑文件
+     */
+    void updateFile(Long businessId, Integer businessType, List<FileInfo> fileInfoList);
+
+    /**
+     * 编辑文件
+     */
+    void deleteFile(Long businessId);
+
+}

+ 151 - 0
hx-serve/file-serve/src/main/java/com/fjhx/service/impl/FileInfoServiceImpl.java

@@ -0,0 +1,151 @@
+package com.fjhx.service.impl;
+
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.lang.UUID;
+import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fjhx.base.BaseEntity;
+import com.fjhx.entity.FileInfo;
+import com.fjhx.mapper.FileInfoMapper;
+import com.fjhx.service.FileInfoService;
+import org.springblade.core.launch.BladeApplication;
+import org.springblade.core.log.exception.ServiceException;
+import org.springblade.core.tool.utils.DateUtil;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.*;
+import java.util.stream.Collectors;
+
+/**
+ * <p>
+ * 文件 服务实现类
+ * </p>
+ *
+ * @author ${author}
+ * @since 2022-07-07
+ */
+@Service
+public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {
+
+    @Value("${spring.application.name}")
+    public String applicationName;
+
+    @Override
+    public Map<String, String> uploadFile(MultipartFile file) {
+
+        String fileName = file.getOriginalFilename();
+        String suffix = FileUtil.getSuffix(fileName);
+
+        String path = getFilePath();
+        String filePath = path + UUID.randomUUID() + "." + suffix;
+
+        FileUtil.mkdir(path);
+        FileOutputStream fileOutputStream = null;
+        InputStream inputStream = null;
+
+        try {
+            inputStream = file.getInputStream();
+            fileOutputStream = new FileOutputStream(filePath);
+            int index;
+            byte[] bytes = new byte[1024];
+
+            while ((index = inputStream.read(bytes)) != -1) {
+                fileOutputStream.write(bytes, 0, index);
+                fileOutputStream.flush();
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw new ServiceException("上传文件失败");
+        } finally {
+            try {
+                if (fileOutputStream != null) {
+                    fileOutputStream.close();
+                }
+                if (inputStream != null) {
+                    inputStream.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+
+        FileInfo fileInfo = new FileInfo();
+        fileInfo.setFileName(fileName);
+        fileInfo.setFileSuffix(suffix);
+        fileInfo.setContentType(file.getContentType());
+        fileInfo.setFileSize(file.getSize());
+        fileInfo.setFilePath(filePath);
+        save(fileInfo);
+
+        Map<String, String> map = new HashMap<>();
+
+        map.put("fileName", fileName);
+        map.put("filePath", filePath);
+        map.put("id", fileInfo.getId().toString());
+
+        return map;
+    }
+
+    @Override
+    public void addFile(Long businessId, Integer businessType, List<FileInfo> fileInfoList) {
+        if (fileInfoList.size() == 0) {
+            return;
+        }
+        fileInfoList.forEach(item -> {
+            item.setBusinessId(businessId);
+            item.setBusinessType(businessType);
+        });
+        updateBatchById(fileInfoList);
+    }
+
+    @Override
+    public void updateFile(Long businessId, Integer businessType, List<FileInfo> fileInfoList) {
+
+        List<Long> excludeIdList = new ArrayList<>();
+
+        List<FileInfo> collect = fileInfoList.stream().filter(fileInfo -> {
+            if (ObjectUtil.isNotEmpty(fileInfo.getBusinessId())) {
+                excludeIdList.add(fileInfo.getId());
+                return false;
+            }
+            fileInfo.setId(businessId);
+            fileInfo.setBusinessType(businessType);
+            return true;
+        }).collect(Collectors.toList());
+
+        remove(Wrappers.<FileInfo>lambdaQuery()
+                .eq(FileInfo::getBusinessId, businessId)
+                .eq(FileInfo::getBusinessType, businessType)
+                .notIn(excludeIdList.size() > 0, BaseEntity::getId, collect));
+
+        if (collect.size() > 0) {
+            saveBatch(collect);
+        }
+
+    }
+
+    @Override
+    public void deleteFile(Long businessId) {
+        remove(FileInfo::getBusinessId, businessId);
+    }
+
+    private String getFilePath() {
+        StringJoiner joiner = new StringJoiner("/");
+        boolean localDev = BladeApplication.isLocalDev();
+        if (localDev) {
+            joiner.add("D:/hx");
+        } else {
+            joiner.add("/mnt/file");
+        }
+        joiner.add(applicationName).add(DateUtil.format(new Date(), "yyyy-MM/dd/"));
+
+        return joiner.toString();
+    }
+
+}

+ 13 - 0
hx-serve/file-serve/src/main/resources/application-dev.yml

@@ -0,0 +1,13 @@
+# 服务器端口
+server:
+  port: 8300
+logging:
+  level:
+    org.springframework.data.mongodb.core: DEBUG
+# 数据源配置
+spring:
+  # 数据库
+  datasource:
+    url: ${blade.datasource.file_serve.dev.url}
+    username: ${blade.datasource.file_serve.dev.username}
+    password: ${blade.datasource.file_serve.dev.password}

+ 12 - 0
hx-serve/file-serve/src/main/resources/application-prod.yml

@@ -0,0 +1,12 @@
+# 服务器端口
+server:
+  port: 8300
+
+# 数据源配置
+spring:
+  # 数据库
+  datasource:
+    url: ${blade.datasource.file_serve.prod.url}
+    username: ${blade.datasource.file_serve.prod.username}
+    password: ${blade.datasource.file_serve.prod.password}
+

+ 11 - 0
hx-serve/file-serve/src/main/resources/application-test.yml

@@ -0,0 +1,11 @@
+# 服务器端口
+server:
+  port: 8300
+
+# 数据源配置
+spring:
+  # 数据库
+  datasource:
+    url: ${blade.datasource.file_serve.test.url}
+    username: ${blade.datasource.file_serve.test.username}
+    password: ${blade.datasource.file_serve.test.password}

+ 7 - 0
hx-serve/pom.xml

@@ -21,6 +21,7 @@
     </properties>
 
     <modules>
+        <module>file-serve</module>
         <module>iot-management</module>
         <module>storage</module>
         <module>product-library</module>
@@ -47,6 +48,12 @@
 
             <dependency>
                 <groupId>com.fjhx</groupId>
+                <artifactId>file-tool</artifactId>
+                <version>${hx.version}</version>
+            </dependency>
+
+            <dependency>
+                <groupId>com.fjhx</groupId>
                 <artifactId>iot-management-api</artifactId>
                 <version>${hx.version}</version>
             </dependency>