Ver código fonte

文件预览还是放开,主要是给onlyoffice用,其他地方改掉

wukai 1 ano atrás
pai
commit
b41f5404e1

+ 1 - 1
doc-admin/src/main/resources/application-t1.yml

@@ -22,7 +22,7 @@ spring:
   # redis 配置
   redis:
     # 地址
-    host: 192.168.188.88
+    host: 192.168.1.28
     # 端口,默认为6379
     port: 6379
     # 数据库索引

+ 17 - 61
doc-biz/src/main/java/com/doc/biz/controller/ApiController.java

@@ -1,29 +1,25 @@
 package com.doc.biz.controller;
 
 import cn.hutool.http.HttpUtil;
+import com.doc.biz.service.IDocRecentService;
 import com.doc.biz.service.IMongoService;
 import com.doc.biz.vo.DocumentVO;
 import com.doc.common.core.controller.BaseController;
 import com.doc.common.core.domain.AjaxResult;
+import com.doc.common.utils.SecurityUtils;
 import com.doc.system.service.ISysConfigService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
-import org.yaml.snakeyaml.util.UriEncoder;
 
 import javax.annotation.Resource;
-import javax.servlet.http.HttpServletResponse;
 import java.io.File;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.nio.file.Files;
 import java.util.HashMap;
-import java.util.Objects;
 
 /**
  * 文件上传
@@ -44,62 +40,22 @@ public class ApiController extends BaseController {
     private IMongoService mongoService;
     @Resource
     private ISysConfigService configService;
+    @Resource
+    private IDocRecentService recentService;
+
 
-//    /**
-//     * 文件下载
-//     *
-//     * @param fileId fileId
-//     * @return
-//     */
-//    @ApiOperation("文件预览")
-//    @GetMapping("/access/{fileId}")
-//    public ResponseEntity<Object> access(@PathVariable(name = "fileId") String fileId) {
-//        return down(fileId, false);
-//    }
-//
-//    /**
-//     * 文件下载
-//     *
-//     * @param fileId fileId
-//     * @return
-//     */
-//    @ApiOperation("文件下载")
-//    @GetMapping("/download/{fileId}")
-//    public ResponseEntity<Object> download(@PathVariable(name = "fileId") String fileId) {
-//        return down(fileId, true);
-//    }
-//
-//    private ResponseEntity<Object> down(String fileId, boolean down) {
-//        DocumentVO vo = mongoService.downloadFile(fileId);
-//        if (Objects.nonNull(vo)) {
-//            String disposition = down ? "attachment" : "inline";
-//            disposition += "; filename=\"" + UriEncoder.encode(vo.getFileName()) + "\"";
-//            return ResponseEntity.ok()
-//                    .header(HttpHeaders.CONTENT_DISPOSITION, disposition)
-//                    .header(HttpHeaders.CONTENT_TYPE, vo.getContentType())
-//                    .header(HttpHeaders.CONTENT_LENGTH, vo.getFileSize() + "")
-//                    .header("Connection", "close")
-//                    .body(vo.getData());
-//        } else {
-//            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file does not exist");
-//        }
-//    }
-//
-//    @ApiOperation("文件下载")
-//    @GetMapping("/down/{fileId}")
-//    public void download(@PathVariable(name = "fileId") String fileId, HttpServletResponse response) {
-//        DocumentVO vo = mongoService.downloadFile(fileId);
-//        response.setContentType(vo.getContentType());
-//        response.setHeader("Content-Disposition", "attachment; filename=\"" + UriEncoder.encode(vo.getFileName()) + "\"");
-//        response.addHeader("Content-Length", "" + vo.getFileSize());
-//
-//        try (OutputStream stream = response.getOutputStream()) {
-//            stream.write(vo.getData());
-//            stream.flush();
-//        } catch (IOException e) {
-//            throw new RuntimeException(e);
-//        }
-//    }
+    /**
+     * 文件下载
+     *
+     * @param fileId fileId
+     * @return
+     */
+    @ApiOperation("文件预览")
+    @GetMapping("/access/{fileId}")
+    public ResponseEntity<Object> access(@PathVariable(name = "fileId") String fileId, @RequestParam Long uid) {
+        recentService.insertDocRecent(uid, fileId);
+        return mongoService.download(fileId, false);
+    }
 
     /**
      * 文字识别

+ 9 - 35
doc-biz/src/main/java/com/doc/biz/controller/DocInfoController.java

@@ -2,7 +2,6 @@ package com.doc.biz.controller;
 
 import com.doc.biz.domain.DocDir;
 import com.doc.biz.domain.DocInfo;
-import com.doc.biz.domain.DocRecent;
 import com.doc.biz.domain.DocSpace;
 import com.doc.biz.service.*;
 import com.doc.biz.vo.DocInfoVO;
@@ -20,17 +19,17 @@ import com.doc.common.utils.poi.ExcelUtil;
 import io.swagger.annotations.*;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.data.elasticsearch.NoSuchIndexException;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
-import org.yaml.snakeyaml.util.UriEncoder;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
 import java.io.InputStream;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * 文件基本信息表Controller
@@ -357,17 +356,10 @@ public class DocInfoController extends BaseController {
     @ApiOperation("文件预览")
     @GetMapping("/access/{fileId}")
     public ResponseEntity<Object> access(@PathVariable(name = "fileId") String fileId) {
-        DocInfo docInfo = docInfoService.selectDocInfoByFileId(fileId);
-        /**
-         * 插入最近访问记录
-         */
-        DocRecent recent = new DocRecent();
-        recent.setIsFolder("N");
-        recent.setOwner(SecurityUtils.getUserId());
-        recent.setRelaId(docInfo.getDocId());
-        recentService.insertDocRecent(recent);
-        
-        return down(fileId, false);
+
+        recentService.insertDocRecent(SecurityUtils.getUserId(), fileId);
+
+        return mongoService.download(fileId, false);
     }
 
     /**
@@ -376,24 +368,6 @@ public class DocInfoController extends BaseController {
     @ApiOperation("文件下载")
     @GetMapping("/download/{fileId}")
     public ResponseEntity<Object> download(@PathVariable(name = "fileId") String fileId) {
-        return down(fileId, true);
+        return mongoService.download(fileId, true);
     }
-
-    private ResponseEntity<Object> down(String fileId, boolean down) {
-        DocumentVO vo = mongoService.downloadFile(fileId);
-        if (Objects.nonNull(vo)) {
-            String disposition = down ? "attachment" : "inline";
-            disposition += "; filename=\"" + UriEncoder.encode(vo.getFileName()) + "\"";
-            return ResponseEntity.ok()
-                    .header(HttpHeaders.CONTENT_DISPOSITION, disposition)
-                    .header(HttpHeaders.CONTENT_TYPE, vo.getContentType())
-                    .header(HttpHeaders.CONTENT_LENGTH, vo.getFileSize() + "")
-                    .header("Connection", "close")
-                    .body(vo.getData());
-        } else {
-            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file does not exist");
-        }
-    }
-
-
 }

+ 12 - 0
doc-biz/src/main/java/com/doc/biz/service/IDocRecentService.java

@@ -34,6 +34,18 @@ public interface IDocRecentService {
      * @return 结果
      */
     public int insertDocRecent(DocRecent docRecent);
