zhaoqingang
2024-12-26 9191081e44363e4227ea3230edd6fb7619ccb294
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from fastapi import APIRouter, File, UploadFile, Form, BackgroundTasks, Depends
from fastapi.responses import JSONResponse, FileResponse
from starlette.websockets import WebSocket
 
from app.api import get_current_user, get_current_user_websocket
from app.models import UserModel
from app.utils.excelmerge.conformity import run_conformity
import shutil
import os
 
router = APIRouter()
 
ALLOWED_EXTENSIONS = {'xlsx'}
EXCEL_FILES_PATH = 'data/output'
SOURCE_FILES_PATH = 'data/source'
 
 
def allowed_file(filename: str) -> bool:
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
 
 
def create_dir_if_not_exists(path: str):
    if not os.path.exists(path):
        os.makedirs(path)
 
 
def clear_directory(path: str) -> dict:
    for filename in os.listdir(path):
        file_path = os.path.join(path, filename)
        try:
            if os.path.isfile(file_path) or os.path.islink(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)
        except Exception as e:
            return {"error": "清空出错"}
    return {"message": "目录已清空"}
 
 
def user_file_path(userid: str, path: str) -> str:
    return os.path.join(path, userid)
 
 
@router.post('/excel/upload')
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)
    if not user_id:
        return JSONResponse(content={"error": "缺少参数user_id"}, status_code=400)
    user_source = user_file_path(user_id, SOURCE_FILES_PATH)
    user_excel = user_file_path(user_id, 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)
 
 
# 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)):
    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)
    create_dir_if_not_exists(user_source)
    create_dir_if_not_exists(user_excel)
 
    while True:
        data = await websocket.receive_text()
        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}"
                    await websocket.send_json({
                        "message": "文档合并成功!",
                        "type": "stream",
                        "files": [{
                            "file_name": file_name,
                            "file_url": download_url
                        }]
                    })
                    await websocket.send_json({
                        "message": "文档合并成功!",
                        "type": "close",
                    })
                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
                        }]
                    })
            else:
                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/excel")
async def download_file(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)
 
    if not os.path.exists(user_excel):
        return JSONResponse(status_code=404, content={"error": "用户目录不存在"})
 
    excel_files = [f for f in os.listdir(user_excel) if os.path.isfile(os.path.join(user_excel, f))]
    excel_files.sort(key=lambda x: os.path.getmtime(os.path.join(user_excel, x)), reverse=True)
    if not excel_files:
        return JSONResponse(status_code=404, content={"error": "用户目录内没有文件"})
 
    filename = excel_files[0]
    file_path = os.path.join(user_excel, filename)
 
    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')