zhaoqingang
2025-01-08 966f9e7367684f93349a347c9dbd69c763d3ae7b
app/api/excel.py
@@ -56,45 +56,76 @@
        if file.filename == '':
            return JSONResponse(content={"error": "没有选择文件"}, status_code=400)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            save_path = os.path.join(SOURCE_FILES_PATH, filename)
            save_path = os.path.join(SOURCE_FILES_PATH, 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={"message": "文件上传成功", "paths": save_path_list}, status_code=201)
    return JSONResponse(content={"code": 200, "msg": "", "data": {}}, status_code=200)
# ws://localhost:9201/api/document/ws/excel
@router.websocket("/ws/excel")
async def ws_excel(websocket: WebSocket):
    await websocket.accept()
    create_dir_if_not_exists(SOURCE_FILES_PATH)
    create_dir_if_not_exists(EXCEL_FILES_PATH)
    while True:
        data = await websocket.receive_json()
        action = data.get("action")
        data = await websocket.receive_text()
        try:
            if action == "process":
                clear_directory(EXCEL_FILES_PATH)
            if data == "\"合并Excel\"":
                output_file_path = run_conformity()
                await websocket.send_json({"step_message": "开始合并"})
            elif action == "inquire":
                output_file_path = run_conformity()
                files = os.listdir(EXCEL_FILES_PATH)
                if not files:
                    await websocket.send_json({"step_message": "正在合并中"})
                if files:
                    first_file = files[0]
                    file_name = os.path.basename(first_file)
                    file_url = f"./api/document/download/{first_file}"
                    await websocket.send_json({
                        "message": "文档合并成功!",
                        "type": "stream",
                        "files": [{
                            "file_name": file_name,
                            "file_url": file_url
                        }]
                    })
                    await websocket.send_json({
                        "message": "文档合并成功!",
                        "type": "close",
                    })
                else:
                    await websocket.send_json({"step_message": "文档合并成功!"})
            elif action == "download":
                    await websocket.send_json({"error": "合并操作未生成文件", "type": "stream", "files": []})
            elif data == "\"查询合并进度\"":
                files = os.listdir(EXCEL_FILES_PATH)
                if not files:
                    await websocket.send_json({"error": "目录下没有生成的文件"})
                    await websocket.send_json({"step_message": "正在合并中", "type": "stream", "files": []})
                else:
                    await websocket.send_json({"step_message": "文档合并成功!", "type": "stream", "files": []})
            elif data == "\"获取文件\"":
                files = os.listdir(EXCEL_FILES_PATH)
                if not files:
                    await websocket.send_json({"error": "目录下没有生成的文件", "type": "stream", "files": []})
                else:
                    first_file = files[0]
                    await websocket.send_json({"step_message": "合并文件已生成", "download_url": f"/download/{first_file}"})
                    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
                        }]
                    })
            else:
                await websocket.send_json({"error": "未知指令"})
                print(f"Received data: {data}")
                await websocket.send_json({"error": "未知指令", "data": str(data)})
        except Exception as e:
            await websocket.send_json({"error": str(e)})
            await websocket.close()
@router.get("/download/{filename}")