zhaoqingang
2025-04-10 abb91124a4372b0efe5ab1b7aa25859c635d30eb
app/api/excel.py
@@ -1,9 +1,15 @@
from fastapi import APIRouter, File, UploadFile, Form, BackgroundTasks, Depends
from fastapi.responses import JSONResponse, FileResponse
from starlette.websockets import WebSocket
import random
import string
from app.api import get_current_user, get_current_user_websocket
from app.models import UserModel
from fastapi import APIRouter, File, UploadFile, Form, BackgroundTasks, Depends, Request, WebSocket
from fastapi.responses import JSONResponse, FileResponse
from sqlalchemy.orm import Session
# from starlette.websockets import WebSocket
from app.api import get_current_user, get_current_user_websocket, Response
from app.models import UserModel, AgentType
from app.models.base_model import get_db
from app.service.session import SessionService
from app.utils.excelmerge.conformity import run_conformity
import shutil
import os
@@ -41,126 +47,132 @@
    return os.path.join(path, userid)
@router.post('/excel/upload')
def generate_db_id(prefix: str = "me") -> str:
    random_part = ''.join(random.choices(string.ascii_letters + string.digits, k=13))
    return prefix + random_part
def db_create_session(db: Session, user_id: str, message:str, upload_filenames: list):
    db_id = generate_db_id()
    session = SessionService(db).create_session(
        db_id,
        message,
        "basic_excel_merge",
        AgentType.BASIC,
        int(user_id),
        {"role": "user", "content": message, "upload_filenames": upload_filenames}
    )
    return session
@router.post('/excel/upload', response_model=Response)
async def upload_file(files: list[UploadFile] = File(...), current_user: UserModel = Depends(get_current_user)):
    user_id = str(current_user.id)
    if not any(file.filename for file in files):
        return JSONResponse(content={"error": "没有文件部分"}, status_code=400)
        return Response(code=400, msg="没有文件部分", data={})
    if not user_id:
        return JSONResponse(content={"error": "缺少参数user_id"}, status_code=400)
        return Response(code=400, msg="缺少参数user_id", data={})
    user_source = user_file_path(user_id, SOURCE_FILES_PATH)
    user_excel = user_file_path(user_id, EXCEL_FILES_PATH)
    user_excel = EXCEL_FILES_PATH
    create_dir_if_not_exists(user_source)
    create_dir_if_not_exists(user_excel)
    clear_directory(user_source)
    clear_directory(user_excel)
    save_path_list = []
    for file in files:
        if file.filename == '':
            return JSONResponse(content={"error": "没有选择文件"}, status_code=400)
        if file and allowed_file(file.filename):
            save_path = os.path.join(user_source, file.filename)
            with open(save_path, 'wb') as buffer:
                shutil.copyfileobj(file.file, buffer)
            save_path_list.append(save_path)
        else:
            return JSONResponse(content={"error": "不允许的文件类型"}, status_code=400)
    return JSONResponse(content={"code": 200, "msg": "", "data": {}}, status_code=200)
            return Response(code=400, msg="不允许的文件类型", data={})
    return Response(code=200, msg="上传成功", data={})
