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 99f2ef99..2aab00c0 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 @@ -78,7 +78,8 @@ public class FileRecorderImpl implements FileRecorder { if (null == file) { return null; } - return file.toFileInfo(storageMapper.lambdaQuery().eq(StorageDO::getId, file.getStorageId()).one().getCode()); + StorageDO storageDO = storageMapper.lambdaQuery().eq(StorageDO::getId, file.getStorageId()).one(); + return file.toFileInfo(storageDO.getCode(),storageDO.getBucketName()); } @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 0ddf9dc4..fa31b056 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 @@ -19,6 +19,7 @@ package top.continew.admin.system.model.entity; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; +import lombok.SneakyThrows; import org.dromara.x.file.storage.core.FileInfo; import top.continew.admin.system.enums.FileTypeEnum; import top.continew.starter.core.constant.StringConstants; @@ -26,6 +27,7 @@ import top.continew.starter.core.util.StrUtils; import top.continew.starter.extension.crud.model.entity.BaseDO; import java.io.Serial; +import java.net.URL; /** * 文件实体 @@ -84,26 +86,55 @@ public class FileDO extends BaseDO { * 转换为 X-File-Storage 文件信息对象 * * @param storageCode 存储编码 + * @param bucketName 桶名称 * @return X-File-Storage 文件信息对象 */ - public FileInfo toFileInfo(String storageCode) { + public FileInfo toFileInfo(String storageCode,String bucketName) { FileInfo fileInfo = new FileInfo(); fileInfo.setUrl(this.url); fileInfo.setSize(this.size); fileInfo.setFilename(StrUtil.contains(this.url, StringConstants.SLASH) - ? StrUtil.subAfter(this.url, StringConstants.SLASH, true) - : this.url); + ? StrUtil.subAfter(this.url, StringConstants.SLASH, true) + : this.url); fileInfo.setOriginalFilename(StrUtils - .blankToDefault(this.extension, this.name, ex -> this.name + StringConstants.DOT + ex)); + .blankToDefault(this.extension, this.name, ex -> this.name + StringConstants.DOT + ex)); fileInfo.setBasePath(StringConstants.EMPTY); - fileInfo.setPath(StrUtil.subBefore(this.url, StringConstants.SLASH, true) + StringConstants.SLASH); + // 优化 path 处理 + fileInfo.setPath(extractRelativePath(this.url,bucketName)); + fileInfo.setExt(this.extension); fileInfo.setPlatform(storageCode); fileInfo.setThUrl(this.thumbnailUrl); fileInfo.setThFilename(StrUtil.contains(this.thumbnailUrl, StringConstants.SLASH) - ? StrUtil.subAfter(this.thumbnailUrl, StringConstants.SLASH, true) - : this.thumbnailUrl); + ? StrUtil.subAfter(this.thumbnailUrl, StringConstants.SLASH, true) + : this.thumbnailUrl); fileInfo.setThSize(this.thumbnailSize); return fileInfo; } + + /** + * 将文件路径处理成资源路径 + * 例如: + * http://domain.cn/bucketName/2024/11/27/6746ec3b2907f0de80afdd70.png => 2024/11/27/ + * http://bucketName.damain.cn/2024/11/27/6746ec3b2907f0de80afdd70.png => 2024/11/27/ + * @param url 文件路径 + * @param bucketName 桶名称 + * @return + */ + @SneakyThrows + private static String extractRelativePath(String url, String bucketName) { + url = StrUtil.subBefore(url, StringConstants.SLASH, true) + StringConstants.SLASH; + // 提取 URL 中的路径部分 + String fullPath = new URL(url).getPath(); + // 移除开头的斜杠 + String relativePath = fullPath.startsWith(StringConstants.SLASH) + ? fullPath.substring(1) + : fullPath; + // 如果路径以 bucketName 开头,则移除 bucketName 例如: bucketName/2024/11/27/ -> 2024/11/27/ + if (relativePath.startsWith(bucketName)) { + return StrUtil.split(relativePath, bucketName).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 aceda06f..07053f4a 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 @@ public class FileServiceImpl extends BaseServiceImpl> entry : fileListGroup.entrySet()) { StorageDO storage = storageService.getById(entry.getKey()); for (FileDO file : entry.getValue()) { - FileInfo fileInfo = file.toFileInfo(storage.getCode()); + FileInfo fileInfo = file.toFileInfo(storage.getCode(),storage.getBucketName()); fileStorageService.delete(fileInfo); } }