本次提交包含两批改动(7月2日遗留未入库 + 本次新功能),分述如下:
【补登记:7月2日已上线但未提交的功能】
- 分片上传:chunk_upload_controller/service/dao + UploadSession model/schema,
支持 4MiB 分片、乱序、断点续传、去重、幂等 complete;后台 reaper 清理过期会话。
- 反向隧道:tunnel_controller/service/dao + TunnelSession model/schema,
SSH remote forward 经 /api/userPort/{userName} 反代到 user 本地服务。
- 上传页:views/upload_html.py(拖拽/多文件/分片/断点续传 UI)。
- config.py:StorageConfig.chunk_session_dir/ttl、TunnelConfig;
requirements.txt 加 httpx;start.sh 清理 .work/ 残留;
schema.sql 加 upload_session/tunnel_session 表;sftp_server 承载隧道转发。
【本次新功能】
- 文件浏览页:GET /files(Basic Auth 同 docs)+ /api/admin/files(list/get/download/DELETE)。
硬删除(DB 行 + 磁盘文件),删除后列表不再显示。前端 static/file_browser.*。
- 共享白板:GET /whiteboard/{id}(公开,不存在则新建)+ WS /ws/whiteboard/{id}。
MySQL 持久化(whiteboard 表),Canvas 实时同步,心跳 3s/5 次失活移除,
清空/复制按钮,移动端兼容。WhiteboardHub 管理 {board_id: set[Connection]},
disconnect 幂等 + 空 set 清理防泄漏,broadcast 失败连接自动移除。
- 白板管理页:GET /whiteboard-admin(Basic Auth)+ /api/admin/whiteboards(list/DELETE)。
删除时 hub.close_board 踢出在线连接。
- 清理:合并 UploadService.get_out_with_disk_path(下载/删除复用,消除重复 DB 读),
移除无用 resolve_disk_path。
- config.py:WhiteboardConfig(heartbeat/threshold/board_id 长度/list_limit);
schema.sql 加 whiteboard 表;README 补新接口与心跳/内存说明。
- 验证:tests/manual_whiteboard_hub.py / _ws.py / _kick.py 全部通过。
90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
"""文件上传 / 列表 / 下载接口。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
||
from fastapi.responses import FileResponse
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ..database import get_db
|
||
from ..dao.uploaded_file_dao import UploadedFileDAO
|
||
from ..schemas.file import (
|
||
FileListResponse,
|
||
FileUploadResponse,
|
||
UploadedFileOut,
|
||
)
|
||
from ..services.upload_service import UploadService
|
||
|
||
router = APIRouter(prefix="/api/files", tags=["files"])
|
||
|
||
|
||
def _service(db: Session = Depends(get_db)) -> UploadService:
|
||
return UploadService(UploadedFileDAO(db))
|
||
|
||
|
||
@router.post(
|
||
"/upload",
|
||
response_model=FileUploadResponse,
|
||
summary="上传单个文件(流式,支持大文件)",
|
||
description=(
|
||
"multipart/form-data 上传,按 1 MiB 分片流式落盘,内存占用恒定;"
|
||
"落盘过程中计算 SHA-256 并入库。\n\n"
|
||
"极大或极慢的传输建议改用分片上传接口(/api/files/chunk-uploads)或 SFTP。"
|
||
),
|
||
)
|
||
async def upload_file(
|
||
file: UploadFile = File(..., description="要上传的文件"),
|
||
service: UploadService = Depends(_service),
|
||
) -> FileUploadResponse:
|
||
if not file.filename:
|
||
raise HTTPException(400, "请求缺少 'file' 字段")
|
||
return service.stream_to_disk(file, source="http", uploaded_by="anonymous")
|
||
|
||
|
||
@router.get("", response_model=FileListResponse, summary="列出已上传的文件")
|
||
def list_files(
|
||
limit: int = 100,
|
||
offset: int = 0,
|
||
service: UploadService = Depends(_service),
|
||
) -> FileListResponse:
|
||
total, items = service.list_files(limit=limit, offset=offset)
|
||
return FileListResponse(total=total, items=items)
|
||
|
||
|
||
@router.get(
|
||
"/exists",
|
||
response_model=UploadedFileOut,
|
||
summary="按 sha256 查询是否已上传",
|
||
description="命中返回 200 + 元数据;未命中返回 404。供客户端在上传前去重。",
|
||
)
|
||
def file_exists(
|
||
sha256: str,
|
||
service: UploadService = Depends(_service),
|
||
) -> UploadedFileOut:
|
||
out = service.find_by_sha256(sha256)
|
||
if out is None:
|
||
raise HTTPException(404, "未找到匹配的 sha256")
|
||
return out
|
||
|
||
|
||
@router.get("/{file_id}", response_model=UploadedFileOut, summary="查询单个文件元数据")
|
||
def get_file(file_id: int, service: UploadService = Depends(_service)) -> UploadedFileOut:
|
||
out = service.get_out(file_id)
|
||
if out is None:
|
||
raise HTTPException(404, "文件不存在")
|
||
return out
|
||
|
||
|
||
@router.get("/{file_id}/download", summary="下载文件")
|
||
def download_file(file_id: int, service: UploadService = Depends(_service)) -> FileResponse:
|
||
out, path = service.get_out_with_disk_path(file_id)
|
||
if out is None:
|
||
raise HTTPException(404, "文件不存在")
|
||
if path is None or not path.exists():
|
||
raise HTTPException(410, "文件实体已不在磁盘上")
|
||
return FileResponse(
|
||
path=str(path),
|
||
media_type=out.content_type or "application/octet-stream",
|
||
filename=out.original_filename,
|
||
)
|