feat:增加上传博客图片模块

This commit is contained in:
2025-10-25 22:53:46 +08:00
parent 6512c536dc
commit f6d1096dbc
7 changed files with 89 additions and 10 deletions

View File

@@ -17,11 +17,12 @@ def sync_all_blog(db: Session) -> bool:
if settings.ENVIRONMENT == 'dev':
return True
query = select(
stmt = select(
Blog.id,
Blog.title,
BlogCategory.name.label("category"),
BlogContent.content.label("content"),
Blog.is_approved.label("isApproved"),
Blog.create_time.label("createTime"),
Blog.update_time.label("updateTime"),
).select_from(Blog).outerjoin(
@@ -29,7 +30,7 @@ def sync_all_blog(db: Session) -> bool:
).outerjoin(
BlogContent, Blog.content_id == BlogContent.id
)
results = db.execute(query).fetchall()
results = db.execute(stmt).fetchall()
actions = [
{
@@ -40,6 +41,7 @@ def sync_all_blog(db: Session) -> bool:
"title": blog.title,
"category": blog.category,
"content": blog.content.decode("utf-8"),
"isApproved": blog.isApproved,
"createTime": blog.createTime.strftime("%Y-%m-%d %H:%M:%S"),
"updateTime": blog.updateTime.strftime("%Y-%m-%d %H:%M:%S")
}
@@ -106,10 +108,23 @@ def search_blog(keyword: str) -> List[BlogSearch]:
# 构造查询请求
body = {
"query": {
"multi_match": {
"query": keyword,
"fields": ["title^3", "content"],
"type": "phrase"
"bool": {
"must": [
{
"multi_match": {
"query": keyword,
"fields": ["title^3", "content"],
"type": "phrase"
}
}
],
"filter": [
{
"term": {
"isApproved": 1
}
}
]
}
},
"highlight": {

40
service/file_service.py Normal file
View File

@@ -0,0 +1,40 @@
from fastapi import UploadFile, File, HTTPException
from config.rustfs import s3
from config.setting import settings
ALLOWED_IMAGE_TYPES = [
"image/jpeg",
"image/png",
"image/gif",
"image/webp",
"image/svg+xml",
]
BUCKET = 'blog'
NGINX_PROXY = 'rustfs'
async def upload_file(md5: str, file: UploadFile = File(...)) -> str:
# 校验文件类型是否是图片
if file.content_type not in ALLOWED_IMAGE_TYPES:
raise HTTPException(
status_code=400,
detail="只允许上传图片文件 (JPEG, PNG, GIF, WEBP, SVG)"
)
file_ext = file.filename.split('.')[-1]
unique_filename = f"{md5}.{file_ext}"
file_content = await file.read()
# 上传到S3
s3.put_object(
Bucket=BUCKET,
Key=unique_filename,
Body=file_content,
ContentType=file.content_type
)
# 返回文件url
return f"{NGINX_PROXY}/{BUCKET}/{unique_filename}"