fix:文件下载跳过权限校验

This commit is contained in:
2026-03-19 20:51:49 +08:00
parent 4bceba3b4e
commit 85c3d2ff52
3 changed files with 86 additions and 14 deletions

View File

@@ -124,6 +124,9 @@ public class FileMetadataInfoResp extends BaseResp implements Serializable {
@Schema(description = "知识库名称")
private String knowledgeBaseName;
@Schema(description = "所有父级ID,包括文件本身id")
private String allParentIds;
private String approvalStatus;
private Integer approveType;
private String tempMetadata;

View File

@@ -125,18 +125,19 @@ public class FileMetadataHierarchyHelper {
/**
* 获取文件元数据并构建父目录缓存(用于需要在中间步骤做更多处理的场景)
*
* @param fileIdList 文件ID列表
* @param fileIdList 文件 ID 列表
* @return 文件元数据和父目录缓存的结果对象
*/
public FileHierarchyResult getFilesWithParentCache(List<Long> fileIdList) {
if (CollectionUtils.isEmpty(fileIdList)) {
return new FileHierarchyResult(new ArrayList<>(), new HashMap<>());
return new FileHierarchyResult(new ArrayList<>(), new HashMap<>(), new HashMap<>());
}
List<FileMetadataInfo> currentFiles = getFileMetadataByIds(fileIdList);
Map<Long, FileMetadataInfo> parentCacheMap = buildParentCacheMap(currentFiles);
return new FileHierarchyResult(currentFiles, parentCacheMap);
Map<Long, String> allParentIdsMap = buildAllParentIdsMap(currentFiles, parentCacheMap);
return new FileHierarchyResult(currentFiles, parentCacheMap, allParentIdsMap);
}
/**
@@ -145,10 +146,12 @@ public class FileMetadataHierarchyHelper {
public static class FileHierarchyResult {
private final List<FileMetadataInfo> files;
private final Map<Long, FileMetadataInfo> parentCacheMap;
private final Map<Long, String> allParentIdsMap;
public FileHierarchyResult(List<FileMetadataInfo> files, Map<Long, FileMetadataInfo> parentCacheMap) {
public FileHierarchyResult(List<FileMetadataInfo> files, Map<Long, FileMetadataInfo> parentCacheMap, Map<Long, String> allParentIdsMap) {
this.files = files;
this.parentCacheMap = parentCacheMap;
this.allParentIdsMap = allParentIdsMap;
}
public List<FileMetadataInfo> getFiles() {
@@ -158,6 +161,10 @@ public class FileMetadataHierarchyHelper {
public Map<Long, FileMetadataInfo> getParentCacheMap() {
return parentCacheMap;
}
public Map<Long, String> getAllParentIdsMap() {
return allParentIdsMap;
}
}
/**
@@ -188,27 +195,27 @@ public class FileMetadataHierarchyHelper {
}
/**
* 批量分层获取所有相关的父目录构建父目录缓存Map
* 批量分层获取所有相关的父目录,构建父目录缓存 Map
*
* @param currentFiles 当前文件列表
* @param maxDepth 最大递归深度
* @return key 是 IDvalue 是对应的元数据实体
* @return key 是 父目录的 IDvalue 是父目录的对应的元数据实体
*/
public Map<Long, FileMetadataInfo> buildParentCacheMap(List<FileMetadataInfo> currentFiles, int maxDepth) {
Map<Long, FileMetadataInfo> parentCacheMap = new HashMap<>();
if (CollectionUtils.isEmpty(currentFiles)) {
return parentCacheMap;
}
// 当前需要去数据库查的父级 ID 集合
Set<Long> nextFetchIds = currentFiles.stream()
.map(FileMetadataInfo::getParentId)
.filter(pid -> pid != null && pid != 0)
.collect(Collectors.toSet());
int safetyDepth = 0; // 防死循环计数器
// 只要还有没查过的父 ID且深度在合理范围内就继续批量查
while (CollectionUtils.isNotEmpty(nextFetchIds) && safetyDepth < maxDepth) {
// 一次性查出当前这一层所有的父节点信息
@@ -216,7 +223,7 @@ public class FileMetadataHierarchyHelper {
if (CollectionUtils.isEmpty(parents)) {
break;
}
nextFetchIds = new HashSet<>(); // 重置,准备收集下一层 ID
for (FileMetadataInfo p : parents) {
parentCacheMap.put(p.getId(), p);
@@ -227,10 +234,63 @@ public class FileMetadataHierarchyHelper {
}
safetyDepth++;
}
return parentCacheMap;
}
/**
* 构建所有父文件 ID 映射关系
*
* @param currentFiles 当前文件列表
* @param parentCacheMap 父目录缓存 Map
* @return key 是文件 IDvalue 是该文件的所有父文件 ID 逗号拼接的结果(从第二层父文件到文件本身,排除最顶层)
*/
public Map<Long, String> buildAllParentIdsMap(List<FileMetadataInfo> currentFiles, Map<Long, FileMetadataInfo> parentCacheMap) {
Map<Long, String> allParentIdsMap = new HashMap<>();
if (CollectionUtils.isEmpty(currentFiles)) {
return allParentIdsMap;
}
for (FileMetadataInfo file : currentFiles) {
Long fileId = file.getId();
if (fileId == null) {
continue;
}
// 使用 LinkedList 方便在头部插入
LinkedList<Long> allIds = new LinkedList<>();
allIds.add(fileId); // 先添加文件本身
Long currentParentId = file.getParentId();
// 递归获取所有父级 ID从直接父文件到顶层父文件
while (currentParentId != null && currentParentId != 0) {
allIds.addFirst(currentParentId); // 在头部插入,保证顶层父文件在最前面
FileMetadataInfo parentFile = parentCacheMap.get(currentParentId);
if (parentFile == null) {
break;
}
currentParentId = parentFile.getParentId();
}
// 移除最顶层的父文件 ID如果有的话
if (allIds.size() > 1) {
allIds.removeFirst(); // 移除第一个元素(最顶层父文件)
}
// 将 ID 列表转换为逗号分隔的字符串
if (!allIds.isEmpty()) { // 只有当还有 ID 时才添加
String allIdsStr = allIds.stream()
.map(String::valueOf)
.collect(Collectors.joining(","));
allParentIdsMap.put(fileId, allIdsStr);
}
}
return allParentIdsMap;
}
/**
* 通过反射设置格式化文件大小
*/

View File

@@ -37,6 +37,11 @@ public class FileSimulationMappingServiceImpl extends ServiceImpl<FileSimulation
@Autowired
IFileMetadataInfoService fileMetadataInfoService;
@Autowired
private FileMetadataHierarchyHelper hierarchyHelper;
@Override
@Transactional(rollbackFor = Exception.class)
public SdmResponse batchSaveFileSimulationMapping(List<SaveFileSimulationMappingReq> saveFileSimulationMappingReqList) {
@@ -103,10 +108,14 @@ public class FileSimulationMappingServiceImpl extends ServiceImpl<FileSimulation
response.setData(map);
return response;
}
FileMetadataHierarchyHelper.FileHierarchyResult hierarchyResult = hierarchyHelper.getFilesWithParentCache(fileMetadataInfos.stream().map(FileMetadataInfo::getId).toList());
Map<Long, String> allParentIdsMap = hierarchyResult.getAllParentIdsMap();
List<FileMetadataInfoResp> fileMetadataInfoResps = new ArrayList<>();
fileMetadataInfos.forEach(fileMetadataInfo -> {
FileMetadataInfoResp fileMetadataInfoResp = new FileMetadataInfoResp();
BeanUtils.copyProperties(fileMetadataInfo, fileMetadataInfoResp);
fileMetadataInfoResp.setAllParentIds(allParentIdsMap.get(fileMetadataInfo.getId()));
fileMetadataInfoResps.add(fileMetadataInfoResp);
});