Skip to content

Commit a37fb88

Browse files
committed
fix: frontend upload option method error
1 parent 75f8d65 commit a37fb88

File tree

6 files changed

+23
-26
lines changed

6 files changed

+23
-26
lines changed

backend/README.md

Whitespace-only changes.

backend/app/api/v1/api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ async def upload_file(file: UploadFile = File(...)):
6565
"""
6666
try:
6767
# 检查文件大小
68-
if file.size and file.size > settings.MAX_FILE_SIZE:
68+
# 注意:FastAPI的UploadFile没有直接的size属性,需要读取文件内容来确定大小
69+
file_content = await file.read()
70+
file_size = len(file_content)
71+
await file.seek(0) # 重置文件指针位置
72+
73+
if file_size > settings.MAX_FILE_SIZE:
6974
raise HTTPException(
7075
status_code=413,
7176
detail=f"文件大小超过限制 ({settings.MAX_FILE_SIZE / 1024 / 1024:.1f}MB)"

backend/app/core/config.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ class Settings(BaseSettings):
2222
HOST: str = Field(default="0.0.0.0")
2323
PORT: int = Field(default=8000)
2424
ALLOWED_HOSTS: Union[str, List[str]] = Field(
25-
default="localhost,127.0.0.1,0.0.0.0"
25+
default="localhost,127.0.0.1,0.0.0.0,http://localhost:3000"
2626
)
2727

2828
# 数据库配置
2929
DATABASE_URL: str = Field(...)
3030
DATABASE_ECHO: bool = Field(default=False)
3131

32-
# Redis配置
33-
REDIS_URL: str = Field(default="redis://localhost:6379/0")
32+
# Redis配置 - 已移除,使用SQLite作为轻量级解决方案
3433

3534
# JWT配置
3635
JWT_SECRET_KEY: str = Field(default="")
@@ -69,9 +68,8 @@ class Settings(BaseSettings):
6968
default=".md,.txt,.docx,.pdf,.pptx"
7069
)
7170

72-
# Celery配置
73-
CELERY_BROKER_URL: str = Field(default="")
74-
CELERY_RESULT_BACKEND: str = Field(default="")
71+
# 任务队列配置 - 简化版,不使用Celery
72+
ENABLE_BACKGROUND_TASKS: bool = Field(default=False)
7573

7674
# 邮件配置
7775
SMTP_HOST: Optional[str] = Field(default=None)
@@ -108,21 +106,7 @@ def set_jwt_secret_key(cls, v, info):
108106
return info.data.get("SECRET_KEY", "")
109107
return v
110108

111-
@field_validator("CELERY_BROKER_URL", mode="before")
112-
@classmethod
113-
def set_celery_broker_url(cls, v, info):
114-
"""设置Celery代理URL,如果未提供则使用REDIS_URL"""
115-
if not v and info.data:
116-
return info.data.get("REDIS_URL", "redis://localhost:6379/0")
117-
return v
118-
119-
@field_validator("CELERY_RESULT_BACKEND", mode="before")
120-
@classmethod
121-
def set_celery_result_backend(cls, v, info):
122-
"""设置Celery结果后端,如果未提供则使用REDIS_URL"""
123-
if not v and info.data:
124-
return info.data.get("REDIS_URL", "redis://localhost:6379/0")
125-
return v
109+
# 移除了Celery相关的验证器,因为我们不再使用Celery
126110

127111
class Config:
128112
env_file = ".env"

backend/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ async def lifespan(app: FastAPI):
6060
# 添加CORS中间件
6161
app.add_middleware(
6262
CORSMiddleware,
63-
allow_origins=settings.ALLOWED_HOSTS,
63+
allow_origins=["*"], # 在开发环境中允许所有来源,生产环境中应该限制
6464
allow_credentials=True,
65-
allow_methods=["*"],
65+
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
6666
allow_headers=["*"],
67+
expose_headers=["Content-Length", "Content-Type", "X-Process-Time"],
68+
max_age=600, # 预检请求缓存10分钟
6769
)
6870

6971
# 添加可信主机中间件

backend/pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "backend"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = []

backend/requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ uvicorn[standard]==0.24.0
55
# 数据库相关
66
sqlalchemy==2.0.23
77
alembic==1.12.1
8-
asyncpg==0.29.0 # PostgreSQL 异步驱动
98
aiosqlite==0.19.0 # SQLite 异步驱动
109

1110
# 认证和安全
@@ -34,7 +33,7 @@ pytest-cov==4.1.0
3433
# AI 和机器学习
3534
openai==1.3.7
3635
scikit-learn==1.3.2
37-
numpy==1.24.4
36+
numpy>=1.26.0 # 更新到兼容 Python 3.12 的版本
3837

3938
# 其他工具
4039
click==8.1.7

0 commit comments

Comments
 (0)