From 31db38d419351f5037405ea99932b98af63d7e27 Mon Sep 17 00:00:00 2001 From: Freedom <459102951@qq.com> Date: Sun, 4 Jan 2026 17:20:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91=EF=BC=9B=E5=88=9D?= =?UTF-8?q?=E6=AD=A5=E5=AE=8C=E5=96=84=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fs-admin/src/main/resources/application.yml | 1 + .../fs/file/controller/FileController.java | 43 ------------------- .../controller/FileTransferController.java | 32 ++++++++++++++ .../xddcodec/fs/file/domain/qry/FileQry.java | 3 ++ .../file/service/FileTransferTaskService.java | 9 ++++ .../service/impl/FileInfoServiceImpl.java | 5 ++- .../service/impl/FileShareServiceImpl.java | 4 ++ .../impl/FileTransferTaskServiceImpl.java | 27 ++++++++++++ 8 files changed, 80 insertions(+), 44 deletions(-) diff --git a/fs-admin/src/main/resources/application.yml b/fs-admin/src/main/resources/application.yml index c25fe0f4..d97a100f 100644 --- a/fs-admin/src/main/resources/application.yml +++ b/fs-admin/src/main/resources/application.yml @@ -131,6 +131,7 @@ sa-token: is-concurrent: false # 在多人登录同一账号时,是否共用一个 token (为 true 时所有登录共用一个 token, 为 false 时每次登录新建一个 token) is-share: false + is-read-body: true # token 风格(默认可取值:uuid、simple-uuid、random-32、random-64、random-128、tik) token-style: random-128 # 是否输出操作日志 diff --git a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/controller/FileController.java b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/controller/FileController.java index a74a7415..89506ce0 100644 --- a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/controller/FileController.java +++ b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/controller/FileController.java @@ -1,6 +1,5 @@ package com.xddcodec.fs.file.controller; -import com.xddcodec.fs.file.domain.FileInfo; import com.xddcodec.fs.file.domain.dto.CreateDirectoryCmd; import com.xddcodec.fs.file.domain.dto.MoveFileCmd; import com.xddcodec.fs.file.domain.dto.RenameFileCmd; @@ -12,24 +11,15 @@ import com.xddcodec.fs.file.service.FileInfoService; import com.xddcodec.fs.file.service.FileRecycleService; import com.xddcodec.fs.file.service.FileUserFavoritesService; import com.xddcodec.fs.framework.common.domain.Result; -import com.xddcodec.fs.framework.common.exception.StorageOperationException; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; -import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.InputStreamResource; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.io.InputStream; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.util.List; /** @@ -75,39 +65,6 @@ public class FileController { return Result.ok(list); } - @GetMapping("/download/{fileId}") - @Operation(summary = "下载文件", description = "根据文件ID下载文件") - @Parameter(name = "fileId", description = "文件ID", in = ParameterIn.PATH, required = true) - public ResponseEntity downloadFile(@Parameter(description = "文件ID") @PathVariable("fileId") String fileId) { - - try { - FileInfo fileInfo = fileInfoService.getById(fileId); - if (fileInfo == null || fileInfo.getIsDir() || fileInfo.getIsDeleted()) { - return ResponseEntity.notFound().build(); - } - - InputStream inputStream = fileInfoService.downloadFile(fileId); - InputStreamResource resource = new InputStreamResource(inputStream); - - String encodedFileName = URLEncoder.encode(fileInfo.getOriginalName(), StandardCharsets.UTF_8).replaceAll("\\+", "%20"); - - return ResponseEntity.ok() - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFileName) - .contentType(MediaType.parseMediaType(fileInfo.getMimeType())) - .contentLength(fileInfo.getSize()) - .body(resource); - } catch (StorageOperationException e) { - log.error("下载文件失败: {}", e.getMessage(), e); - // 根据异常消息返回适当的HTTP状态码 - String message = e.getMessage().toLowerCase(); - if (message.contains("文件不存在") || message.contains("nosuchkey")) { - return ResponseEntity.notFound().build(); - } - // 其他错误返回500 - return ResponseEntity.status(500).build(); - } - } - @GetMapping("/url/{fileId}") @Operation(summary = "获取文件URL", description = "获取文件的访问URL") @Parameters(value = {@Parameter(name = "fileId", description = "文件ID", required = true), @Parameter(name = "expireSeconds", description = "URL有效时间(秒),如果不支持或永久有效可为null")}) diff --git a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/controller/FileTransferController.java b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/controller/FileTransferController.java index 98b795ec..32e9b346 100644 --- a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/controller/FileTransferController.java +++ b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/controller/FileTransferController.java @@ -5,17 +5,25 @@ import com.xddcodec.fs.file.domain.dto.InitUploadCmd; import com.xddcodec.fs.file.domain.dto.UploadChunkCmd; import com.xddcodec.fs.file.domain.qry.TransferFilesQry; import com.xddcodec.fs.file.domain.vo.CheckUploadResultVO; +import com.xddcodec.fs.file.domain.vo.FileDownloadVO; import com.xddcodec.fs.file.domain.vo.FileTransferTaskVO; import com.xddcodec.fs.file.service.FileTransferTaskService; import com.xddcodec.fs.framework.common.domain.Result; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Set; @@ -101,4 +109,28 @@ public class FileTransferController { fileTransferTaskService.clearTransfers(); return Result.ok(); } + + @GetMapping("/download/{fileId}") + @Operation(summary = "下载文件", description = "根据文件ID下载文件") + @Parameter(name = "fileId", description = "文件ID", in = ParameterIn.PATH, required = true) + public ResponseEntity downloadFile(@Parameter(description = "文件ID") @PathVariable("fileId") String fileId) { + + try { + // 获取文件信息和文件流 + FileDownloadVO fileDownload = fileTransferTaskService.downloadFile(fileId); + + // 设置响应头 + HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_DISPOSITION, + "attachment; filename=\"" + URLEncoder.encode(fileDownload.getFileName(), StandardCharsets.UTF_8) + "\""); + headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream"); + + return ResponseEntity.ok() + .headers(headers) + .contentLength(fileDownload.getFileSize()) + .body(fileDownload.getResource()); + } catch (Exception e) { + throw new RuntimeException("文件下载失败", e); + } + } } diff --git a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/domain/qry/FileQry.java b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/domain/qry/FileQry.java index 242c4fb7..a6f277e0 100644 --- a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/domain/qry/FileQry.java +++ b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/domain/qry/FileQry.java @@ -28,4 +28,7 @@ public class FileQry { @Schema(description = "最近使用", example = "true") private Boolean isRecents; + + @Schema(description = "是否目录", example = "true") + private Boolean isDir; } diff --git a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/FileTransferTaskService.java b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/FileTransferTaskService.java index 93c40d05..54d16571 100644 --- a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/FileTransferTaskService.java +++ b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/FileTransferTaskService.java @@ -8,6 +8,7 @@ import com.xddcodec.fs.file.domain.dto.InitUploadCmd; import com.xddcodec.fs.file.domain.dto.UploadChunkCmd; import com.xddcodec.fs.file.domain.qry.TransferFilesQry; import com.xddcodec.fs.file.domain.vo.CheckUploadResultVO; +import com.xddcodec.fs.file.domain.vo.FileDownloadVO; import com.xddcodec.fs.file.domain.vo.FileTransferTaskVO; import java.util.List; @@ -86,4 +87,12 @@ public interface FileTransferTaskService extends IService { * 清空已完成传输列表 */ void clearTransfers(); + + /** + * 下载文件 + * + * @param fileId 文件ID + * @return + */ + FileDownloadVO downloadFile(String fileId); } diff --git a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/impl/FileInfoServiceImpl.java b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/impl/FileInfoServiceImpl.java index f733e7db..8bf6605b 100644 --- a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/impl/FileInfoServiceImpl.java +++ b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/impl/FileInfoServiceImpl.java @@ -423,11 +423,14 @@ public class FileInfoServiceImpl extends ServiceImpl i } else { wrapper.and(FILE_INFO.STORAGE_PLATFORM_SETTING_ID.eq(storagePlatformSettingId)); } + if (Boolean.TRUE.equals(qry.getIsDir())) { + wrapper.and(FILE_INFO.IS_DIR.eq(true)); + } // 最近使用过滤(优先级最高) if (Boolean.TRUE.equals(qry.getIsRecents())) { wrapper.and(FILE_INFO.IS_DIR.eq(false)) .orderBy(FILE_INFO.LAST_ACCESS_TIME.desc()) - .limit(8); // 限制返回 8 条 + .limit(20); log.info("用户 {} 查询最近使用文件", userId); return this.listAs(wrapper, FileVO.class); diff --git a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/impl/FileShareServiceImpl.java b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/impl/FileShareServiceImpl.java index 200c2d49..6913c826 100644 --- a/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/impl/FileShareServiceImpl.java +++ b/fs-modules/fs-file/src/main/java/com/xddcodec/fs/file/service/impl/FileShareServiceImpl.java @@ -280,6 +280,10 @@ public class FileShareServiceImpl extends ServiceImpl