Files
zTools2/app/schemas/file.py
zikai 6c7d3f1272 feat: 增加 /api/files/exists 与 /api/files/register-sftp 接口
- DAO 增加 get_by_sha256
- UploadService 抽出 _to_response 复用;新增 find_by_sha256、register_sftp
- 控制器新增两条路由(注意排在 /{file_id} 之前以避免被吞)
- 引入 SftpRegisterRequest schema
- SFTP 服务启动时自动创建 uploads/incoming/
- 行为:路径穿越 400、文件不存在 404、sha256 已存在则去重返回已有行
2026-06-23 16:22:03 +00:00

47 lines
1.2 KiB
Python

"""文件接口 DTO。"""
from __future__ import annotations
from datetime import datetime
from pydantic import BaseModel, Field
class UploadedFileOut(BaseModel):
id: int
storage_path: str
original_filename: str
content_type: str
size_bytes: int
sha256: str
source: str
uploaded_by: str
uploaded_at: datetime
model_config = {"from_attributes": True}
class FileUploadResponse(BaseModel):
id: int = Field(..., description="新写入的数据库行 ID")
filename: str = Field(..., description="客户端原始文件名")
size_bytes: int
sha256: str
storage_path: str
uploaded_at: datetime
class FileListResponse(BaseModel):
total: int
items: list[UploadedFileOut]
class SftpRegisterRequest(BaseModel):
"""登记一个通过 SFTP 上传到 ``incoming/`` 下的文件。"""
filename: str = Field(
...,
description="文件在 SFTP chroot 下的相对路径,必须落在 incoming/ 之下,例如 incoming/abc.bin",
)
original_filename: str = Field(..., description="客户端原始文件名")
uploaded_by: str = Field(default="sftp", description="登记者标识,写入 uploaded_by 字段")