From 32197f248ac761a74e9a2384d008c184330b1bfc Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期三, 25 十二月 2024 18:10:53 +0800 Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/rag-gateway --- app/service/user.py | 5 +- app/api/excel.py | 87 ++++++++++++++++++++++++++++++------------- app/utils/excelmerge/conformity.py | 12 ++---- 3 files changed, 67 insertions(+), 37 deletions(-) diff --git a/app/api/excel.py b/app/api/excel.py index a21ee3f..0622cb0 100644 --- a/app/api/excel.py +++ b/app/api/excel.py @@ -1,7 +1,9 @@ -from fastapi import APIRouter, File, UploadFile +from fastapi import APIRouter, File, UploadFile, Form, BackgroundTasks, Depends from fastapi.responses import JSONResponse, FileResponse -from fastapi.exceptions import HTTPException 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 @@ -13,17 +15,16 @@ SOURCE_FILES_PATH = 'data/source' -def allowed_file(filename): +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): +def create_dir_if_not_exists(path: str): if not os.path.exists(path): os.makedirs(path) -# 娓呯悊鍑芥暟 -def clear_directory(path): +def clear_directory(path: str) -> dict: for filename in os.listdir(path): file_path = os.path.join(path, filename) try: @@ -36,22 +37,31 @@ 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(...)): +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(SOURCE_FILES_PATH) - create_dir_if_not_exists(EXCEL_FILES_PATH) - clear_directory(SOURCE_FILES_PATH) - clear_directory(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(SOURCE_FILES_PATH, 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) @@ -62,18 +72,21 @@ # ws://localhost:9201/api/document/ws/excel @router.websocket("/ws/excel") -async def ws_excel(websocket: WebSocket): +async def ws_excel(websocket: WebSocket, current_user: UserModel = Depends(get_current_user_websocket)): await websocket.accept() + user_id = str(current_user.id) - create_dir_if_not_exists(SOURCE_FILES_PATH) - create_dir_if_not_exists(EXCEL_FILES_PATH) + 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() - files = os.listdir(EXCEL_FILES_PATH) + 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) @@ -93,13 +106,13 @@ else: await websocket.send_json({"error": "鍚堝苟澶辫触", "type": "stream", "files": []}) elif data == "\"鏌ヨ鍚堝苟杩涘害\"": - files = os.listdir(EXCEL_FILES_PATH) + files = os.listdir(user_excel) if not files: await websocket.send_json({"step_message": "姝e湪鍚堝苟涓�", "type": "stream", "files": []}) else: await websocket.send_json({"step_message": "鏂囨。鍚堝苟鎴愬姛锛�", "type": "stream", "files": []}) elif data == "\"鑾峰彇鏂囦欢\"": - files = os.listdir(EXCEL_FILES_PATH) + files = os.listdir(user_excel) if not files: await websocket.send_json({"error": "鐩綍涓嬫病鏈夌敓鎴愮殑鏂囦欢", "type": "stream", "files": []}) else: @@ -123,11 +136,31 @@ @router.get("/download/{filename}") -async def download_file(filename: str): - try: - return FileResponse(os.path.join(EXCEL_FILES_PATH, filename), filename=filename, - media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') - except FileNotFoundError: - raise HTTPException(status_code=404, detail="鏂囦欢涓嶅瓨鍦�") - except Exception as e: - raise HTTPException(status_code=500, detail="鏈嶅姟鍣ㄩ敊璇�") +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) + + if not os.path.exists(file_path): + return JSONResponse(status_code=404, content={"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') diff --git a/app/service/user.py b/app/service/user.py index 5b8e013..fbc4119 100644 --- a/app/service/user.py +++ b/app/service/user.py @@ -60,7 +60,7 @@ app_register = AppRegisterDao(db).get_apps() register_dict = {} token = "" - app_password = generate_password() + app_password = await generate_password() for app in app_register: if app["id"] == RAGFLOW: service = RagflowService(settings.fwr_base_url) @@ -98,8 +98,9 @@ db.commit() db.refresh(user_model) u_id = user_model.id + user_app_dao = UserAppDao(db) for k, v in register_dict.items(): - await UserAppDao(db).update_and_insert_data(v.get("name"), pwd, v.get("email"), u_id, str(v.get("id")), k) + await user_app_dao.update_and_insert_data(v.get("name"), pwd, v.get("email"), u_id, str(v.get("id")), k) except Exception as e: logger.error(e) # db.rollback() diff --git a/app/utils/excelmerge/conformity.py b/app/utils/excelmerge/conformity.py index 6a609b3..bca8868 100644 --- a/app/utils/excelmerge/conformity.py +++ b/app/utils/excelmerge/conformity.py @@ -19,20 +19,16 @@ [source_sheet.cell(row=row, column=col).value for col in range(1, source_sheet.max_column + 1)]) -def run_conformity(): +def run_conformity(file_path, print_path): try: # 鍔犺浇妯℃澘鏂囦欢 template_path = os.path.join('app', 'utils', 'excelmerge', '鍥界綉涓婃捣鐢靛姏鏁村悎妯$増.xlsx') template_excel = load_workbook(template_path) - EXCEL_FILES_PATH = os.path.join('data', 'output') - template_sheets = {sheet.title: sheet for sheet in template_excel} - - source_folder = os.path.join('data', 'source') - source_files = [f for f in os.listdir(source_folder) if f.endswith('.xlsx') and not f.startswith('~$')] + source_files = [f for f in os.listdir(file_path) if f.endswith('.xlsx') and not f.startswith('~$')] for file in source_files: - source_path = os.path.join(source_folder, file) + source_path = os.path.join(file_path, file) source_excel = load_workbook(source_path) # 鍔ㄦ�佽幏鍙栧伐浣滆〃 @@ -59,7 +55,7 @@ template_sheets[name].cell(row=i, column=1).value = i - start_row + 1 timestamp = datetime.now().strftime('%Y_%m_%d_%H_%M_%S') - output_path = os.path.join(EXCEL_FILES_PATH, f'{timestamp}.xlsx') + output_path = os.path.join(print_path, f'{timestamp}.xlsx') template_excel.save(output_path) template_excel.close() -- Gitblit v1.8.0