+    /**
+     * 新增最近文件
+     *
+     * @param docRecent 最近文件
+     * @return 结果
+     */
+    /**
+     * @param uid    用户ID
+     * @param fileId 文件ID
+     * @return
+     */
+    public int insertDocRecent(Long uid, String fileId);
 
     /**
      * 修改最近文件

+ 10 - 0
doc-biz/src/main/java/com/doc/biz/service/IMongoService.java

@@ -1,6 +1,7 @@
 package com.doc.biz.service;
 
 import com.doc.biz.vo.DocumentVO;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
@@ -38,6 +39,15 @@ public interface IMongoService {
     DocumentVO downloadFile(String fileId);
 
     /**
+     * 前端文件下载
+     *
+     * @param fileId 文件ID
+     * @param isDown 是否下载
+     * @return 下载流
+     */
+    ResponseEntity<Object> download(String fileId, boolean isDown);
+
+    /**
      * 文件删除
      *
      * @param fileId

+ 24 - 0
doc-biz/src/main/java/com/doc/biz/service/impl/DocRecentServiceImpl.java

@@ -1,7 +1,9 @@
 package com.doc.biz.service.impl;
 
+import com.doc.biz.domain.DocInfo;
 import com.doc.biz.domain.DocRecent;
 import com.doc.biz.mapper.DocRecentMapper;
+import com.doc.biz.service.IDocInfoService;
 import com.doc.biz.service.IDocRecentService;
 import com.doc.common.utils.DateUtils;
 import org.springframework.stereotype.Service;
@@ -19,6 +21,8 @@ import java.util.List;
 public class DocRecentServiceImpl implements IDocRecentService {
     @Resource
     private DocRecentMapper docRecentMapper;
+    @Resource
+    private IDocInfoService infoService;
 
     /**
      * 查询最近文件
@@ -55,6 +59,26 @@ public class DocRecentServiceImpl implements IDocRecentService {
     }
 
     /**
+     * @param uid    用户ID
+     * @param fileId 文件ID
+     * @return
+     */
+    @Override
+    public int insertDocRecent(Long uid, String fileId) {
+        DocInfo info = infoService.selectDocInfoByFileId(fileId);
+        if (info != null) {
+            //插入最近访问记录
+            //如果info=null,则代表是查看历史版本文件,不需要记录
+            DocRecent recent = new DocRecent();
+            recent.setIsFolder("N");
+            recent.setOwner(uid);
+            recent.setRelaId(info.getDocId());
+            return insertDocRecent(recent);
+        }
+        return 0;
+    }
+
+    /**
      * 修改最近文件
      *
      * @param docRecent 最近文件

+ 28 - 0
doc-biz/src/main/java/com/doc/biz/service/impl/MongoServiceImpl.java

@@ -16,8 +16,12 @@ import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.data.mongodb.core.query.Criteria;
 import org.springframework.data.mongodb.core.query.Query;
 import org.springframework.data.mongodb.gridfs.GridFsTemplate;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
+import org.yaml.snakeyaml.util.UriEncoder;
 
 import javax.annotation.Resource;
 import java.io.InputStream;
@@ -101,6 +105,30 @@ public class MongoServiceImpl implements IMongoService {
     }
 
     /**
+     * 前端文件下载
+     *
+     * @param fileId 文件ID
+     * @param isDown 是否下载
+     * @return 下载流
+     */
+    @Override
+    public ResponseEntity<Object> download(String fileId,boolean isDown) {
+        DocumentVO vo = downloadFile(fileId);
+        if (Objects.nonNull(vo)) {
+            String disposition = isDown ? "attachment" : "inline";
+            disposition += "; filename=\"" + UriEncoder.encode(vo.getFileName()) + "\"";
+            return ResponseEntity.ok()
+                    .header(HttpHeaders.CONTENT_DISPOSITION, disposition)
+                    .header(HttpHeaders.CONTENT_TYPE, vo.getContentType())
+                    .header(HttpHeaders.CONTENT_LENGTH, vo.getFileSize() + "")
+                    .header("Connection", "close")
+                    .body(vo.getData());
+        } else {
+            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file does not exist");
+        }
+    }
+
+    /**
      * 文件删除
      *
      * @param fileId