Skip to content

Commit

Permalink
fix: 修复本地文件管理删除文件异常或不成功的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
KAI committed Dec 3, 2024
1 parent a654637 commit c7b58a0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -116,23 +116,26 @@ 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();
// 移除开头的斜杠
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);
if (relativePath.startsWith(storageDO.getBucketName())) {
return StrUtil.split(relativePath, storageDO.getBucketName()).get(1);
}
return relativePath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void beforeDelete(List<Long> ids) {
for (Map.Entry<Long, List<FileDO>> 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);
}
}
Expand Down

0 comments on commit c7b58a0

Please sign in to comment.