Sfoglia il codice sorgente

网盘代码提交

pans 8 mesi fa
parent
commit
b82e3e0d15

+ 15 - 12
snowy-plugin/snowy-plugin-disk/snowy-plugin-disk-func/src/main/java/vip/xiaonuo/disk/controller/FileController.java

@@ -74,6 +74,9 @@ public class FileController {
     @Value("${ufop.storage-type}")
     private Integer storageType;
 
+    @Value("${common.account}")
+    private String commonUserId;
+
     public static Executor executor = Executors.newFixedThreadPool(20);
 
     public static final String CURRENT_MODULE = "文件接口";
@@ -160,7 +163,7 @@ public class FileController {
 
         try {
 
-            String userId = "common";
+            String userId = commonUserId;
             String filePath = createFileDTO.getFilePath();
             String fileName = createFileDTO.getFileName();
             String extendName = createFileDTO.getExtendName();
@@ -259,7 +262,7 @@ public class FileController {
     public RestResult<String> createCommonFold(@Valid @RequestBody CreateFoldDTO createFoldDto) {
 
 //        String userId = SessionUtil.getSession().getUserId();
-        String userId = "common";
+        String userId = commonUserId;
         String filePath = createFoldDto.getFilePath();
 
 
@@ -357,7 +360,7 @@ public class FileController {
     @MyLog(operation = "文件搜索", module = CURRENT_MODULE)
     @ResponseBody
     public RestResult<SearchFileVO> searchCommon(SearchFileDTO searchFileDTO) {
-        String userId = "common";
+        String userId = commonUserId;
 
         int currentPage = (int)searchFileDTO.getCurrentPage() - 1;
         int pageCount = (int)(searchFileDTO.getPageCount() == 0 ? 10 : searchFileDTO.getPageCount());
@@ -466,7 +469,7 @@ public class FileController {
     @ResponseBody
     public RestResult<String> renameCommonfile(@RequestBody RenameFileDTO renameFileDto) {
 
-        String userId ="common";
+        String userId =commonUserId;
         UserFile userFile = userFileService.getById(renameFileDto.getUserFileId());
 
         List<UserFile> userFiles = userFileService.selectUserFileByNameAndPath(renameFileDto.getFileName(), userFile.getFilePath(), userId);
@@ -527,7 +530,7 @@ public class FileController {
             @Parameter(description = "当前页", required = true) long currentPage,
             @Parameter(description = "页面数量", required = true) long pageCount
     ){
-        String userId = "common";
+        String userId = commonUserId;
         if ("0".equals(fileType)) {
             IPage<FileListVO> fileList = userFileService.userFileList(userId, filePath, currentPage, pageCount);
             return RestResult.success().dataList(fileList.getRecords(), fileList.getTotal());
@@ -566,7 +569,7 @@ public class FileController {
     @MyLog(operation = "批量删除文件", module = CURRENT_MODULE)
     @ResponseBody
     public RestResult<String> batchdeleteCommonfile(@RequestBody BatchDeleteFileDTO batchDeleteFileDto) {
-        String userFileIds = "common";
+        String userFileIds = commonUserId;
         String[] userFileIdList = userFileIds.split(",");
         userFileService.update(new UpdateWrapper<UserFile>().lambda().set(UserFile::getDeleteFlag, 1).in(UserFile::getUserFileId, Arrays.asList(userFileIdList)));
         for (String userFileId : userFileIdList) {
@@ -607,7 +610,7 @@ public class FileController {
     @MyLog(operation = "删除文件", module = CURRENT_MODULE)
     @ResponseBody
     public RestResult deleteCommonFile(@RequestBody DeleteFileDTO deleteFileDto) {
-        String userId = "common";
+        String userId =commonUserId;
         userFileService.deleteUserFile(deleteFileDto.getUserFileId(), userId);
         fileDealComp.deleteESByUserFileId(deleteFileDto.getUserFileId());
 
@@ -686,7 +689,7 @@ public class FileController {
     @MyLog(operation = "文件复制", module = CURRENT_MODULE)
     @ResponseBody
     public RestResult<String> copyCommonfile(@RequestBody CopyFileDTO copyFileDTO) {
-        String userId = "common";
+        String userId = commonUserId;
         String filePath = copyFileDTO.getFilePath();
         String userFileIds = copyFileDTO.getUserFileIds();
         String[] userFileIdArr = userFileIds.split(",");
@@ -750,7 +753,7 @@ public class FileController {
     public RestResult<String> moveCommonFile(@RequestBody MoveFileDTO moveFileDto) {
 
 //        JwtUser sessionUserBean =  SessionUtil.getSession();
-        String userId ="common";
+        String userId =commonUserId;
         UserFile userFile = userFileService.getById(moveFileDto.getUserFileId());
         String oldfilePath = userFile.getFilePath();
         String newfilePath = moveFileDto.getFilePath();
@@ -813,7 +816,7 @@ public class FileController {
     @ResponseBody
     public RestResult<String> batchmoveCommonFile(@RequestBody BatchMoveFileDTO batchMoveFileDto) {
 
-        String userId = "common";
+        String userId =commonUserId;
 
         String newfilePath = batchMoveFileDto.getFilePath();
 
@@ -894,7 +897,7 @@ public class FileController {
     public RestResult<TreeNode> getCommonFileTree() {
         RestResult<TreeNode> result = new RestResult<TreeNode>();
 
-        String userId = "common";
+        String userId = commonUserId;
         List<UserFile> userFileList = userFileService.selectFilePathTreeByUserId(userId);
         TreeNode resultTreeNode = new TreeNode();
         resultTreeNode.setLabel(QiwenFile.separator);
@@ -981,7 +984,7 @@ public class FileController {
     @ResponseBody
     public RestResult<String> updateCommon(@RequestBody UpdateFileDTO updateFileDTO) {
 //        JwtUser sessionUserBean =  SessionUtil.getSession();
-        String userId = "common";
+        String userId = commonUserId;
         UserFile userFile = userFileService.getById(updateFileDTO.getUserFileId());
         FileBean fileBean = fileService.getById(userFile.getFileId());
         Long pointCount = fileService.getFilePointCount(userFile.getFileId());

+ 246 - 3
snowy-plugin/snowy-plugin-disk/snowy-plugin-disk-func/src/main/java/vip/xiaonuo/disk/controller/FiletransferController.java

@@ -13,6 +13,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
 import vip.xiaonuo.disk.component.FileDealComp;
@@ -62,6 +63,9 @@ public class FiletransferController {
     @Resource
     UFOPFactory ufopFactory;
 
+    @Value("${common.account}")
+    private String commonUserId;
+
 
     public static final String CURRENT_MODULE = "文件传输接口";
 
@@ -83,11 +87,11 @@ public class FiletransferController {
 
 
     @Operation(summary = "极速上传", description = "校验文件MD5判断文件是否存在,如果存在直接上传成功并返回skipUpload=true,如果不存在返回skipUpload=false需要再次调用该接口的POST方法", tags = {"filetransfer"})
-    @RequestMapping(value = "/uploadcommonfile", method = RequestMethod.GET)
+    @RequestMapping(value = "/uploadCommonFile", method = RequestMethod.GET)
     @MyLog(operation = "极速上传", module = CURRENT_MODULE)
     @ResponseBody
-    public RestResult<UploadFileVo> uploadcommonfile(UploadFileDTO uploadFileDto) {
-        String userId = "common";
+    public RestResult<UploadFileVo> uploadCommonFile(UploadFileDTO uploadFileDto) {
+        String userId = commonUserId;
 
         boolean isCheckSuccess = storageService.checkStorage(userId, uploadFileDto.getTotalSize());
         if (!isCheckSuccess) {
@@ -112,7 +116,19 @@ public class FiletransferController {
         return RestResult.success().data(uploadFileVo);
 
     }
+    @Operation(summary = "上传文件", description = "真正的上传文件接口", tags = {"filetransfer"})
+    @RequestMapping(value = "/uploadCommonFile", method = RequestMethod.POST)
+    @MyLog(operation = "上传文件", module = CURRENT_MODULE)
+    @ResponseBody
+    public RestResult<UploadFileVo> uploadCommonFile(HttpServletRequest request, UploadFileDTO uploadFileDto) {
+        String userId =commonUserId;
 
+        filetransferService.uploadFile(request, uploadFileDto, userId);
+
+        UploadFileVo uploadFileVo = new UploadFileVo();
+        return RestResult.success().data(uploadFileVo);
+
+    }
 
     @Operation(summary = "下载文件", description = "下载文件接口", tags = {"filetransfer"})
     @MyLog(operation = "下载文件", module = CURRENT_MODULE)
@@ -155,6 +171,56 @@ public class FiletransferController {
         filetransferService.downloadFile(httpServletResponse, downloadFileDTO);
     }
 
+
+
+    @Operation(summary = "下载文件", description = "下载文件接口", tags = {"filetransfer"})
+    @MyLog(operation = "下载文件", module = CURRENT_MODULE)
+    @RequestMapping(value = "/downloadCommonFile", method = RequestMethod.GET)
+    public void downloadCommonFile(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, DownloadFileDTO downloadFileDTO) {
+//        Cookie[] cookieArr = httpServletRequest.getCookies();
+        String token = "";
+//        if (cookieArr != null) {
+//            for (Cookie cookie : cookieArr) {
+//                if ("token".equals(cookie.getName())) {
+//                    token = cookie.getValue();
+//                }
+//            }
+//        }
+        boolean authResult = fileDealComp.checkAuthDownloadAndPreview(downloadFileDTO.getShareBatchNum(),
+                downloadFileDTO.getExtractionCode(),
+                token,
+                downloadFileDTO.getUserFileId(), null);
+        if (!authResult) {
+            log.error("没有权限下载!!!");
+            return;
+        }
+        httpServletResponse.setContentType("application/force-download");// 设置强制下载不打开
+        UserFile userFile = userFileService.getById(downloadFileDTO.getUserFileId());
+        String fileName = "";
+        if (userFile.getIsDir() == 1) {
+            fileName = userFile.getFileName() + ".zip";
+        } else {
+            fileName = userFile.getFileName() + "." + userFile.getExtendName();
+
+        }
+        try {
+            fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+
+        httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
+
+        filetransferService.downloadFile(httpServletResponse, downloadFileDTO);
+    }
+
+
+
+
+
+
+
+
     @Operation(summary = "批量下载文件", description = "批量下载文件", tags = {"filetransfer"})
     @RequestMapping(value = "/batchDownloadFile", method = RequestMethod.GET)
     @MyLog(operation = "批量下载文件", module = CURRENT_MODULE)
@@ -202,6 +268,59 @@ public class FiletransferController {
         filetransferService.downloadUserFileList(httpServletResponse, userFile.getFilePath(), fileName, userFileIds);
     }
 
+
+
+    @Operation(summary = "批量下载文件", description = "批量下载文件", tags = {"filetransfer"})
+    @RequestMapping(value = "/batchCommonDownloadFile", method = RequestMethod.GET)
+    @MyLog(operation = "批量下载文件", module = CURRENT_MODULE)
+    @ResponseBody
+    public void batchCommonDownloadFile(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BatchDownloadFileDTO batchDownloadFileDTO) {
+//        Cookie[] cookieArr = httpServletRequest.getCookies();
+        String token = "";
+//        if (cookieArr != null) {
+//            for (Cookie cookie : cookieArr) {
+//                if ("token".equals(cookie.getName())) {
+//                    token = cookie.getValue();
+//                }
+//            }
+//        }
+        boolean authResult = fileDealComp.checkAuthDownloadAndPreview(batchDownloadFileDTO.getShareBatchNum(),
+                batchDownloadFileDTO.getExtractionCode(),
+                token,
+                batchDownloadFileDTO.getUserFileIds(), null);
+        if (!authResult) {
+            log.error("没有权限下载!!!");
+            return;
+        }
+
+        String files = batchDownloadFileDTO.getUserFileIds();
+        String[] userFileIdStrs = files.split(",");
+        List<String> userFileIds = new ArrayList<>();
+        for(String userFileId : userFileIdStrs) {
+            UserFile userFile = userFileService.getById(userFileId);
+            if (userFile.getIsDir() == 0) {
+                userFileIds.add(userFileId);
+            } else {
+                QiwenFile qiwenFile = new QiwenFile(userFile.getFilePath(), userFile.getFileName(), true);
+                List<UserFile> userFileList = userFileService.selectUserFileByLikeRightFilePath(qiwenFile.getPath(), userFile.getUserId());
+                List<String> userFileIds1 = userFileList.stream().map(UserFile::getUserFileId).collect(Collectors.toList());
+                userFileIds.add(userFile.getUserFileId());
+                userFileIds.addAll(userFileIds1);
+            }
+
+        }
+        UserFile userFile = userFileService.getById(userFileIdStrs[0]);
+        httpServletResponse.setContentType("application/force-download");// 设置强制下载不打开
+        Date date = new Date();
+        String fileName = String.valueOf(date.getTime());
+        httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + fileName + ".zip");// 设置文件名
+        filetransferService.downloadUserFileList(httpServletResponse, userFile.getFilePath(), fileName, userFileIds);
+    }
+
+
+
+
+
     @Operation(summary="预览文件", description="用于文件预览", tags = {"filetransfer"})
     @GetMapping("/preview")
     public void preview(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,  PreviewDTO previewDTO) throws IOException {
@@ -290,6 +409,111 @@ public class FiletransferController {
 
     }
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+    @Operation(summary="预览文件", description="用于文件预览", tags = {"filetransfer"})
+    @GetMapping("/previewCommon")
+    public void previewCommon(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,  PreviewDTO previewDTO) throws IOException {
+
+        if (previewDTO.getPlatform() != null && previewDTO.getPlatform() == 2) {
+            filetransferService.previewPictureFile(httpServletResponse, previewDTO);
+            return ;
+        }
+        // token逻辑去掉
+        String token = "";
+
+        UserFile userFile = userFileService.getById(previewDTO.getUserFileId());
+        boolean authResult = fileDealComp.checkAuthDownloadAndPreview(previewDTO.getShareBatchNum(),
+                previewDTO.getExtractionCode(),
+                token,
+                previewDTO.getUserFileId(),
+                previewDTO.getPlatform());
+
+        if (!authResult) {
+            log.error("没有权限预览!!!");
+            return;
+        }
+
+        String fileName = userFile.getFileName() + "." + userFile.getExtendName();
+        try {
+            fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+
+        httpServletResponse.addHeader("Content-Disposition", "fileName=" + fileName);// 设置文件名
+        String mime = MimeUtils.getMime(userFile.getExtendName());
+        httpServletResponse.setHeader("Content-Type", mime);
+        if (UFOPUtils.isImageFile(userFile.getExtendName())) {
+            httpServletResponse.setHeader("cache-control", "public");
+        }
+
+        FileBean fileBean = fileService.getById(userFile.getFileId());
+        if (UFOPUtils.isVideoFile(userFile.getExtendName()) || "mp3".equalsIgnoreCase(userFile.getExtendName()) || "flac".equalsIgnoreCase(userFile.getExtendName())) {
+            //获取从那个字节开始读取文件
+            String rangeString = httpServletRequest.getHeader("Range");
+            int start = 0;
+            if (StringUtils.isNotBlank(rangeString)) {
+                start = Integer.parseInt(rangeString.substring(rangeString.indexOf("=") + 1, rangeString.indexOf("-")));
+            }
+
+            Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
+            DownloadFile downloadFile = new DownloadFile();
+            downloadFile.setFileUrl(fileBean.getFileUrl());
+            Range range = new Range();
+            range.setStart(start);
+
+            if (start + 1024 * 1024 * 1 >= fileBean.getFileSize().intValue()) {
+                range.setLength(fileBean.getFileSize().intValue() - start);
+            } else {
+                range.setLength(1024 * 1024 * 1);
+            }
+            downloadFile.setRange(range);
+            InputStream inputStream = downloader.getInputStream(downloadFile);
+
+            OutputStream outputStream = httpServletResponse.getOutputStream();
+            try {
+
+                //返回码需要为206,代表只处理了部分请求,响应了部分数据
+
+                httpServletResponse.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
+                // 每次请求只返回1MB的视频流
+
+                httpServletResponse.setHeader("Accept-Ranges", "bytes");
+                //设置此次相应返回的数据范围
+                httpServletResponse.setHeader("Content-Range", "bytes " + start + "-" + (fileBean.getFileSize() - 1) + "/" + fileBean.getFileSize());
+                IOUtils.copy(inputStream, outputStream);
+
+
+            } finally {
+                IOUtils.closeQuietly(inputStream);
+                IOUtils.closeQuietly(outputStream);
+                if (downloadFile.getOssClient() != null) {
+                    downloadFile.getOssClient().shutdown();
+                }
+            }
+
+        } else {
+            filetransferService.previewFile(httpServletResponse, previewDTO);
+        }
+
+    }
+
+
+
+
+
     @Operation(summary = "获取存储信息", description = "获取存储信息", tags = {"filetransfer"})
     @RequestMapping(value = "/getstorage", method = RequestMethod.GET)
     @ResponseBody
@@ -312,5 +536,24 @@ public class FiletransferController {
 
     }
 
+    @Operation(summary = "获取存储信息", description = "获取存储信息", tags = {"filetransfer"})
+    @RequestMapping(value = "/getCommonStorage", method = RequestMethod.GET)
+    @ResponseBody
+    public RestResult<StorageBean> getCommonStorage() {
+
+        String userId = commonUserId;
+        StorageBean storageBean = new StorageBean();
+
+        storageBean.setUserId(userId);
+
+
+        Long storageSize = filetransferService.selectStorageSizeByUserId(userId);
+        StorageBean storage = new StorageBean();
+        storage.setUserId(userId);
+        storage.setStorageSize(storageSize);
+        Long totalStorageSize = storageService.getTotalStorageSize(userId);
+        storage.setTotalStorageSize(totalStorageSize);
+        return RestResult.success().data(storage);
 
+    }
 }