"""模板相关业务逻辑""" from fastapi import UploadFile from sqlalchemy.orm import Session from ..models.template import Template ALLOWED_EXTENSIONS = {".docx"} MAX_FILE_SIZE = 50 * 1024 * 1024 # 50MB def validate_file(file: UploadFile): """校验文件格式和大小""" ext = "." + file.filename.rsplit(".", 1)[-1].lower() if "." in file.filename else "" if ext not in ALLOWED_EXTENSIONS: raise ValueError(f"不支持的文件格式: {ext},仅支持 .docx") # 读取文件头校验大小 content = file.file.read() file.file.seek(0) if len(content) > MAX_FILE_SIZE: raise ValueError(f"文件大小超过限制 (50MB)") return content def create_template_record(db: Session, name: str, file_path: str, type: str = "report") -> Template: """创建模板记录""" tmpl = Template(name=name, type=type, original_file_path=file_path) db.add(tmpl) db.commit() db.refresh(tmpl) return tmpl