# ws://localhost:9201/api/document/ws/excel
@router.websocket("/ws/excel")
async def ws_excel(websocket: WebSocket, current_user: UserModel = Depends(get_current_user_websocket)):
async def ws_excel(websocket: WebSocket,
                   current_user: UserModel = Depends(get_current_user_websocket),
                   db: Session = Depends(get_db)):
    await websocket.accept()
    user_id = str(current_user.id)
    user_source = user_file_path(user_id, SOURCE_FILES_PATH)
    user_excel = user_file_path(user_id, EXCEL_FILES_PATH)
    user_excel = EXCEL_FILES_PATH
    create_dir_if_not_exists(user_source)
    create_dir_if_not_exists(user_excel)
    while True:
        data = await websocket.receive_text()
        # data = await websocket.receive_text()git
        receive_message = await websocket.receive_json()
        try:
            if data == "\"合并Excel\"":
                run_excel = run_conformity(user_source, user_excel)
                files = os.listdir(user_excel)
                if run_excel:
                    first_file = files[0]
                    file_name = os.path.basename(first_file)
                    download_url = f"./api/document/download/{first_file}"
            if receive_message.get("message") == "合并Excel":
                upload_filenames = receive_message.get('upload_filenames', [])
                merge_file = run_conformity(user_source, user_excel)
                if merge_file is not None:
                    await websocket.send_json({
                        "message": "文档合并成功!",
                        "type": "stream",
                        "files": [{
                            "file_name": file_name,
                            "file_url": download_url
                        }]
                        "files": [
                            {
                                "file_name": "Excel",
                                "file_url": f"./api/document/download/{merge_file}.xlsx?file_type=excel",
                            }
                        ]
                    })
                    await websocket.send_json({
                        "message": "文档合并成功!",
                        "message": "合并成功",
                        "type": "close",
                    })
                    # 创建会话记录
                    session = db_create_session(db, user_id, receive_message.get("message"), upload_filenames)
                    # 更新会话记录
                    if session:
                        session_id = session.id
                        new_message = {
                            "role": "assistant",
                            "content": {
                                "message": "\u5408\u5e76\u6210\u529f",
                                "type": "message",
                                "file_name": "Excel",
                                "file_url": f"/api/document/download/{merge_file}.xlsx?file_type=excel"
                            }
                        }
                        session_service = SessionService(db)
                        session_service.update_session(session_id, message=new_message)
                else:
                    await websocket.send_json({"error": "合并失败", "type": "stream", "files": []})
            elif data == "\"查询合并进度\"":
                files = os.listdir(user_excel)
                if not files:
                    await websocket.send_json({"step_message": "正在合并中", "type": "stream", "files": []})
                else:
                    await websocket.send_json({"step_message": "文档合并成功!", "type": "stream", "files": []})
            elif data == "\"获取文件\"":
                files = os.listdir(user_excel)
                if not files:
                    await websocket.send_json({"error": "目录下没有生成的文件", "type": "stream", "files": []})
                else:
                    first_file = files[0]
                    file_name = os.path.basename(first_file)
                    file_url = f"./api/document/download/{first_file}"
                    await websocket.send_json({
                        "step_message": "文档合并成功!",
                        "type": "stream",
                        "files": [{
                            "file_name": file_name,
                            "file_url": file_url
                        }]
                    })
                    await websocket.close()
            else:
                print(f"Received data: {data}")
                await websocket.send_json({"error": "未知指令", "data": str(data)})
                print(f"Received data: {receive_message.get('message')}")
                await websocket.send_json({"error": "未知指令", "data": str(receive_message.get('message'))})
                await websocket.close()
        except Exception as e:
            await websocket.send_json({"error": str(e)})
            await websocket.close()
@router.get("/download/{filename}")
async def download_file(filename: str, background_tasks: BackgroundTasks,
                        current_user: UserModel = Depends(get_current_user)):
    user_id = str(current_user.id)
    user_excel = user_file_path(user_id, EXCEL_FILES_PATH)
    user_source = user_file_path(user_id, SOURCE_FILES_PATH)
    file_path = os.path.join(user_excel, filename)
@router.get("/download/{file_full_name}")
async def download_file(file_full_name: str):
    file_name = os.path.basename(file_full_name)
    user_excel = EXCEL_FILES_PATH
    file_path = os.path.join(user_excel, file_full_name)
    if not os.path.exists(file_path):
        return JSONResponse(status_code=404, content={"error": "文件不存在"})
        return JSONResponse(content={"error": "文件不存在"}, status_code=404)
    return FileResponse(
        path=file_path,
        filename="Excel.xlsx",
        media_type='application/octet-stream',
    )
    # def delete_file():
    #     try:
    #         os.unlink(file_path)
    #     except OSError as e:
    #         print(f"Deleting file error")
    def delete_files_in_directory(directory):
        for root, dirs, files in os.walk(directory, topdown=False):
            for name in files:
                os.remove(os.path.join(root, name))
            for name in dirs:
                os.rmdir(os.path.join(root, name))
    def delete_file():
        try:
            delete_files_in_directory(user_excel)
            delete_files_in_directory(user_source)
        except OSError as e:
            print(f"Error deleting file {file_path}: {e}")
    background_tasks.add_task(delete_file)
    return FileResponse(file_path, filename=filename,
                        media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    # 待下载完成后删除生成的文件
    # background_tasks.add_task(delete_file)
    # return FileResponse(path=file_path, filename=file_name,
    #                    media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")