Prechádzať zdrojové kódy

解决文件复制时的相关问题.

wukai 1 rok pred
rodič
commit
c22ebec6c4

+ 5 - 0
doc-biz/src/main/java/com/doc/biz/controller/DocInfoController.java

@@ -181,9 +181,14 @@ public class DocInfoController extends BaseController {
     @ApiImplicitParams({@ApiImplicitParam(name = "docId", value = "文件ID", required = true), @ApiImplicitParam(name = "spaceId", value = "空间ID"), @ApiImplicitParam(name = "dirId", value = "新目录ID", required = true)})
     public AjaxResult copy(Long docId, Long spaceId, Long dirId) {
         DocInfo info = docInfoService.selectDocInfoByDocId(docId);
+
+        DocumentVO vo = mongoService.copy(info.getFileId());
+
         info.setDocId(null);
         info.setSpaceId(spaceId);
         info.setDirId(dirId);
+        info.setFileId(vo.getFileId());
+
         docInfoService.insertDocInfo(info);
 
         return success();

+ 15 - 7
doc-biz/src/main/java/com/doc/biz/service/IMongoService.java

@@ -16,8 +16,8 @@ public interface IMongoService {
     /**
      * 文件上传
      *
-     * @param file
-     * @return
+     * @param file 文件
+     * @return 结果
      * @throws Exception
      */
     DocumentVO uploadFile(MultipartFile file) throws Exception;
@@ -25,16 +25,16 @@ public interface IMongoService {
     /**
      * 多文件上传
      *
-     * @param files
-     * @return
+     * @param files 文件
+     * @return 结果
      */
     List<DocumentVO> uploadFiles(List<MultipartFile> files);
 
     /**
      * 文件下载
      *
-     * @param fileId
-     * @return
+     * @param fileId 文件ID
+     * @return 结果
      */
     DocumentVO downloadFile(String fileId);
 
@@ -50,7 +50,15 @@ public interface IMongoService {
     /**
      * 文件删除
      *
-     * @param fileId
+     * @param fileId 文件ID
      */
     void removeFile(String fileId);
+
+    /**
+     * 查询并复制mongo文件
+     *
+     * @param fileId 文件id
+     * @return 结果
+     */
+    DocumentVO copy(String fileId);
 }

+ 19 - 9
doc-biz/src/main/java/com/doc/biz/service/impl/MongoServiceImpl.java

@@ -112,7 +112,7 @@ public class MongoServiceImpl implements IMongoService {
      * @return 下载流
      */
     @Override
-    public ResponseEntity<Object> download(String fileId,boolean isDown) {
+    public ResponseEntity<Object> download(String fileId, boolean isDown) {
         DocumentVO vo = downloadFile(fileId);
         if (Objects.nonNull(vo)) {
             String disposition = isDown ? "attachment" : "inline";
@@ -181,9 +181,8 @@ public class MongoServiceImpl implements IMongoService {
      */
     public DocumentVO saveBinaryFile(MultipartFile file) throws Exception {
         String suffix = getFileSuffix(file);
-        DocumentFile documentFile = null;
         try (InputStream in = file.getInputStream()) {
-            documentFile = mongoFileRepository.save(
+            DocumentFile documentFile = mongoFileRepository.save(
                     DocumentFile.builder()
                             .fileName(file.getOriginalFilename())
                             .fileSize(file.getSize())
@@ -194,13 +193,13 @@ public class MongoServiceImpl implements IMongoService {
                             .md5(Md5Utils.getMd5ByInputStream(in))
                             .build()
             );
+            return new DocumentVO(documentFile);
         } catch (Exception e) {
             log.error(e.getMessage());
             throw e;
             // 抛出异常,让调用方处理
         }
 
-        return new DocumentVO(documentFile);
     }
 
     /**
@@ -212,10 +211,9 @@ public class MongoServiceImpl implements IMongoService {
      */
     public DocumentVO saveGridFsFile(MultipartFile file) throws Exception {
         String suffix = getFileSuffix(file);
-        DocumentFile documentFile = null;
         try (InputStream in = file.getInputStream()) {
             String gridFsId = this.storeFileToGridFS(in, file.getOriginalFilename(), file.getContentType());
-            documentFile = mongoFileRepository.save(
+            DocumentFile documentFile = mongoFileRepository.save(
                     DocumentFile.builder()
                             .fileName(file.getOriginalFilename())
                             .fileSize(file.getSize())
@@ -226,14 +224,12 @@ public class MongoServiceImpl implements IMongoService {
                             .gridFsId(gridFsId)
                             .build()
             );
+            return new DocumentVO(documentFile);
         } catch (Exception e) {
             log.error(e.getMessage());
             throw e;
             // 抛出异常,让调用方处理
         }
-
-
-        return new DocumentVO(documentFile);
     }
 
     /**
@@ -260,6 +256,20 @@ public class MongoServiceImpl implements IMongoService {
     }
 
     /**
+     * 查询并复制mongo文件
+     *
+     * @param id 文件id
+     * @return 对象
+     */
+    public DocumentVO copy(String id) {
+        Optional<DocumentFile> optional = mongoFileRepository.findById(id);
+        DocumentFile file = optional.get();
+        file.setId(null);
+        mongoFileRepository.save(file);
+        return new DocumentVO(file);
+    }
+
+    /**
      * 获取Grid文件
      *
      * @param id