增加对上传图片文件名称的校验
This commit is contained in:
@ -139,6 +139,9 @@ def upload_project_image(project_id: int = Form(...),
|
||||
project_info = pic.get_project_by_id(project_id, session)
|
||||
if project_info is None:
|
||||
return rc.response_error("项目查询错误,请刷新页面后再试")
|
||||
is_check, file_name = ps.check_image_name(project_id, files, session)
|
||||
if not is_check:
|
||||
return rc.response_error(msg="存在重名的图片文件:" + file_name)
|
||||
ps.upload_project_image(project_info, files, session)
|
||||
return rc.response_success(msg="上传成功")
|
||||
|
||||
|
@ -38,6 +38,7 @@ class ProjectLabel(DbCommon):
|
||||
class ProjectImage(DbCommon):
|
||||
"""项目图片表"""
|
||||
__tablename__ = "project_image"
|
||||
file_name: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
image_url: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
thumb_image_url: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
project_id: Mapped[int] = mapped_column(Integer)
|
||||
|
@ -14,6 +14,21 @@ def get_image_pager(image: ProjectImagePager, session: Session):
|
||||
return pager
|
||||
|
||||
|
||||
def check_img_name(project_id: int, file_name: str, session: Session):
|
||||
"""
|
||||
根据项目id和文件名称进行查重
|
||||
:param project_id:
|
||||
:param file_name:
|
||||
:param session:
|
||||
:return:
|
||||
"""
|
||||
image = session.query(piModel)\
|
||||
.filter(piModel.project_id == project_id).filter(piModel.file_name == file_name).first()
|
||||
if image is not None:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def get_img_url(image_id: int, session: Session):
|
||||
"""
|
||||
根据id获取图片
|
||||
|
@ -1,3 +1,5 @@
|
||||
import datetime
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from typing import Optional, List
|
||||
|
||||
@ -5,6 +7,8 @@ from typing import Optional, List
|
||||
class ProjectImage(BaseModel):
|
||||
id: Optional[int] = Field(None, description="id")
|
||||
project_id: Optional[int] = Field(..., description="项目id")
|
||||
file_name: Optional[str] = Field(None, description="文件名称")
|
||||
create_time: Optional[datetime.datetime] = Field(None, description="上传时间")
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
@ -36,6 +36,13 @@ def add_project(info: ProjectInfoIn, session: Session, user_id: int):
|
||||
return project_info.id
|
||||
|
||||
|
||||
def check_image_name(project_id: int, files: List[UploadFile], session: Session):
|
||||
for file in files:
|
||||
if not pimc.check_img_name(project_id, file.filename, session):
|
||||
return False, file.filename
|
||||
return True, None
|
||||
|
||||
|
||||
def upload_project_image(project_info: ProjectInfoOut, files: List[UploadFile], session: Session):
|
||||
"""
|
||||
上传项目的图片
|
||||
@ -48,6 +55,7 @@ def upload_project_image(project_info: ProjectInfoOut, files: List[UploadFile],
|
||||
for file in files:
|
||||
image = ProjectImage()
|
||||
image.project_id = project_info.id
|
||||
image.file_name = file.filename
|
||||
# 保存原图
|
||||
path = os.save_images(images_url, project_info.project_no, file=file)
|
||||
image.image_url = path
|
||||
|
Reference in New Issue
Block a user