|
|
@@ -135,16 +135,22 @@ async def ws_excel(websocket: WebSocket, current_user: UserModel = Depends(get_c
|
|
|
await websocket.close()
|
|
|
|
|
|
|
|
|
-@router.get("/download/{filename}")
|
|
|
-async def download_file(filename: str, background_tasks: BackgroundTasks,
|
|
|
- current_user: UserModel = Depends(get_current_user)):
|
|
|
+@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):
|