reghao 3 месяцев назад
Родитель
Сommit
614d84cb81

+ 9 - 31
file/file-service/src/main/java/cn/reghao/tnb/file/app/controller/FileController.java

@@ -1,11 +1,11 @@
 package cn.reghao.tnb.file.app.controller;
 
 import cn.reghao.jutil.jdk.web.db.PageList;
+import cn.reghao.tnb.common.auth.AuthUser;
 import cn.reghao.tnb.common.web.ServletUtil;
 import cn.reghao.tnb.common.web.WebResult;
 import cn.reghao.tnb.file.app.model.dto.UploadFile;
 import cn.reghao.tnb.file.app.model.po.LocalFile;
-import cn.reghao.tnb.file.app.model.vo.LocalFileInfo;
 import cn.reghao.tnb.file.app.model.vo.LocalFileUrl;
 import cn.reghao.tnb.file.app.service.FileService;
 import cn.reghao.tnb.file.app.util.JarFileResources;
@@ -14,7 +14,6 @@ import io.swagger.v3.oas.annotations.tags.Tag;
 import jakarta.servlet.http.HttpServletRequest;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
-import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -29,7 +28,7 @@ import java.util.Map;
  * @date 2025-09-26 20:40:27
  */
 @Tag(name = "file 管理接口")
