mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-11-04 09:01:37 +08:00 
			
		
		
		
	fix: 修复文件管理删除文件异常或不陈工的情况
This commit is contained in:
		@@ -78,7 +78,8 @@ public class FileRecorderImpl implements FileRecorder {
 | 
				
			|||||||
        if (null == file) {
 | 
					        if (null == file) {
 | 
				
			||||||
            return null;
 | 
					            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
 | 
					    @Override
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,6 +19,7 @@ package top.continew.admin.system.model.entity;
 | 
				
			|||||||
import cn.hutool.core.util.StrUtil;
 | 
					import cn.hutool.core.util.StrUtil;
 | 
				
			||||||
import com.baomidou.mybatisplus.annotation.TableName;
 | 
					import com.baomidou.mybatisplus.annotation.TableName;
 | 
				
			||||||
import lombok.Data;
 | 
					import lombok.Data;
 | 
				
			||||||
 | 
					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.starter.core.constant.StringConstants;
 | 
					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 top.continew.starter.extension.crud.model.entity.BaseDO;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.Serial;
 | 
					import java.io.Serial;
 | 
				
			||||||
 | 
					import java.net.URL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 文件实体
 | 
					 * 文件实体
 | 
				
			||||||
@@ -84,26 +86,55 @@ public class FileDO extends BaseDO {
 | 
				
			|||||||
     * 转换为 X-File-Storage 文件信息对象
 | 
					     * 转换为 X-File-Storage 文件信息对象
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param storageCode 存储编码
 | 
					     * @param storageCode 存储编码
 | 
				
			||||||
 | 
					     * @param bucketName 桶名称
 | 
				
			||||||
     * @return X-File-Storage 文件信息对象
 | 
					     * @return X-File-Storage 文件信息对象
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public FileInfo toFileInfo(String storageCode) {
 | 
					    public FileInfo toFileInfo(String storageCode,String bucketName) {
 | 
				
			||||||
        FileInfo fileInfo = new FileInfo();
 | 
					        FileInfo fileInfo = new FileInfo();
 | 
				
			||||||
        fileInfo.setUrl(this.url);
 | 
					        fileInfo.setUrl(this.url);
 | 
				
			||||||
        fileInfo.setSize(this.size);
 | 
					        fileInfo.setSize(this.size);
 | 
				
			||||||
        fileInfo.setFilename(StrUtil.contains(this.url, StringConstants.SLASH)
 | 
					        fileInfo.setFilename(StrUtil.contains(this.url, StringConstants.SLASH)
 | 
				
			||||||
            ? StrUtil.subAfter(this.url, StringConstants.SLASH, true)
 | 
					                ? StrUtil.subAfter(this.url, StringConstants.SLASH, true)
 | 
				
			||||||
            : this.url);
 | 
					                : this.url);
 | 
				
			||||||
        fileInfo.setOriginalFilename(StrUtils
 | 
					        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.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.setExt(this.extension);
 | 
				
			||||||
        fileInfo.setPlatform(storageCode);
 | 
					        fileInfo.setPlatform(storageCode);
 | 
				
			||||||
        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)
 | 
				
			||||||
            : this.thumbnailUrl);
 | 
					                : this.thumbnailUrl);
 | 
				
			||||||
        fileInfo.setThSize(this.thumbnailSize);
 | 
					        fileInfo.setThSize(this.thumbnailSize);
 | 
				
			||||||
        return fileInfo;
 | 
					        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;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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());
 | 
					                FileInfo fileInfo = file.toFileInfo(storage.getCode(),storage.getBucketName());
 | 
				
			||||||
                fileStorageService.delete(fileInfo);
 | 
					                fileStorageService.delete(fileInfo);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user