Skip to content

Commit d379e4e

Browse files
committed
fix #125
1 parent cf92003 commit d379e4e

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

pywxdump/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Author: xaoyaoo
66
# Date: 2023/10/14
77
# -------------------------------------------------------------------------------
8-
__version__ = "3.1.27"
8+
__version__ = "3.1.28"
99

1010
import os, json
1111

pywxdump/api/__init__.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
import sys
1111
import time
1212
import uvicorn
13+
import mimetypes
1314

1415
from fastapi import FastAPI, Request, Path, Query
1516
from fastapi.staticfiles import StaticFiles
1617
from fastapi.exceptions import RequestValidationError
1718
from starlette.middleware.cors import CORSMiddleware
18-
from starlette.responses import RedirectResponse
19+
from starlette.responses import RedirectResponse, FileResponse
1920

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

36+
web_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "ui", "web") # web文件夹路径
37+
3538
# 跨域
3639
origins = [
3740
"http://localhost:5000",
@@ -48,11 +51,13 @@ def gen_fastapi_app():
4851
allow_headers=["*"], # 允许所有头
4952
)
5053

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

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

71+
# 根据文件类型,设置mime_type,返回文件
72+
@app.get("/s/{filename:path}")
73+
async def serve_file(filename: str):
74+
# 构建完整的文件路径
75+
file_path = os.path.join(web_path, filename)
76+
77+
# 检查文件是否存在
78+
if os.path.isfile(file_path):
79+
# 获取文件 MIME 类型
80+
mime_type, _ = mimetypes.guess_type(file_path)
81+
# 如果 MIME 类型为空,则默认为 application/octet-stream
82+
if mime_type is None:
83+
mime_type = "application/octet-stream"
84+
85+
# 返回文件
86+
return FileResponse(file_path, media_type=mime_type)
87+
88+
# 如果文件不存在,返回 404
89+
return {"detail": "Not Found"}, 404
90+
6691
# 静态文件挂载
67-
web_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "ui", "web")
68-
if os.path.exists(os.path.join(web_path, "index.html")):
69-
app.mount("/s", StaticFiles(directory=web_path), name="static")
92+
# if os.path.exists(os.path.join(web_path, "index.html")):
93+
# app.mount("/s", StaticFiles(directory=web_path), name="static")
7094

7195
return app
7296

0 commit comments

Comments
 (0)