文件预览报500问题优化

This commit is contained in:
runny committed 2026-06-03 14:46:37 +08:00
1 parent 55ce8dd949
commit 8206630823
2 files changed
+38 -7

No files matched your search

+4
View File
@@ -10,6 +10,7 @@ from sqlalchemy.orm import joinedload
from app import db from app import db
from app.models import KbDifySyncRecord, KbDocument, KbDocumentVersion, KbGroup from app.models import KbDifySyncRecord, KbDocument, KbDocumentVersion, KbGroup
from app.errors import AppError, ValidationError, UnauthorizedError, ForbiddenError, NotFoundError, ConflictError, ServiceUnavailableError, raise_for_status from app.errors import AppError, ValidationError, UnauthorizedError, ForbiddenError, NotFoundError, ConflictError, ServiceUnavailableError, raise_for_status
from app.services.integration_api import IntegrationApiService
integration_bp = Blueprint('integration', __name__) integration_bp = Blueprint('integration', __name__)
@@ -120,6 +121,9 @@ def _require_integration_key():
def _integration_base_url() -> str: def _integration_base_url() -> str:
request_origin = IntegrationApiService._request_origin_url()
if request_origin:
return request_origin
explicit = str(current_app.config.get('WECOM_PUBLIC_BASE_URL') or '').strip().rstrip('/') explicit = str(current_app.config.get('WECOM_PUBLIC_BASE_URL') or '').strip().rstrip('/')
if explicit: if explicit:
return explicit return explicit
+34 -7
View File
@@ -14,6 +14,28 @@ from app.config import Config
class IntegrationApiService: class IntegrationApiService:
_cache: Dict[Tuple[str, str], Dict[str, Any]] = {} _cache: Dict[Tuple[str, str], Dict[str, Any]] = {}
@staticmethod
def _request_origin_url() -> str:
if not has_request_context():
return ""
forwarded_proto = (request.headers.get("X-Forwarded-Proto") or "").split(",")[0].strip()
forwarded_host = (request.headers.get("X-Forwarded-Host") or "").split(",")[0].strip()
if forwarded_proto and forwarded_host:
return f"{forwarded_proto}://{forwarded_host}".rstrip("/")
origin = str(request.headers.get("Origin") or "").strip().rstrip("/")
if origin:
return origin
referer = str(request.headers.get("Referer") or "").strip()
if referer:
parsed = urlparse(referer)
if parsed.scheme and parsed.netloc:
return f"{parsed.scheme}://{parsed.netloc}".rstrip("/")
return request.host_url.rstrip("/")
@staticmethod @staticmethod
def _lookup_from_dify_document_api(dataset_id: str, remote_document_id: str) -> Optional[Dict[str, Any]]: def _lookup_from_dify_document_api(dataset_id: str, remote_document_id: str) -> Optional[Dict[str, Any]]:
""" """
@@ -152,6 +174,9 @@ class IntegrationApiService:
return configured return configured
if mode != "external": if mode != "external":
request_origin = IntegrationApiService._request_origin_url()
if request_origin:
return request_origin
if public_base: if public_base:
return public_base return public_base
if has_request_context(): if has_request_context():
@@ -226,13 +251,15 @@ class IntegrationApiService:
version = record.document_version version = record.document_version
document = version.document document = version.document
explicit = str(current_app.config.get('WECOM_PUBLIC_BASE_URL') or '').strip().rstrip('/') base_url = IntegrationApiService._request_origin_url()
if explicit: if not base_url:
base_url = explicit explicit = str(current_app.config.get('WECOM_PUBLIC_BASE_URL') or '').strip().rstrip('/')
elif has_request_context(): if explicit:
base_url = request.host_url.rstrip('/') base_url = explicit
else: elif has_request_context():
base_url = '' base_url = request.host_url.rstrip('/')
else:
base_url = ''
import time import time
import jwt import jwt