|
|
@@ -2,10 +2,12 @@ from fastapi import APIRouter, HTTPException, Request, Depends
|
|
|
from fastapi.responses import JSONResponse, StreamingResponse
|
|
|
import httpx
|
|
|
import os
|
|
|
+from pathlib import Path
|
|
|
from typing import Dict
|
|
|
-
|
|
|
from pydantic import BaseModel
|
|
|
from typing import Optional
|
|
|
+import pathlib
|
|
|
+import os
|
|
|
from utils.config import WORKER_SERVICE_URL,config,Browser
|
|
|
from utils.logu import logger
|
|
|
|
|
|
@@ -16,6 +18,18 @@ class Endpoint(BaseModel):
|
|
|
token: Optional[str] = "temp_token_123"
|
|
|
health: Optional[Dict] = {}
|
|
|
|
|
|
+class BrowserPathValidationRequest(BaseModel):
|
|
|
+ exe_path: str
|
|
|
+
|
|
|
+class PathValidationRequest(BaseModel):
|
|
|
+ file_path: str
|
|
|
+
|
|
|
+class PathValidationResponse(BaseModel):
|
|
|
+ valid: bool
|
|
|
+ message: str
|
|
|
+ file_name: Optional[str] = None
|
|
|
+ file_size: Optional[int] = None
|
|
|
+
|
|
|
class ResponseStatus(BaseModel):
|
|
|
endpoint: Endpoint = Endpoint()
|
|
|
browser_config: Optional[Browser] = None
|
|
|
@@ -45,6 +59,90 @@ async def status():
|
|
|
"""获取当前请求的端点"""
|
|
|
return ResponseStatus(endpoint=Endpoint(health=health), browser_config=config.browser)
|
|
|
|
|
|
+@router.post("/validate_browser_path", tags=["worker"])
|
|
|
+async def validate_browser_path(request: BrowserPathValidationRequest):
|
|
|
+ """验证浏览器路径是否有效"""
|
|
|
+ path = Path(request.exe_path)
|
|
|
+ try:
|
|
|
+ if not path.exists():
|
|
|
+ return JSONResponse(status_code=400, content={
|
|
|
+ "valid": False,
|
|
|
+ "message": "路径不存在"
|
|
|
+ })
|
|
|
+
|
|
|
+ if not path.is_file():
|
|
|
+ return JSONResponse(status_code=400, content={
|
|
|
+ "valid": False,
|
|
|
+ "message": "路径不是文件"
|
|
|
+ })
|
|
|
+
|
|
|
+ if not os.access(path, os.X_OK):
|
|
|
+ return JSONResponse(status_code=400, content={
|
|
|
+ "valid": False,
|
|
|
+ "message": "文件不可执行"
|
|
|
+ })
|
|
|
+
|
|
|
+ if path.suffix.lower() != '.exe':
|
|
|
+ return JSONResponse(status_code=400, content={
|
|
|
+ "valid": False,
|
|
|
+ "message": "仅支持可执行文件 (.exe)"
|
|
|
+ })
|
|
|
+
|
|
|
+ return {
|
|
|
+ "valid": True,
|
|
|
+ "message": "浏览器路径有效"
|
|
|
+ }
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"浏览器路径验证失败: {str(e)}")
|
|
|
+ return JSONResponse(status_code=500, content={
|
|
|
+ "valid": False,
|
|
|
+ "message": f"路径验证异常: {str(e)}"
|
|
|
+ })
|
|
|
+
|
|
|
+@router.post("/validate_path", tags=["worker"])
|
|
|
+async def validate_file_path(request: PathValidationRequest):
|
|
|
+ """验证文件路径是否有效"""
|
|
|
+ path = Path(request.file_path)
|
|
|
+ try:
|
|
|
+ if not path.exists():
|
|
|
+ return JSONResponse(status_code=400, content={
|
|
|
+ "valid": False,
|
|
|
+ "message": "路径不存在"
|
|
|
+ })
|
|
|
+
|
|
|
+ if not path.is_file():
|
|
|
+ return JSONResponse(status_code=400, content={
|
|
|
+ "valid": False,
|
|
|
+ "message": "路径不是文件"
|
|
|
+ })
|
|
|
+
|
|
|
+ if not os.access(path, os.R_OK):
|
|
|
+ return JSONResponse(status_code=400, content={
|
|
|
+ "valid": False,
|
|
|
+ "message": "文件不可读"
|
|
|
+ })
|
|
|
+
|
|
|
+ if path.suffix.lower() not in ('.xlsx', '.xls'):
|
|
|
+ return JSONResponse(status_code=400, content={
|
|
|
+ "valid": False,
|
|
|
+ "message": "仅支持Excel文件 (.xlsx/.xls)"
|
|
|
+ })
|
|
|
+
|
|
|
+ return {
|
|
|
+ "valid": True,
|
|
|
+ "message": "文件有效",
|
|
|
+ "file_name": path.name,
|
|
|
+ "file_size": path.stat().st_size
|
|
|
+ }
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"路径验证失败: {str(e)}")
|
|
|
+ return JSONResponse(status_code=500, content={
|
|
|
+ "valid": False,
|
|
|
+ "message": f"路径验证异常: {str(e)}"
|
|
|
+ })
|
|
|
+
|
|
|
async def health_check():
|
|
|
"""健康检查"""
|
|
|
async with httpx.AsyncClient(base_url=WORKER_SERVICE_URL) as client:
|