fix _path_to_id

This commit is contained in:
jxxghp
2025-03-26 08:54:51 +08:00
parent 43fe8f25f8
commit 2020244cae
+23 -12
View File
@@ -236,9 +236,12 @@ class U115Pan(StorageBase, metaclass=Singleton):
""" """
路径转FID(带缓存机制) 路径转FID(带缓存机制)
""" """
# 根目录
if path == "/":
return 0
if len(path) > 1 and path.endswith("/"): if len(path) > 1 and path.endswith("/"):
path = path[:-1] path = path[:-1]
# 命中缓存 # 检查缓存
if path in self._id_cache: if path in self._id_cache:
return self._id_cache[path] return self._id_cache[path]
# 逐级查找缓存 # 逐级查找缓存
@@ -252,18 +255,26 @@ class U115Pan(StorageBase, metaclass=Singleton):
# 计算相对路径 # 计算相对路径
rel_path = Path(path).relative_to(parent_path) rel_path = Path(path).relative_to(parent_path)
for part in Path(rel_path).parts: for part in Path(rel_path).parts:
resp = self._request_api( offset = 0
"GET", while True:
"/open/ufile/files", resp = self._request_api(
"data", "GET",
params={"cid": current_id, "limit": 1000, "cur": True, "show_dir": 1} "/open/ufile/files",
) "data",
for item in resp: params={"cid": current_id, "limit": 1000, "offset": offset, "cur": True, "show_dir": 1}
if item["fn"] == part: )
current_id = item["fid"] if not resp:
break break
else: for item in resp:
raise FileNotFoundError(f"【115】路径不存在: {path}") if item["fn"] == part:
current_id = item["fid"]
break
if len(resp) < 1000:
break
offset += len(resp)
if not current_id:
raise FileNotFoundError(f"【115】{path} 不存在")
# 缓存路径
self._id_cache[path] = current_id self._id_cache[path] = current_id
return current_id return current_id