This commit is contained in:
2024-12-05 16:15:09 +08:00
3 changed files with 15 additions and 12 deletions

View File

@@ -79,7 +79,7 @@ public class FileRecorderImpl implements FileRecorder {
return null; return null;
} }
StorageDO storageDO = storageMapper.lambdaQuery().eq(StorageDO::getId, file.getStorageId()).one(); StorageDO storageDO = storageMapper.lambdaQuery().eq(StorageDO::getId, file.getStorageId()).one();
return file.toFileInfo(storageDO.getCode(),storageDO.getBucketName()); return file.toFileInfo(storageDO);
} }
@Override @Override

View File

@@ -22,6 +22,7 @@ import lombok.Data;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.dromara.x.file.storage.core.FileInfo; import org.dromara.x.file.storage.core.FileInfo;
import top.continew.admin.system.enums.FileTypeEnum; 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.constant.StringConstants;
import top.continew.starter.core.util.StrUtils; import top.continew.starter.core.util.StrUtils;
import top.continew.starter.extension.crud.model.entity.BaseDO; import top.continew.starter.extension.crud.model.entity.BaseDO;
@@ -85,11 +86,10 @@ public class FileDO extends BaseDO {
/** /**
* 转换为 X-File-Storage 文件信息对象 * 转换为 X-File-Storage 文件信息对象
* *
* @param storageCode 存储编码 * @param storageDO 存储桶信息
* @param bucketName 桶名称
* @return X-File-Storage 文件信息对象 * @return X-File-Storage 文件信息对象
*/ */
public FileInfo toFileInfo(String storageCode,String bucketName) { public FileInfo toFileInfo(StorageDO storageDO) {
FileInfo fileInfo = new FileInfo(); FileInfo fileInfo = new FileInfo();
fileInfo.setUrl(this.url); fileInfo.setUrl(this.url);
fileInfo.setSize(this.size); fileInfo.setSize(this.size);
@@ -100,10 +100,10 @@ public class FileDO extends BaseDO {
.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.setBasePath(StringConstants.EMPTY);
// 优化 path 处理 // 优化 path 处理
fileInfo.setPath(extractRelativePath(this.url,bucketName)); fileInfo.setPath(extractRelativePath(this.url,storageDO));
fileInfo.setExt(this.extension); fileInfo.setExt(this.extension);
fileInfo.setPlatform(storageCode); fileInfo.setPlatform(storageDO.getCode());
fileInfo.setThUrl(this.thumbnailUrl); fileInfo.setThUrl(this.thumbnailUrl);
fileInfo.setThFilename(StrUtil.contains(this.thumbnailUrl, StringConstants.SLASH) fileInfo.setThFilename(StrUtil.contains(this.thumbnailUrl, StringConstants.SLASH)
? StrUtil.subAfter(this.thumbnailUrl, StringConstants.SLASH, true) ? StrUtil.subAfter(this.thumbnailUrl, StringConstants.SLASH, true)
@@ -116,14 +116,17 @@ public class FileDO extends BaseDO {
* 将文件路径处理成资源路径 * 将文件路径处理成资源路径
* 例如: * 例如:
* http://domain.cn/bucketName/2024/11/27/6746ec3b2907f0de80afdd70.png => 2024/11/27/ * 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 url 文件路径
* @param bucketName 桶名称 * @param storageDO 存储桶信息
* @return * @return
*/ */
@SneakyThrows @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; url = StrUtil.subBefore(url, StringConstants.SLASH, true) + StringConstants.SLASH;
if (storageDO.getType().equals(StorageTypeEnum.LOCAL)){
return url;
}
// 提取 URL 中的路径部分 // 提取 URL 中的路径部分
String fullPath = new URL(url).getPath(); String fullPath = new URL(url).getPath();
// 移除开头的斜杠 // 移除开头的斜杠
@@ -131,8 +134,8 @@ public class FileDO extends BaseDO {
? fullPath.substring(1) ? fullPath.substring(1)
: fullPath; : fullPath;
// 如果路径以 bucketName 开头,则移除 bucketName 例如: bucketName/2024/11/27/ -> 2024/11/27/ // 如果路径以 bucketName 开头,则移除 bucketName 例如: bucketName/2024/11/27/ -> 2024/11/27/
if (relativePath.startsWith(bucketName)) { if (relativePath.startsWith(storageDO.getBucketName())) {
return StrUtil.split(relativePath, bucketName).get(1); return StrUtil.split(relativePath, storageDO.getBucketName()).get(1);
} }
return relativePath; return relativePath;
} }

View File

@@ -73,7 +73,7 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
for (Map.Entry<Long, List<FileDO>> entry : fileListGroup.entrySet()) { for (Map.Entry<Long, List<FileDO>> entry : fileListGroup.entrySet()) {
StorageDO storage = storageService.getById(entry.getKey()); StorageDO storage = storageService.getById(entry.getKey());
for (FileDO file : entry.getValue()) { for (FileDO file : entry.getValue()) {
FileInfo fileInfo = file.toFileInfo(storage.getCode(),storage.getBucketName()); FileInfo fileInfo = file.toFileInfo(storage);
fileStorageService.delete(fileInfo); fileStorageService.delete(fileInfo);
} }
} }