From e5848725dbce7a6d76b0cc262083e9fa4ece2780 Mon Sep 17 00:00:00 2001 From: Fantasy lee <129943055+Fantasylee21@users.noreply.github.com> Date: Sat, 3 May 2025 21:25:49 +0800 Subject: [PATCH] =?UTF-8?q?[fix]:=20=E5=A4=B4=E5=83=8F=E6=8C=82=E8=BD=BD?= =?UTF-8?q?=E9=9D=99=E6=80=81=E6=96=87=E4=BB=B6=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/v1/endpoints/user.py | 4 ++-- app/curd/user.py | 2 +- app/main.py | 6 +++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/api/v1/endpoints/user.py b/app/api/v1/endpoints/user.py index e9f619b..437f360 100644 --- a/app/api/v1/endpoints/user.py +++ b/app/api/v1/endpoints/user.py @@ -41,10 +41,10 @@ async def update_current_user( f.write(await avatar_file.read()) # 生成 URL 路径 - avatar_url = f"/app/static/avatar/{unique_filename}" + avatar_url = f"/static/avatar/{unique_filename}" # 删除旧的头像文件 - if db_user.avatar and db_user.avatar != "/app/static/avatar/default.png": + if db_user.avatar and db_user.avatar != "/static/avatar/default.png": old_avatar_path = db_user.avatar.lstrip("/") # 去掉开头的斜杠 if os.path.exists(old_avatar_path): os.remove(old_avatar_path) diff --git a/app/curd/user.py b/app/curd/user.py index 0a42ede..6545c34 100644 --- a/app/curd/user.py +++ b/app/curd/user.py @@ -9,7 +9,7 @@ async def get_user_by_email(db: AsyncSession, email: str): return result.scalar_one_or_none() async def create_user(db: AsyncSession, email: str, username: str, hashed_password: str): - new_user = User(email=email, username=username, password=hashed_password, avatar="/app/static/avatar/default.png") + new_user = User(email=email, username=username, password=hashed_password, avatar="/static/avatar/default.png") db.add(new_user) await db.commit() await db.refresh(new_user) diff --git a/app/main.py b/app/main.py index 840f0ba..e4e1c3c 100644 --- a/app/main.py +++ b/app/main.py @@ -3,6 +3,7 @@ from fastapi_pagination import add_pagination from loguru import logger from fastapi.middleware.cors import CORSMiddleware +from fastapi.staticfiles import StaticFiles app = FastAPI() @@ -37,4 +38,7 @@ async def log_requests(request: Request, call_next): allow_credentials=True, # 允许发送凭据(如 Cookies 或 Authorization 头) allow_methods=["*"], # 允许的 HTTP 方法 allow_headers=["*"], # 允许的请求头 -) \ No newline at end of file +) + +# 挂载静态文件目录 +app.mount("/static", StaticFiles(directory="app/static"), name="static") \ No newline at end of file