From f37670f13f8faf018a87d5b73b662bb1909ebe87 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期二, 19 十一月 2024 19:38:03 +0800
Subject: [PATCH] 审计结果文件下载

---
 app/service/basic.py |   29 +++++++++++++++++++----------
 app/api/files.py     |   30 ++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/app/api/files.py b/app/api/files.py
index fe80f4a..968b6b3 100644
--- a/app/api/files.py
+++ b/app/api/files.py
@@ -1,3 +1,4 @@
+import io
 from typing import Optional
 
 import requests
@@ -5,6 +6,7 @@
 from pydantic import BaseModel
 from sqlalchemy.orm import Session
 from starlette.responses import StreamingResponse
+from werkzeug.utils import send_file
 
 from app.api import Response, get_current_user, ResponseList
 from app.config.config import settings
@@ -76,6 +78,8 @@
         agent_id: str = Query(..., description="Agent ID"),
         doc_id: Optional[str] = Query(None, description="Optional doc id for ragflow agents"),
         doc_name: Optional[str] = Query(None, description="Optional doc name for ragflow agents"),
+        file_id:  Optional[str] = Query(None, description="Optional file id for basic agents"),
+        file_type:  Optional[str] = Query(None, description="Optional file type for basic agents"),
         db: Session = Depends(get_db)
 ):
     agent = db.query(AgentModel).filter(AgentModel.id == agent_id).first()
@@ -93,6 +97,10 @@
             return Response(code=400, msg="doc_id is required")
         url = f"{settings.fwr_base_url}/v1/document/get/{doc_id}"
         filename = doc_name
+    elif agent.agent_type == AgentType.BASIC:
+        if agent_id == "basic_excel_talk":
+            return await download_basic_file(file_id, file_type)
+
     else:
         return Response(code=400, msg="Unsupported agent type")
 
@@ -109,3 +117,25 @@
         )
     except Exception as e:
         raise HTTPException(status_code=400, detail=f"Error downloading file: {e}")
+
+
+async def download_basic_file(file_id: str, file_type: str):
+    service = BasicService(base_url=settings.basic_base_url)
+    if not file_type or not file_id:
+        return Response(code=400, msg="file_type and file_id is required")
+    if file_type == "image":
+        content, filename, mimetype = await service.excel_talk_image_download(file_id)
+        return StreamingResponse(
+                io.BytesIO(content),
+                media_type=mimetype,
+                headers={"Content-Disposition": f"attachment; filename={filename}"}
+            )
+    elif file_type == "excel":
+        content, filename, mimetype = await service.excel_talk_excel_download(file_id)
+        return StreamingResponse(
+            io.BytesIO(content),
+            media_type=mimetype,
+            headers={"Content-Disposition": f"attachment; filename={filename}"}
+        )
+    else:
+        return Response(code=400, msg="Unsupported file type")
\ No newline at end of file
diff --git a/app/service/basic.py b/app/service/basic.py
index 30ac727..c64d47a 100644
--- a/app/service/basic.py
+++ b/app/service/basic.py
@@ -15,16 +15,25 @@
             raise Exception(f"Failed to fetch data from API: {response.text}")
         return response_data.get("data", {})
 
-    async def download_from_url(self, url: str, params: dict):
+    async def download_from_url(self, url, params=None):
         async with httpx.AsyncClient() as client:
-            response = await client.get(url, params=params, stream=True)
-            if response.status_code == 200:
-                content_disposition = response.headers.get('Content-Disposition')
-                filename = content_disposition.split('filename=')[-1].strip(
-                    '"') if content_disposition else 'unknown_filename'
-                return response.content, filename, response.headers.get('Content-Type')
-            else:
-                return None, None, None
+            async with client.stream('GET', url, params=params) as response:
+                if response.status_code == 200:
+                    # 鑾峰彇鏂囦欢鍚�
+                    content_disposition = response.headers.get('Content-Disposition')
+                    if content_disposition:
+                        filename = content_disposition.split('filename=')[1].strip('"')
+                    else:
+                        filename = 'unknown_filename'
+
+                    # 鑾峰彇鍐呭绫诲瀷
+                    content_type = response.headers.get('Content-Type')
+
+                    # 璇诲彇鏂囦欢鍐呭
+                    content = await response.aread()
+                    return content, filename, content_type
+                else:
+                    raise Exception(f"Failed to download: {response.status_code}")
 
     async def excel_talk_image_download(self, file_id: str):
         url = f"{self.base_url}/exceltalk/download/image"
@@ -65,4 +74,4 @@
                         print(e)
                         return
                 else:
-                    yield f"Error: {response.status_code}"
+                    yield f"Error: {response.status_code}"
\ No newline at end of file

--
Gitblit v1.8.0