Skip to content

Commit d7916cf

Browse files
committed
upload_file_to_kb>response.get('data') 防御性处理
1 parent 6a045eb commit d7916cf

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

ragflows/api.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def check_api_url() -> tuple[bool, str]:
3636

3737
response = r.json()
3838
if is_succeed(response):
39-
return True, "API地址配置正确"
39+
timeutils.print_log(f"ragflow version:{response.get('data')}")
40+
return True, f"API地址配置正确"
4041

4142
code = response.get("code")
4243
message = response.get("message")
@@ -139,8 +140,8 @@ def parse_chunks_with_check(filename, doc_id=None):
139140

140141
if not doc_id:
141142
timeutils.print_log(f'根据文件名[{filename}]从数据库获取文档id')
142-
doc_item = ragflowdb.get_doc_item_by_name(filename, max_retries=configs.SQL_RETRIES)
143-
if not doc_item:
143+
doc_item = ragflowdb.get_doc_item_by_name(filename, max_retries=configs.SQL_RETRIES) or {}
144+
if not doc_item.get('id'):
144145
timeutils.print_log(f'找不到{filename}对应的数据库记录,跳过')
145146
return False
146147

@@ -197,8 +198,14 @@ def set_document_metadata(doc_id, filepath) -> bool:
197198
return False
198199

199200
if not doc_id:
200-
timeutils.print_log(F'设置文档元数据失败: doc_id为空,跳过')
201-
return False
201+
filename = os.path.basename(filepath)
202+
timeutils.print_log(f'根据文件名[{filename}]从数据库获取文档id')
203+
doc_item = ragflowdb.get_doc_item_by_name(filename, max_retries=configs.SQL_RETRIES) or {}
204+
if not doc_item.get('id'):
205+
timeutils.print_log(F'设置文档元数据失败: doc_id为空,跳过')
206+
return False
207+
208+
doc_id = doc_item.get('id')
202209

203210
# 构建元数据文件路径-移除原文件后缀再拼接元数据后缀
204211
filepath_without_ext = os.path.splitext(filepath)[0]

ragflows/main.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,16 @@ def main():
159159
timeutils.print_log(f'{file_path} 上传失败:{response.get("text")}')
160160
continue
161161

162-
# 解析doc_id
163-
doc_id = response.get('data')[0].get('id') if response.get('data') else None
164-
162+
# 尝试从响应内容中解析doc_id,防御性处理,有的版本api返回格式为:{'code': 0, 'data': True, 'message': 'success'}
163+
data = response.get('data')
164+
doc_id = None
165+
if isinstance(data, list) and data and isinstance(data[0], dict):
166+
doc_id = data[0].get('id')
167+
elif isinstance(data, dict):
168+
doc_id = data.get('id')
169+
else:
170+
timeutils.print_log(f"上传文件后返回数据不包含文档id: {data}")
171+
165172
# 检查配置并更新元数据
166173
api.set_document_metadata(doc_id, file_path)
167174

scripts/launcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(self):
8181
self.geometry("800x750")
8282

8383
# 版本和仓库信息
84-
self.version = "v1.0.5" # 版本号
84+
self.version = "v1.0.6-alpha" # 版本号
8585
self.github_repo = "https://github.com/Samge0/ragflow-upload" # GitHub仓库地址
8686

8787
# 自定义图标

0 commit comments

Comments
 (0)