Skip to content

Commit

Permalink
fix #125
Browse files Browse the repository at this point in the history
  • Loading branch information
xaoyaoo committed Aug 21, 2024
1 parent cf92003 commit d379e4e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pywxdump/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Author: xaoyaoo
# Date: 2023/10/14
# -------------------------------------------------------------------------------
__version__ = "3.1.27"
__version__ = "3.1.28"

import os, json

Expand Down
32 changes: 28 additions & 4 deletions pywxdump/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import sys
import time
import uvicorn
import mimetypes

from fastapi import FastAPI, Request, Path, Query
from fastapi.staticfiles import StaticFiles
from fastapi.exceptions import RequestValidationError
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import RedirectResponse
from starlette.responses import RedirectResponse, FileResponse

from .utils import gc, is_port_in_use, server_loger
from .rjson import ReJson
Expand All @@ -32,6 +33,8 @@ def gen_fastapi_app():
license_info={"name": "MIT License",
"url": "https://github.com/xaoyaoo/PyWxDump/blob/master/LICENSE"})

web_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "ui", "web") # web文件夹路径

# 跨域
origins = [
"http://localhost:5000",
Expand All @@ -48,11 +51,13 @@ def gen_fastapi_app():
allow_headers=["*"], # 允许所有头
)

# 错误处理
@app.exception_handler(RequestValidationError)
async def request_validation_exception_handler(request: Request, exc: RequestValidationError):
# print(request.body)
return ReJson(1002, {"detail": exc.errors()})

# 首页
@app.get("/")
@app.get("/index.html")
async def index():
Expand All @@ -63,10 +68,29 @@ async def index():
app.include_router(rs_api, prefix='/api/rs', tags=['远程api'])
app.include_router(ls_api, prefix='/api/ls', tags=['本地api'])

# 根据文件类型,设置mime_type,返回文件
@app.get("/s/{filename:path}")
async def serve_file(filename: str):
# 构建完整的文件路径
file_path = os.path.join(web_path, filename)

# 检查文件是否存在
if os.path.isfile(file_path):
# 获取文件 MIME 类型
mime_type, _ = mimetypes.guess_type(file_path)
# 如果 MIME 类型为空,则默认为 application/octet-stream
if mime_type is None:
mime_type = "application/octet-stream"

# 返回文件
return FileResponse(file_path, media_type=mime_type)

# 如果文件不存在,返回 404
return {"detail": "Not Found"}, 404

# 静态文件挂载
web_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "ui", "web")
if os.path.exists(os.path.join(web_path, "index.html")):
app.mount("/s", StaticFiles(directory=web_path), name="static")
# if os.path.exists(os.path.join(web_path, "index.html")):
# app.mount("/s", StaticFiles(directory=web_path), name="static")

return app

Expand Down

0 comments on commit d379e4e

Please sign in to comment.