| | |
| | | from fastapi import APIRouter, File, UploadFile, Form, BackgroundTasks |
| | | 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.post('/excel/upload') |
| | | async def upload_file(files: list[UploadFile] = File(...), user_id: str = Form(...)): |
| | | 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: |
| | |
| | | |
| | | |
| | | # ws://localhost:9201/api/document/ws/excel |
| | | @router.websocket("/ws/excel/{user_id}") |
| | | async def ws_excel(websocket: WebSocket, user_id: str): |
| | | @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) |
| | |
| | | await websocket.close() |
| | | |
| | | |
| | | @router.get("/download/{filename}") |
| | | async def download_file(filename: str, user_id: str, background_tasks: BackgroundTasks): |
| | | @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) |
| | | file_path = os.path.join(user_excel, filename) |
| | | |
| | | if not os.path.exists(file_path): |
| | | return JSONResponse(status_code=404, content={"error": "文件不存在"}) |
| | | 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): |