原始文件气泡映射错误 + 多版本下载错版本

This commit is contained in:
runny committed 2026-06-02 21:07:04 +08:00
1 parent f877f61efd
commit 854bc8e4db
7 files changed
+203 -80

No files matched your search

+5 -4
View File
@@ -867,22 +867,23 @@ class DifyService:
remote_document_id = (
resource.get('remote_document_id')
or resource.get('dify_document_id')
or resource.get('document_id')
or resource.get('source_document_id')
or resource.get('doc_id')
or metadata.get('remote_document_id')
or metadata.get('dify_document_id')
or metadata.get('document_id')
or metadata.get('source_document_id')
or metadata.get('doc_id')
or DifyService._find_first_by_keys(
resource,
[
'remote_document_id',
'dify_document_id',
'document_id',
'doc_id',
'source_document_id',
'doc_id',
],
)
or resource.get('document_id')
or metadata.get('document_id')
)
return dataset_id, remote_document_id
+15 -5
View File
@@ -203,13 +203,19 @@ class IntegrationApiService:
from app import db
from app.models import KbDifySyncRecord, KbDocumentVersion, KbDocument
normalized_dataset_id = str(dataset_id or '').strip()
normalized_remote_document_id = str(remote_document_id or '').strip()
if not normalized_dataset_id or not normalized_remote_document_id:
return None
record = (
db.session.query(KbDifySyncRecord)
.join(KbDocumentVersion, KbDocumentVersion.id == KbDifySyncRecord.document_version_id)
.join(KbDocument, KbDocument.id == KbDocumentVersion.document_id)
.filter(
KbDocument.is_deleted.is_(False),
KbDifySyncRecord.remote_document_id == remote_document_id,
KbDifySyncRecord.dataset_id == normalized_dataset_id,
KbDifySyncRecord.remote_document_id == normalized_remote_document_id,
)
.order_by(KbDifySyncRecord.updated_at.desc(), KbDifySyncRecord.id.desc())
.first()
@@ -236,13 +242,17 @@ class IntegrationApiService:
secret = str(current_app.config.get('SECRET_KEY') or 'default-secret').strip()
exp = int(time.time()) + 86400 * 7 # 7 days expiration for local tokens
payload = {
'dataset_id': 'local',
'remote_document_id': remote_document_id,
'dataset_id': normalized_dataset_id,
'remote_document_id': normalized_remote_document_id,
'version_id': version.id,
'exp': exp
}
token = jwt.encode(payload, secret, algorithm='HS256')
source_download_url = f'{base_url}/api/integration/files/{document.id}/download?kind=source&token={token}'
source_download_url = (
f'{base_url}/api/integration/files/{document.id}/download'
f'?kind=source&version_id={version.id}&token={token}'
)
return {
"source_download_url": source_download_url,
@@ -266,7 +276,7 @@ class IntegrationApiService:
base_url = IntegrationApiService.get_base_url()
api_key = IntegrationApiService.get_api_key()
if not base_url or not api_key:
return None
return IntegrationApiService._lookup_local_db(dataset_id, remote_document_id)
url = f"{base_url}/api/integration/dify-file"
headers = {"X-Integration-Key": api_key}
@@ -177,6 +177,7 @@ def sync_document_version_to_dify(document_version_id: int, synced_by_id: int |
remote_document_id = None
remote_document_name = None
upload_name = version.source_file_name or document.title
if existing_record and existing_record.dataset_id == dataset_id and existing_record.sync_hash == sync_hash:
try:
payload = service.get_document(
@@ -184,23 +185,23 @@ def sync_document_version_to_dify(document_version_id: int, synced_by_id: int |
document_id=existing_record.remote_document_id,
)
remote_document_id = payload.get('id') or existing_record.remote_document_id
remote_document_name = payload.get('name') or existing_record.remote_document_name or document.title
remote_document_name = payload.get('name') or existing_record.remote_document_name or upload_name
except KbDifySyncError as exc:
if not _is_remote_document_missing(str(exc)):
raise
payload = service.create_document_by_text(
dataset_id=dataset_id,
name=document.title,
name=upload_name,
text=text,
)
remote_document_id = payload.get('document', {}).get('id') or payload.get('id')
remote_document_name = payload.get('document', {}).get('name') or document.title
remote_document_name = payload.get('document', {}).get('name') or upload_name
elif existing_record and existing_record.dataset_id == dataset_id:
try:
payload = service.update_document_by_text(
dataset_id=dataset_id,
document_id=existing_record.remote_document_id,
name=document.title,
name=upload_name,
text=text,
)
except KbDifySyncError as exc:
@@ -208,19 +209,19 @@ def sync_document_version_to_dify(document_version_id: int, synced_by_id: int |
raise
payload = service.create_document_by_text(
dataset_id=dataset_id,
name=document.title,
name=upload_name,
text=text,
)
remote_document_id = payload.get('document', {}).get('id') or payload.get('id') or existing_record.remote_document_id
remote_document_name = payload.get('document', {}).get('name') or document.title
remote_document_name = payload.get('document', {}).get('name') or upload_name
else:
payload = service.create_document_by_text(
dataset_id=dataset_id,
name=document.title,
name=upload_name,
text=text,
)
remote_document_id = payload.get('document', {}).get('id') or payload.get('id')
remote_document_name = payload.get('document', {}).get('name') or document.title
remote_document_name = payload.get('document', {}).get('name') or upload_name
if not remote_document_id:
raise KbDifySyncError('Dify 未返回远端文档 ID')