From 5a11a870e2abb5201f62c253ca811e52035864ed Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期五, 10 一月 2025 18:25:13 +0800
Subject: [PATCH] 知识库选择返回自己创建的
---
app/api/__init__.py | 62 +++++++++++++++++++++++++++++-
1 files changed, 59 insertions(+), 3 deletions(-)
diff --git a/app/api/__init__.py b/app/api/__init__.py
index 5ddd5aa..09538a7 100644
--- a/app/api/__init__.py
+++ b/app/api/__init__.py
@@ -1,4 +1,8 @@
+import urllib
+from urllib.parse import urlencode
+
import jwt
+# from cryptography.fernet import Fernet
from fastapi import FastAPI, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer
from passlib.context import CryptContext
@@ -6,19 +10,29 @@
from starlette import status
from starlette.websockets import WebSocket, WebSocketDisconnect
+from Log import logger
+# from app.models.app_model import AppRegisterModel
from app.models.user_model import UserModel
from app.service.auth import SECRET_KEY, ALGORITHM
+from app.config.config import settings
app = FastAPI()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
+# cipher_suite = Fernet(settings.HASH_SUB_KEY)
class Response(BaseModel):
code: int = 200
msg: str = ""
data: dict = {}
+
+
+class ResponseList(BaseModel):
+ code: int = 200
+ msg: str = ""
+ data: list[dict] = []
def get_current_user(token: str = Depends(oauth2_scheme)):
@@ -48,11 +62,10 @@
async def get_current_user_websocket(websocket: WebSocket):
- auth_header = websocket.headers.get('Authorization')
- if auth_header is None or not auth_header.startswith('Bearer '):
+ token = websocket.query_params.get('token')
+ if token is None:
await websocket.close(code=1008)
raise WebSocketDisconnect(code=status.WS_1008_POLICY_VIOLATION)
- token = auth_header[len('Bearer '):]
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username: str = payload.get("sub")
@@ -68,3 +81,46 @@
print(e)
await websocket.close(code=1008)
raise WebSocketDisconnect(code=status.WS_1008_POLICY_VIOLATION)
+
+
+def format_file_url(agent_id: str, file_url: str, doc_id: str = None, doc_name: str = None) -> str:
+ if file_url:
+ # 瀵� file_url 杩涜 URL 缂栫爜
+ encoded_file_url = urllib.parse.quote(file_url, safe=':/')
+ return f"./api/files/download/?url={encoded_file_url}&agent_id={agent_id}"
+
+ if doc_id:
+ # 瀵� doc_id 鍜� doc_name 杩涜 URL 缂栫爜
+ encoded_doc_id = urllib.parse.quote(doc_id, safe='')
+ encoded_doc_name = urllib.parse.quote(doc_name, safe='')
+ return f"./api/files/download/?doc_id={encoded_doc_id}&doc_name={encoded_doc_name}&agent_id={agent_id}"
+
+ return file_url
+
+
+def process_files(files, agent_id):
+ """
+ 澶勭悊鏂囦欢鍒楄〃锛屾牸寮忓寲姣忎釜鏂囦欢鐨� URL銆�
+
+ :param files: 鏂囦欢鍒楄〃锛屾瘡涓枃浠舵槸涓�涓瓧鍏�
+ :param agent_id: 浠g悊 ID
+ """
+ if not files:
+ return # 濡傛灉鏂囦欢鍒楄〃涓虹┖锛岀洿鎺ヨ繑鍥�
+
+ for file in files:
+ if "file_url" in file and file["file_url"]:
+ try:
+ file["file_url"] = format_file_url(agent_id, file["file_url"])
+ except Exception as e:
+ # 璁板綍寮傚父淇℃伅锛屼絾缁х画澶勭悊鍏朵粬鏂囦欢
+ print(f"Error processing file URL: {e}")
+
+
+if __name__=="__main__":
+
+ files1 = [{"file_url": "aaa.com"}, {"file_url":"bbb.com"}]
+ print(files1)
+
+ process_files(files1,11111)
+ print(files1)
\ No newline at end of file
--
Gitblit v1.8.0