From c7b58a0fd167c566f6680c87cc455b71c42b8eda Mon Sep 17 00:00:00 2001 From: KAI <1373639299@qq.com> Date: Tue, 3 Dec 2024 18:20:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=AE=A1=E7=90=86=E5=88=A0=E9=99=A4=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=BC=82=E5=B8=B8=E6=88=96=E4=B8=8D=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/config/file/FileRecorderImpl.java | 2 +- .../admin/system/model/entity/FileDO.java | 23 +++++++++++-------- .../system/service/impl/FileServiceImpl.java | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/continew-module-system/src/main/java/top/continew/admin/system/config/file/FileRecorderImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/config/file/FileRecorderImpl.java index 2aab00c02..b15681d76 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/config/file/FileRecorderImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/config/file/FileRecorderImpl.java @@ -79,7 +79,7 @@ public FileInfo getByUrl(String url) { return null; } StorageDO storageDO = storageMapper.lambdaQuery().eq(StorageDO::getId, file.getStorageId()).one(); - return file.toFileInfo(storageDO.getCode(),storageDO.getBucketName()); + return file.toFileInfo(storageDO); } @Override diff --git a/continew-module-system/src/main/java/top/continew/admin/system/model/entity/FileDO.java b/continew-module-system/src/main/java/top/continew/admin/system/model/entity/FileDO.java index fa31b0563..277b6c029 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/model/entity/FileDO.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/model/entity/FileDO.java @@ -22,6 +22,7 @@ import lombok.SneakyThrows; import org.dromara.x.file.storage.core.FileInfo; import top.continew.admin.system.enums.FileTypeEnum; +import top.continew.admin.system.enums.StorageTypeEnum; import top.continew.starter.core.constant.StringConstants; import top.continew.starter.core.util.StrUtils; import top.continew.starter.extension.crud.model.entity.BaseDO; @@ -85,11 +86,10 @@ public class FileDO extends BaseDO { /** * 转换为 X-File-Storage 文件信息对象 * - * @param storageCode 存储编码 - * @param bucketName 桶名称 + * @param storageDO 存储桶信息 * @return X-File-Storage 文件信息对象 */ - public FileInfo toFileInfo(String storageCode,String bucketName) { + public FileInfo toFileInfo(StorageDO storageDO) { FileInfo fileInfo = new FileInfo(); fileInfo.setUrl(this.url); fileInfo.setSize(this.size); @@ -100,10 +100,10 @@ public FileInfo toFileInfo(String storageCode,String bucketName) { .blankToDefault(this.extension, this.name, ex -> this.name + StringConstants.DOT + ex)); fileInfo.setBasePath(StringConstants.EMPTY); // 优化 path 处理 - fileInfo.setPath(extractRelativePath(this.url,bucketName)); + fileInfo.setPath(extractRelativePath(this.url,storageDO)); fileInfo.setExt(this.extension); - fileInfo.setPlatform(storageCode); + fileInfo.setPlatform(storageDO.getCode()); fileInfo.setThUrl(this.thumbnailUrl); fileInfo.setThFilename(StrUtil.contains(this.thumbnailUrl, StringConstants.SLASH) ? StrUtil.subAfter(this.thumbnailUrl, StringConstants.SLASH, true) @@ -116,14 +116,17 @@ public FileInfo toFileInfo(String storageCode,String bucketName) { * 将文件路径处理成资源路径 * 例如: * http://domain.cn/bucketName/2024/11/27/6746ec3b2907f0de80afdd70.png => 2024/11/27/ - * http://bucketName.damain.cn/2024/11/27/6746ec3b2907f0de80afdd70.png => 2024/11/27/ + * http://bucketName.domain.cn/2024/11/27/6746ec3b2907f0de80afdd70.png => 2024/11/27/ * @param url 文件路径 - * @param bucketName 桶名称 + * @param storageDO 存储桶信息 * @return */ @SneakyThrows - private static String extractRelativePath(String url, String bucketName) { + private static String extractRelativePath(String url, StorageDO storageDO) { url = StrUtil.subBefore(url, StringConstants.SLASH, true) + StringConstants.SLASH; + if (storageDO.getType().equals(StorageTypeEnum.LOCAL)){ + return url; + } // 提取 URL 中的路径部分 String fullPath = new URL(url).getPath(); // 移除开头的斜杠 @@ -131,8 +134,8 @@ private static String extractRelativePath(String url, String bucketName) { ? fullPath.substring(1) : fullPath; // 如果路径以 bucketName 开头,则移除 bucketName 例如: bucketName/2024/11/27/ -> 2024/11/27/ - if (relativePath.startsWith(bucketName)) { - return StrUtil.split(relativePath, bucketName).get(1); + if (relativePath.startsWith(storageDO.getBucketName())) { + return StrUtil.split(relativePath, storageDO.getBucketName()).get(1); } return relativePath; } diff --git a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java index 07053f4ab..3d7c10d6c 100644 --- a/continew-module-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java +++ b/continew-module-system/src/main/java/top/continew/admin/system/service/impl/FileServiceImpl.java @@ -73,7 +73,7 @@ protected void beforeDelete(List ids) { for (Map.Entry> entry : fileListGroup.entrySet()) { StorageDO storage = storageService.getById(entry.getKey()); for (FileDO file : entry.getValue()) { - FileInfo fileInfo = file.toFileInfo(storage.getCode(),storage.getBucketName()); + FileInfo fileInfo = file.toFileInfo(storage); fileStorageService.delete(fileInfo); } }