-@Controller
+@RestController
 public class FileController {
     private final JarFileResources jarFileResources;
     private final FileService fileService;
@@ -39,23 +38,9 @@ public class FileController {
         this.fileService = fileService;
     }
 
-    @Operation(summary = "文件列表页面", description = "N")
-    @GetMapping("/api/file/list")
-    public String fileList(@RequestParam("pn") int pageNumber) {
-        PageList<LocalFileInfo> pageList = fileService.getLocalFiles(pageNumber);
-        return WebResult.success(pageList);
-    }
-
-    @Operation(summary = "图片列表页面", description = "N")
-    @GetMapping("/api/file/image")
-    public String imageList(@RequestParam("pn") int pageNumber) {
-        PageList<LocalFileUrl> pageList = fileService.getImageFiles(pageNumber);
-        return WebResult.success(pageList);
-    }
-
+    @AuthUser
     @Operation(summary = "文件上传接口", description = "N")
     @PostMapping(value = "/api/file/upload", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ResponseBody
     public String uploadFile(UploadFile uploadFile) throws Exception {
         MultipartFile file = uploadFile.getFile();
         LocalFile localFile = fileService.putLocalFile(file);
@@ -66,23 +51,16 @@ public class FileController {
         return WebResult.success(map);
     }
 
-    @Operation(summary = "文件预览接口", description = "N")
-    @GetMapping(value = "/api/file/preview", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ResponseBody
-    public String previewFile(@RequestParam("objectId") String objectId) {
-        LocalFile localFile = fileService.getLocalFile(objectId);
-        String objectName = localFile.getObjectName();
-
-        Map<String, Object> map = new HashMap<>();
-        map.put("filename", localFile.getFilename());
-        map.put("fileType", localFile.getFileType());
-        map.put("url", "/"+objectName);
-        return WebResult.success(map);
+    @AuthUser
+    @Operation(summary = "图片列表页面", description = "N")
+    @GetMapping("/api/file/image")
+    public String imageList(@RequestParam("pn") int pageNumber) {
+        PageList<LocalFileUrl> pageList = fileService.getImageFiles(pageNumber);
+        return WebResult.success(pageList);
     }
 
     @Operation(summary = "文件访问接口", description = "N")
     @GetMapping("/file/**")
-    @ResponseBody
     public void getFile() throws IOException {
         HttpServletRequest servletRequest = ServletUtil.getRequest();
         String uri = servletRequest.getRequestURI();

+ 0 - 6
file/file-service/src/main/java/cn/reghao/tnb/file/app/db/mapper/LocalFileMapper.java

@@ -15,12 +15,6 @@ import java.util.List;
  */
 @Mapper
 public interface LocalFileMapper extends BaseMapper<LocalFile> {
-    LocalFile findByObjectName(String objectName);
-    LocalFile findByObjectId(String objectId);
-    List<LocalFile> findBySha256sum(String sha256sum);
-    List<LocalFile> findByOwner(long owner);
-    List<LocalFile> findByFileTypeAndOwner(@Param("fileType") int fileType, @Param("owner") long owner);
-
     int countByFileQuery(FileQuery fileQuery);
     List<LocalFile> findFileQueryByPage(@Param("page") Page page, @Param("fileQuery") FileQuery fileQuery);
 }

+ 38 - 10
file/file-service/src/main/java/cn/reghao/tnb/file/app/service/FileService.java

@@ -3,12 +3,14 @@ package cn.reghao.tnb.file.app.service;
 import cn.reghao.jutil.jdk.converter.ByteConverter;
 import cn.reghao.jutil.jdk.converter.ByteType;
 import cn.reghao.jutil.jdk.security.DigestUtil;
+import cn.reghao.jutil.jdk.web.db.Page;
 import cn.reghao.jutil.jdk.web.db.PageList;
 import cn.reghao.tnb.common.auth.UserContext;
 import cn.reghao.tnb.common.web.ServletUtil;
 import cn.reghao.tnb.file.app.config.AppProperties;
 import cn.reghao.tnb.file.app.db.mapper.LocalFileMapper;
 import cn.reghao.tnb.file.app.model.po.LocalFile;
+import cn.reghao.tnb.file.app.model.vo.FileQuery;
 import cn.reghao.tnb.file.app.model.vo.LocalFileInfo;
 import cn.reghao.tnb.file.app.model.vo.LocalFileUrl;
 import jakarta.servlet.http.HttpServletResponse;
@@ -37,8 +39,8 @@ public class FileService {
     private final int bufSize = 1024*1024*8;
     private final ByteConverter byteConverter;
     private final LocalFileMapper localFileMapper;
-    private int pageSize = 10;
-    private String baseDir;
+    private final int pageSize = 10;
+    private final String baseDir;
 
     public FileService(ByteConverter byteConverter, LocalFileMapper localFileMapper, AppProperties appProperties) {
         this.byteConverter = byteConverter;
@@ -75,8 +77,12 @@ public class FileService {
 
     @Cacheable(cacheNames = "filePaths", key = "#objectName", unless = "#result == null")
     public LocalFile getByObjectName(String objectName) {
-        LocalFile diskFile = localFileMapper.findByObjectName(objectName);
-        return diskFile;
+        FileQuery fileQuery = new FileQuery.Builder()
+                .objectName(objectName)
+                .build();
+        Page page = new Page(1, pageSize);
+        List<LocalFile> localFiles = localFileMapper.findFileQueryByPage(page, fileQuery);
+        return localFiles.isEmpty() ? null : localFiles.get(0);
     }
 
     private void writeResponse(int statusCode) throws IOException {
@@ -140,8 +146,12 @@ public class FileService {
         String contentType = "image/jpeg";
         int fileType = 1001;
         String sha256sum = DigestUtil.sha256sum(savedFile.getAbsolutePath());
+        FileQuery fileQuery = new FileQuery.Builder()
+                .sha256sum(sha256sum)
+                .build();
 
-        List<LocalFile> diskFiles = localFileMapper.findBySha256sum(sha256sum);
+        Page page = new Page(1, pageSize);
+        List<LocalFile> diskFiles = localFileMapper.findFileQueryByPage(page, fileQuery);
         LocalFile diskFile;
         if (!diskFiles.isEmpty()) {
             LocalFile existFile = diskFiles.get(0);
@@ -176,7 +186,14 @@ public class FileService {
 
     public PageList<LocalFileInfo> getLocalFiles(int pageNumber) {
         long owner = UserContext.getUserId();
-        List<LocalFile> list = localFileMapper.findByOwner(owner);
+        FileQuery fileQuery = new FileQuery.Builder()
+                .owner(owner)
+                .build();
+
+        Page page = new Page(pageNumber, pageSize);
+        int total = localFileMapper.countByFileQuery(fileQuery);
+        List<LocalFile> list = localFileMapper.findFileQueryByPage(page, fileQuery);
+
         List<LocalFileInfo> list0 = list.stream().map(diskFile -> {
             String size = byteConverter.convert(ByteType.Bytes, diskFile.getSize());
             return new LocalFileInfo(diskFile, size);
@@ -187,7 +204,13 @@ public class FileService {
     public PageList<LocalFileUrl> getImageFiles(int pageNumber) {
         long owner = UserContext.getUserId();
         int fileType = 1001;
-        List<LocalFile> list = localFileMapper.findByFileTypeAndOwner(fileType, owner);
+        FileQuery fileQuery = new FileQuery.Builder()
+                .fileType(fileType)
+                .owner(owner)
+                .build();
+        Page page = new Page(pageNumber, pageSize);
+        int total = localFileMapper.countByFileQuery(fileQuery);
+        List<LocalFile> list = localFileMapper.findFileQueryByPage(page, fileQuery);
         List<LocalFileUrl> list0 = list.stream().map(diskFile -> {
             String fileId = diskFile.getObjectId();
             String filename = diskFile.getFilename();
@@ -195,12 +218,17 @@ public class FileService {
             String url = String.format("/%s", objectName);
             return new LocalFileUrl(fileId, filename, url);
         }).collect(Collectors.toList());
-
-        return PageList.empty();
+        return PageList.pageList(pageNumber, pageSize, total, list0);
     }
 
     public LocalFile getLocalFile(String objectId) {
-        return localFileMapper.findByObjectId(objectId);
+        FileQuery fileQuery = new FileQuery.Builder()
+                .objectId(objectId)
+                .build();
+
+        Page page = new Page(1, 1);
+        List<LocalFile> list = localFileMapper.findFileQueryByPage(page, fileQuery);
+        return list.isEmpty() ? null : list.get(0);
     }
 
     public String getObjectName(String path) {

+ 6 - 6
file/file-service/src/main/resources/mapper/file/LocalFileMapper.xml

@@ -3,13 +3,13 @@
 
 <mapper namespace="cn.reghao.tnb.file.app.db.mapper.LocalFileMapper">
     <insert id="save" useGeneratedKeys="true" keyProperty="id">
-        insert into local_file
+        insert into file_local_file
         (`object_name`,`object_id`,`absolute_path`,`filename`,`file_type`,`content_type`,`sha256sum`,`size`,`owner`)
         values
         (#{objectName},#{objectId},#{absolutePath},#{filename},#{fileType},#{contentType},#{sha256sum},#{size},#{owner})
     </insert>
     <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
-        insert into local_file
+        insert into file_local_file
         (`object_name`,`object_id`,`absolute_path`,`filename`,`file_type`,`content_type`,`sha256sum`,`size`,`owner`)
         values
         <foreach collection="list" item="item" index="index" separator=",">
@@ -17,9 +17,9 @@
         </foreach>
     </insert>
 
-    <select id="countByDiskQuery" resultType="java.lang.Integer">
+    <select id="countByFileQuery" resultType="java.lang.Integer">
         select count(*)
-        from local_file
+        from file_local_file
         <where>
             deleted=0
             <if test="fileType != null">
@@ -39,9 +39,9 @@
             </if>
         </where>
     </select>
-    <select id="findDiskQueryByPage" resultType="cn.reghao.tnb.file.app.zdisk.model.po.DiskFile">
+    <select id="findFileQueryByPage" resultType="cn.reghao.tnb.file.app.model.po.LocalFile">
         select *
-        from local_file
+        from file_local_file
         <where>
             deleted=0
             <if test="fileQuery.fileType != null">