Files
filecodebox/apps/base/views.py
T
orion 3b6949d862 🐛 fix: 整合官方 2.7.1 修复并升级定制版本
* 校验变更的出站地址并保留现有内部 CloudDrive 配置
* 修复未知长度上传、API 404 语义及下载令牌比较
* 合并 S3 小分片为符合最小尺寸要求的 multipart 部分
* 同步官方运行依赖、版本元数据和已推送前端版本
* 验证单元集成、覆盖率及定制上传下载兼容并记录整合范围
2026-10-07 00:16:11 +08:00

1142 lines
43 KiB
Python

import datetime
import asyncio
import hashlib
import hmac
from io import BytesIO
import os
import uuid
from datetime import timedelta
from urllib.parse import quote, unquote
from typing import Optional, Tuple, Union
from fastapi import APIRouter, Form, Request, UploadFile, File, Depends, HTTPException
from pydantic import BaseModel, ValidationError
from starlette import status
from starlette.responses import Response, RedirectResponse
from tortoise.expressions import Case, F, Q, When
from apps.admin.dependencies import share_required_login
from apps.base.models import FileCodes, UploadChunk, PresignUploadSession, TransferStats
from apps.base.quota import release_storage, reserve_storage
from apps.base.upload_sessions import (serialize_upload, upload_lock, completion_task, start_completion, take_completion, cancel_completion, is_cancelling, begin_cancel, end_cancel)
from apps.base.schemas import (
SelectFileModel,
InitChunkUploadModel,
CompleteUploadModel,
PresignUploadInitRequest,
)
from apps.base.file_validation import validate_file_type, validate_upload_file, validate_header_bytes
from apps.base.utils import (
get_expire_info,
get_file_path_name,
ip_limit,
get_chunk_file_path_name,
validate_expire_style,
)
from apps.base.local_share import is_local_ref, build_local_ref_download
from core.response import APIResponse
from core.settings import settings
from core.storage import storages, FileStorageInterface
from core.utils import (
get_file_url as get_proxy_file_url,
get_select_token,
get_now,
sanitize_filename,
)
share_api = APIRouter(prefix="/share", tags=["分享"])
# ============ 公共服务层 ============
class FileUploadService:
"""统一的文件上传服务"""
@staticmethod
async def generate_file_path(
file_name: str, upload_id: Optional[str] = None
) -> tuple[str, str, str, str, str]:
"""统一的路径生成"""
today = datetime.datetime.now()
storage_path = settings.storage_path.strip("/")
file_uuid = upload_id or uuid.uuid4().hex
filename = await sanitize_filename(unquote(file_name))
base_path = f"share/data/{today.strftime('%Y/%m/%d')}/{file_uuid}"
path = f"{storage_path}/{base_path}" if storage_path else base_path
prefix, suffix = os.path.splitext(filename)
save_path = f"{path}/{filename}"
return path, suffix, prefix, filename, save_path
@staticmethod
async def create_file_record(
file_name: str,
file_size: int,
file_path: str,
expire_value: int,
expire_style: str,
ip: str = "",
**extra_fields,
) -> str:
"""统一创建FileCodes记录,返回code"""
expired_at, expired_count, used_count, code = await get_expire_info(
expire_value, expire_style
)
prefix, suffix = os.path.splitext(file_name)
file_code = await FileCodes.create(
code=code,
prefix=prefix,
suffix=suffix,
uuid_file_name=file_name,
file_path=file_path,
size=file_size,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
**extra_fields,
)
await record_transfer_stat("upload", file_code, file_size, ip)
return code
async def validate_file_size(file: UploadFile, max_size: int) -> int:
size = file.size
if size is None:
def measure():
file.file.seek(0, 2)
size = file.file.tell()
file.file.seek(0)
return size
size = await asyncio.to_thread(measure)
if size > max_size:
max_size_mb = max_size / (1024 * 1024)
raise HTTPException(
status_code=403, detail=f"大小超过限制,最大为{max_size_mb:.2f} MB"
)
return size
async def create_file_code(code, **kwargs):
return await FileCodes.create(code=code, **kwargs)
async def record_transfer_stat(
action: str,
file_code: FileCodes,
size: Optional[int] = None,
ip: str = "",
deleted_at=None,
meta: str = "",
) -> None:
try:
name = (
f"{file_code.prefix or ''}{file_code.suffix or ''}"
or file_code.uuid_file_name
or file_code.code
)
await TransferStats.create(
action=action,
file_code_id=file_code.id,
code=file_code.code,
name=name,
size=int(size if size is not None else (file_code.size or 0)),
ip=ip or "",
file_path=file_code.file_path or "",
expired_at=file_code.expired_at,
deleted_at=deleted_at,
meta=meta or "",
)
except Exception:
pass
def normalize_share_code(code: str) -> str:
return str(code or "").strip()
@share_api.post("/text/", dependencies=[Depends(share_required_login)])
async def share_text(
text: str = Form(...),
expire_value: int = Form(default=1, gt=0),
expire_style: str = Form(default="day"),
ip: str = Depends(ip_limit["upload"]),
):
validate_expire_style(expire_style)
text_size = len(text.encode("utf-8"))
max_txt_size = 222 * 1024
if text_size > max_txt_size:
raise HTTPException(status_code=403, detail="内容过多,建议采用文件形式")
reservation_token = f"text:{uuid.uuid4().hex}"
await reserve_storage(reservation_token, text_size, ttl_seconds=300)
try:
expired_at, expired_count, used_count, code = await get_expire_info(
expire_value, expire_style
)
file_code = await create_file_code(
code=code,
text=text,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
size=text_size,
prefix="Text",
)
await record_transfer_stat("upload", file_code, text_size, ip)
finally:
await release_storage(reservation_token)
ip_limit["upload"].add_ip(ip)
return APIResponse(detail={"code": code})
@share_api.post("/file/", dependencies=[Depends(share_required_login)])
async def share_file(
expire_value: int = Form(default=1, gt=0),
expire_style: str = Form(default="day"),
file: UploadFile = File(...),
ip: str = Depends(ip_limit["upload"]),
):
file_size = await validate_file_size(file, settings.uploadSize)
await validate_upload_file(file)
validate_expire_style(expire_style)
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(file)
reservation_token = f"file:{uuid.uuid4().hex}"
await reserve_storage(reservation_token, file_size, ttl_seconds=3600)
file_storage: FileStorageInterface = storages[settings.file_storage]()
try:
expired_at, expired_count, used_count, code = await get_expire_info(
expire_value, expire_style
)
await file_storage.save_file(file, save_path)
file_code = await create_file_code(
code=code,
prefix=prefix,
suffix=suffix,
uuid_file_name=uuid_file_name,
file_path=path,
size=file_size,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
)
await record_transfer_stat("upload", file_code, file_size, ip)
except Exception:
try:
await file_storage.delete_file(
FileCodes(file_path=path, uuid_file_name=uuid_file_name)
)
except Exception:
pass
raise
finally:
await release_storage(reservation_token)
ip_limit["upload"].add_ip(ip)
return APIResponse(detail={"code": code, "name": file.filename})
async def get_code_file_by_code(
code: str, check: bool = True
) -> Tuple[bool, Union[FileCodes, str]]:
normalized_code = normalize_share_code(code)
if not normalized_code:
return False, "文件不存在"
file_code = await FileCodes.filter(code=normalized_code, is_trashed=False).first()
if not file_code:
return False, "文件不存在"
if check and (file_code.expired_count == 0 or await file_code.is_expired()):
return False, "文件已过期"
return True, file_code
async def consume_file_usage(file_code: FileCodes) -> bool:
"""原子校验分享状态并记录一次实际领取。"""
now = await get_now()
eligible = (
Q(expired_count__gt=0)
| Q(expired_count__lt=0, expired_at__gt=now)
| Q(expired_count__lt=0, expired_at=None)
)
updated = await FileCodes.filter(Q(id=file_code.id) & eligible).update(
expired_count=Case(
When(expired_count__gt=0, then=F("expired_count") - 1),
default=F("expired_count"),
),
used_count=F("used_count") + 1,
)
if not updated:
return False
await file_code.refresh_from_db()
return True
def build_file_metadata(file_code: FileCodes) -> dict:
is_text = file_code.text is not None
remaining_downloads = (
file_code.expired_count if file_code.expired_count > 0 else None
)
return {
"code": file_code.code,
"name": file_code.prefix + file_code.suffix,
"size": file_code.size,
"type": "text" if is_text else "file",
"is_text": is_text,
"created_at": file_code.created_at,
"expired_at": file_code.expired_at,
"expires_at": file_code.expired_at,
"expired_count": file_code.expired_count,
"used_count": file_code.used_count,
"remaining_downloads": remaining_downloads,
}
async def build_download_response(file_code: FileCodes, file_storage: FileStorageInterface):
if file_code.text is not None:
filename = f"{file_code.prefix or 'Text'}{file_code.suffix or '.txt'}"
return Response(
content=file_code.text,
media_type="text/plain",
headers={"Content-Disposition": f"attachment; filename*=UTF-8''{quote(filename, safe='')}"},
)
if is_local_ref(file_code):
return build_local_ref_download(file_code)
if file_code.expired_count < 0:
url = await file_storage.get_file_url(file_code)
if isinstance(url, str) and url.startswith(("https://", "http://")):
return RedirectResponse(url)
return await file_storage.get_file_response(file_code)
async def build_select_detail(
file_code: FileCodes, file_storage: FileStorageInterface
) -> dict:
metadata = build_file_metadata(file_code)
# Lookup never counts as retrieval; all downloads first pass this signed entry.
# Unlimited direct storage links still redirect at download time.
download_url = await get_proxy_file_url(file_code.code)
content = file_code.text if file_code.text is not None else None
return {
**metadata,
"text": content if content is not None else download_url,
"content": content,
"download_url": download_url,
}
@share_api.get("/metadata/")
async def get_file_metadata(code: str, ip: str = Depends(ip_limit["metadata"])):
has, file_code = await get_code_file_by_code(code)
if not has:
ip_limit["metadata"].add_ip(ip)
return APIResponse(code=404, detail=file_code)
assert isinstance(file_code, FileCodes)
ip_limit["metadata"].add_ip(ip)
return APIResponse(detail=build_file_metadata(file_code))
@share_api.post("/metadata/")
async def post_file_metadata(
data: SelectFileModel, ip: str = Depends(ip_limit["metadata"])
):
has, file_code = await get_code_file_by_code(data.code)
if not has:
ip_limit["metadata"].add_ip(ip)
return APIResponse(code=404, detail=file_code)
assert isinstance(file_code, FileCodes)
ip_limit["metadata"].add_ip(ip)
return APIResponse(detail=build_file_metadata(file_code))
@share_api.get("/select/")
async def get_code_file(code: str, ip: str = Depends(ip_limit["error"])):
file_storage: FileStorageInterface = storages[settings.file_storage]()
has, file_code = await get_code_file_by_code(code)
if not has:
ip_limit["error"].add_ip(ip)
return APIResponse(code=404, detail=file_code)
assert isinstance(file_code, FileCodes)
if not await consume_file_usage(file_code):
return APIResponse(code=404, detail="文件已过期")
await record_transfer_stat("download", file_code, file_code.size, ip)
return await build_download_response(file_code, file_storage)
@share_api.post("/select/")
async def select_file(data: SelectFileModel, ip: str = Depends(ip_limit["error"])):
file_storage: FileStorageInterface = storages[settings.file_storage]()
has, file_code = await get_code_file_by_code(data.code)
if not has:
ip_limit["error"].add_ip(ip)
return APIResponse(code=404, detail=file_code)
assert isinstance(file_code, FileCodes)
detail = await build_select_detail(file_code, file_storage)
await record_transfer_stat("lookup", file_code, 0, ip)
return APIResponse(detail=detail)
@share_api.get("/download")
async def download_file(key: str, code: str, ip: str = Depends(ip_limit["error"])):
file_storage: FileStorageInterface = storages[settings.file_storage]()
normalized_code = normalize_share_code(code)
# 同时接受当前窗口与上一窗口 token,避免时间窗边界竞态导致偶发 403
valid_keys = [
await get_select_token(normalized_code, offset=0),
await get_select_token(normalized_code, offset=1),
]
if not any(hmac.compare_digest(key.encode(), candidate.encode()) for candidate in valid_keys):
ip_limit["error"].add_ip(ip)
raise HTTPException(status_code=403, detail="下载鉴权失败")
has, file_code = await get_code_file_by_code(normalized_code)
if not has:
return APIResponse(code=404, detail=file_code)
assert isinstance(file_code, FileCodes)
if not await consume_file_usage(file_code):
return APIResponse(code=404, detail="文件已过期")
await record_transfer_stat("download", file_code, file_code.size, ip)
return await build_download_response(file_code, file_storage)
chunk_api = APIRouter(prefix="/chunk", tags=["切片"])
async def parse_body_model(request: Request, model_class: type[BaseModel]):
content_type = request.headers.get("content-type", "").lower()
try:
if "application/x-www-form-urlencoded" in content_type or "multipart/form-data" in content_type:
payload = dict(await request.form())
else:
payload = await request.json()
return model_class.model_validate(payload)
except ValidationError as e:
raise HTTPException(status_code=422, detail=e.errors())
except Exception:
raise HTTPException(status_code=400, detail="请求体格式错误")
async def parse_init_chunk_upload(request: Request) -> InitChunkUploadModel:
return await parse_body_model(request, InitChunkUploadModel)
async def parse_complete_upload(request: Request) -> CompleteUploadModel:
return await parse_body_model(request, CompleteUploadModel)
@chunk_api.post("/upload/init/", dependencies=[Depends(share_required_login)])
async def init_chunk_upload(data: InitChunkUploadModel = Depends(parse_init_chunk_upload)):
safe_file_name = await sanitize_filename(unquote(data.file_name or ""))
validate_file_type(safe_file_name)
# New sessions negotiate the smaller client/server size. Existing sessions
# below retain their original offsets when an administrator changes this.
configured_mb = max(1, min(100, int(settings.chunkSizeMB)))
data.chunk_size = min(data.chunk_size, configured_mb * 1024 * 1024, data.file_size)
# File limits concern actual bytes, not a rounded-up final chunk.
total_chunks = (data.file_size + data.chunk_size - 1) // data.chunk_size
max_possible_size = data.file_size
if max_possible_size > settings.uploadSize:
max_size_mb = settings.uploadSize / (1024 * 1024)
raise HTTPException(
status_code=403, detail=f"文件大小超过限制,最大为 {max_size_mb:.2f} MB"
)
# # 秒传检查
# existing = await FileCodes.filter(file_hash=data.file_hash).first()
# if existing:
# if await existing.is_expired():
# file_storage: FileStorageInterface = storages[settings.file_storage](
# )
# await file_storage.delete_file(existing)
# await existing.delete()
# else:
# return APIResponse(detail={
# "code": existing.code,
# "existed": True,
# "name": f'{existing.prefix}{existing.suffix}'
# })
preferred_mode = "legacy"
if settings.file_storage == "webdav":
preferred_mode = await storages["webdav"]().chunk_upload_mode()
# 断点续传:检查是否存在相同文件的未完成上传会话
existing_session = await UploadChunk.filter(
chunk_hash=data.file_hash,
chunk_index=-1,
file_size=data.file_size,
file_name=safe_file_name,
).first()
if existing_session and existing_session.storage_mode == "webdav-offset" and preferred_mode == "webdav-stream":
# Restart the failed mutable-object protocol instead of extending it again.
await cancel_upload(existing_session.upload_id)
existing_session = None
if existing_session:
task = completion_task(existing_session.upload_id)
if task is not None and not task.done():
return APIResponse(detail={
"existed":False,"upload_id":existing_session.upload_id,
"chunk_size":existing_session.chunk_size,"total_chunks":existing_session.total_chunks,
"uploaded_chunks":list(range(existing_session.total_chunks)),"processing":True,
})
async with upload_lock(existing_session.upload_id):
existing_session = await UploadChunk.filter(upload_id=existing_session.upload_id, chunk_index=-1).first()
if existing_session:
if not existing_session.save_path:
await UploadChunk.filter(upload_id=existing_session.upload_id).delete()
await release_storage(f"chunk:{existing_session.upload_id}")
else:
await reserve_storage(
f"chunk:{existing_session.upload_id}",
existing_session.file_size,
ttl_seconds=max(
1, int(getattr(settings, "chunk_expire_hours", 24))
)
* 3600,
)
await UploadChunk.filter(id=existing_session.id).update(created_at=await get_now())
uploaded_chunks = await UploadChunk.filter(
upload_id=existing_session.upload_id, completed=True
).values_list("chunk_index", flat=True)
return APIResponse(
detail={
"existed": False,
"upload_id": existing_session.upload_id,
"chunk_size": existing_session.chunk_size,
"total_chunks": existing_session.total_chunks,
"uploaded_chunks": list(uploaded_chunks),
}
)
# 创建新的上传会话
upload_id = uuid.uuid4().hex
reservation_token = f"chunk:{upload_id}"
chunk_expire_seconds = max(1, int(getattr(settings, "chunk_expire_hours", 24))) * 3600
await reserve_storage(
reservation_token, data.file_size, ttl_seconds=chunk_expire_seconds
)
_, _, _, safe_file_name, save_path = await get_chunk_file_path_name(
data.file_name, upload_id
)
try:
storage_mode = preferred_mode
await UploadChunk.create(
upload_id=upload_id,
storage_mode=storage_mode,
chunk_index=-1,
total_chunks=total_chunks,
file_size=data.file_size,
chunk_size=data.chunk_size,
chunk_hash=data.file_hash,
file_name=safe_file_name,
save_path=save_path,
)
except Exception:
await release_storage(reservation_token)
raise
return APIResponse(
detail={
"existed": False,
"upload_id": upload_id,
"chunk_size": data.chunk_size,
"total_chunks": total_chunks,
"uploaded_chunks": [],
}
)
@chunk_api.post(
"/upload/chunk/{upload_id}/{chunk_index}",
dependencies=[Depends(share_required_login)],
)
@serialize_upload
async def upload_chunk(
upload_id: str,
chunk_index: int,
chunk: UploadFile = File(...),
):
# 获取上传会话信息
chunk_info = await UploadChunk.filter(upload_id=upload_id, chunk_index=-1).first()
if not chunk_info:
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="上传会话不存在")
await reserve_storage(
f"chunk:{upload_id}",
chunk_info.file_size,
ttl_seconds=max(1, int(getattr(settings, "chunk_expire_hours", 24))) * 3600,
)
# 检查分片索引有效性
if chunk_index < 0 or chunk_index >= chunk_info.total_chunks:
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="无效的分片索引")
# 检查是否已上传(支持断点续传)
existing_chunk = await UploadChunk.filter(
upload_id=upload_id, chunk_index=chunk_index, completed=True
).first()
if existing_chunk and chunk_info.storage_mode not in {"webdav-offset", "webdav-stream", "clouddrive-native"}:
return APIResponse(
detail={"chunk_hash": existing_chunk.chunk_hash, "skipped": True}
)
# 读取分片数据并计算哈希
chunk_data = await chunk.read()
if chunk_index == 0:
validate_header_bytes(chunk_info.file_name, None, chunk_data[:64])
chunk_size = len(chunk_data)
# 校验分片大小不超过声明的 chunk_size
if chunk_size > chunk_info.chunk_size:
raise HTTPException(
status.HTTP_400_BAD_REQUEST,
detail=f"分片大小超过声明值: 最大 {chunk_info.chunk_size}, 实际 {chunk_size}",
)
# 计算已上传分片数,校验累计大小不超限(用分片数 * chunk_size 估算)
uploaded_count = await UploadChunk.filter(
upload_id=upload_id, completed=True
).count()
# 已上传分片的最大可能大小 + 当前分片
max_uploaded_size = chunk_info.file_size
if max_uploaded_size > settings.uploadSize:
max_size_mb = settings.uploadSize / (1024 * 1024)
raise HTTPException(
status_code=403, detail=f"累计上传大小超过限制,最大为 {max_size_mb:.2f} MB"
)
chunk_hash = hashlib.sha256(chunk_data).hexdigest()
expected_size = min(chunk_info.chunk_size, chunk_info.file_size-chunk_index*chunk_info.chunk_size)
if chunk_size != expected_size:
raise HTTPException(400, "分片大小与声明值不一致")
if chunk_info.storage_mode in {"webdav-offset", "webdav-stream", "clouddrive-native"}:
if existing_chunk:
if existing_chunk.chunk_hash != chunk_hash:
raise HTTPException(409, "重传分片内容不一致")
return APIResponse(detail={"chunk_hash":chunk_hash,"skipped":True})
save_path = chunk_info.save_path
# 保存分片到存储
storage = storages[settings.file_storage]()
try:
await storage.save_chunk(
upload_id, chunk_index, chunk_data, chunk_hash, save_path
)
except HTTPException:
raise
except Exception as e:
raise HTTPException(
status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"分片保存失败: {str(e)}"
)
# 更新或创建分片记录(保存成功后再记录)
await UploadChunk.update_or_create(
upload_id=upload_id,
chunk_index=chunk_index,
defaults={
"chunk_hash": chunk_hash,
"completed": True,
"file_size": chunk_info.file_size,
"total_chunks": chunk_info.total_chunks,
"chunk_size": chunk_info.chunk_size,
"file_name": chunk_info.file_name,
"save_path": chunk_info.save_path,
},
)
await UploadChunk.filter(id=chunk_info.id).update(created_at=await get_now())
return APIResponse(detail={"chunk_hash": chunk_hash})
@chunk_api.post("/upload/raw/{upload_id}/{chunk_index}", dependencies=[Depends(share_required_login)])
async def upload_raw_chunk(upload_id: str, chunk_index: int, request: Request):
info = await UploadChunk.filter(upload_id=upload_id, chunk_index=-1).first()
if info is None:
raise HTTPException(404, "上传会话不存在")
if not 0 <= chunk_index < info.total_chunks:
raise HTTPException(400, "无效的分片索引")
maximum = min(info.chunk_size, info.file_size - chunk_index * info.chunk_size, 100 * 1024 * 1024)
content_length = request.headers.get("content-length")
if content_length:
try:
if int(content_length) > maximum: raise HTTPException(413, "分片超过声明大小")
except ValueError: raise HTTPException(400, "无效的请求大小")
buffer = BytesIO()
try:
async for block in request.stream():
if buffer.tell() + len(block) > maximum:
raise HTTPException(413, "分片超过声明大小")
buffer.write(block)
buffer.seek(0)
return await upload_chunk(upload_id, chunk_index, UploadFile(file=buffer, filename=info.file_name))
finally:
buffer.close()
@chunk_api.delete("/upload/{upload_id}", dependencies=[Depends(share_required_login)])
async def cancel_upload(upload_id: str):
if is_cancelling(upload_id):
raise HTTPException(409, "上传正在取消,请稍后重试")
begin_cancel(upload_id)
try:
await cancel_completion(upload_id)
return await _cancel_upload(upload_id)
finally:
end_cancel(upload_id)
@serialize_upload
async def _cancel_upload(upload_id):
"""取消上传并清理临时文件"""
published = await FileCodes.filter(upload_id=upload_id).first()
chunk_info = await UploadChunk.filter(upload_id=upload_id, chunk_index=-1).first()
if published:
if chunk_info and chunk_info.save_path:
await storages[settings.file_storage]().clean_chunks(upload_id, chunk_info.save_path)
await UploadChunk.filter(upload_id=upload_id).delete()
await release_storage(f"chunk:{upload_id}")
return APIResponse(detail={"completed":True, "code":published.code, "name":published.prefix+published.suffix})
if not chunk_info:
return APIResponse(detail={"cancelled":True, "message":"上传会话已结束"})
save_path = chunk_info.save_path
# 清理存储中的临时文件
storage = storages[settings.file_storage]()
if save_path:
try:
await storage.clean_chunks(upload_id, save_path)
except Exception as e:
if chunk_info.storage_mode in {"webdav-offset", "webdav-stream", "clouddrive-native"}:
raise
pass
# 清理数据库记录
await UploadChunk.filter(upload_id=upload_id).delete()
await release_storage(f"chunk:{upload_id}")
return APIResponse(detail={"cancelled":True,"message": "上传已取消"})
@chunk_api.get(
"/upload/status/{upload_id}", dependencies=[Depends(share_required_login)]
)
async def get_upload_status(upload_id: str):
"""获取上传状态"""
chunk_info = await UploadChunk.filter(upload_id=upload_id, chunk_index=-1).first()
if not chunk_info:
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="上传会话不存在")
# 获取已上传的分片列表
uploaded_chunks = await UploadChunk.filter(
upload_id=upload_id, completed=True
).values_list("chunk_index", flat=True)
return APIResponse(
detail={
"upload_id": upload_id,
"file_name": chunk_info.file_name,
"file_size": chunk_info.file_size,
"chunk_size": chunk_info.chunk_size,
"total_chunks": chunk_info.total_chunks,
"uploaded_chunks": list(uploaded_chunks),
"progress": len(uploaded_chunks) / chunk_info.total_chunks * 100,
}
)
@chunk_api.post(
"/upload/complete/{upload_id}", dependencies=[Depends(share_required_login)]
)
async def complete_upload(
upload_id: str,
data: CompleteUploadModel = Depends(parse_complete_upload),
ip: str = Depends(ip_limit["upload"]),
):
if is_cancelling(upload_id):
raise HTTPException(409, "上传正在取消")
task = completion_task(upload_id)
if task is None:
info = await UploadChunk.filter(upload_id=upload_id, chunk_index=-1).first()
if is_cancelling(upload_id):
raise HTTPException(409, "上传正在取消")
if info is None or info.storage_mode not in {"webdav-offset", "webdav-stream", "clouddrive-native"}:
return await _complete_upload(upload_id, data, ip)
task = completion_task(upload_id)
if task is None:
task = start_completion(upload_id, _complete_upload(upload_id, data, ip))
if not task.done():
return APIResponse(code=202, detail={"upload_id":upload_id,"processing":True})
return take_completion(upload_id).result()
@serialize_upload
async def _complete_upload(upload_id, data, ip):
published = await FileCodes.filter(upload_id=upload_id, is_trashed=False).first()
if published:
storage = storages[settings.file_storage]()
await storage.clean_chunks(upload_id, await published.get_file_path())
await UploadChunk.filter(upload_id=upload_id).delete()
await release_storage(f"chunk:{upload_id}")
return APIResponse(detail={"code":published.code,"name":published.prefix+published.suffix})
# 获取上传基本信息
chunk_info = await UploadChunk.filter(upload_id=upload_id, chunk_index=-1).first()
if not chunk_info:
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="上传会话不存在")
validate_expire_style(data.expire_style)
await reserve_storage(
f"chunk:{upload_id}",
chunk_info.file_size,
ttl_seconds=max(1, int(getattr(settings, "chunk_expire_hours", 24))) * 3600,
)
storage = storages[settings.file_storage]()
# 验证所有分片
completed_chunks_list = await UploadChunk.filter(
upload_id=upload_id, completed=True
).all()
if len(completed_chunks_list) != chunk_info.total_chunks:
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail="分片不完整")
# Each newly uploaded chunk has an exact-length check, including the last.
max_total_size = chunk_info.file_size
if max_total_size > settings.uploadSize:
save_path = chunk_info.save_path
if save_path:
try:
await storage.clean_chunks(upload_id, save_path)
except Exception:
if chunk_info.storage_mode in {"webdav-offset", "webdav-stream", "clouddrive-native"}:
raise
pass
await UploadChunk.filter(upload_id=upload_id).delete()
await release_storage(f"chunk:{upload_id}")
max_size_mb = settings.uploadSize / (1024 * 1024)
raise HTTPException(
status_code=403, detail=f"实际上传大小超过限制,最大为 {max_size_mb:.2f} MB"
)
save_path = chunk_info.save_path
path = os.path.dirname(save_path) if save_path else ""
safe_file_name = os.path.basename(save_path) if save_path else ""
prefix, suffix = os.path.splitext(safe_file_name)
try:
# 合并文件并计算哈希
_, file_hash = await storage.merge_chunks(upload_id, chunk_info, save_path)
# 创建文件记录
expired_at, expired_count, used_count, code = await get_expire_info(
data.expire_value, data.expire_style
)
file_code = await FileCodes.create(
code=code,
file_hash=file_hash, # 使用合并后计算的哈希
is_chunked=True,
upload_id=upload_id,
size=chunk_info.file_size,
expired_at=expired_at,
expired_count=expired_count,
used_count=used_count,
file_path=path,
uuid_file_name=safe_file_name,
prefix=prefix,
suffix=suffix,
)
await record_transfer_stat("upload", file_code, chunk_info.file_size, ip)
# 清理临时文件
await storage.clean_chunks(upload_id, save_path)
# 清理数据库中的分片记录
await UploadChunk.filter(upload_id=upload_id).delete()
await release_storage(f"chunk:{upload_id}")
ip_limit["upload"].add_ip(ip)
return APIResponse(detail={"code": code, "name": safe_file_name})
except ValueError as e:
raise HTTPException(status.HTTP_400_BAD_REQUEST, detail=str(e))
except HTTPException:
raise
except Exception as e:
if chunk_info.storage_mode in {"webdav-offset", "webdav-stream", "clouddrive-native"}:
raise HTTPException(500, "上传完成状态保存失败,请重试完成上传") from e
# 合并失败时清理临时文件
try:
await storage.clean_chunks(upload_id, save_path)
except Exception:
pass
raise HTTPException(
status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"文件合并失败: {str(e)}"
)
# ============ 预签名上传API ============
presign_api = APIRouter(prefix="/presign", tags=["预签名上传"])
PRESIGN_SESSION_EXPIRES = 900 # 15分钟
def build_proxy_upload_urls(upload_id: str) -> dict:
proxy_upload_url = f"/presign/upload/proxy/{upload_id}"
return {
"proxy_upload_url": proxy_upload_url,
"legacy_proxy_upload_url": f"/api{proxy_upload_url}",
}
async def _get_valid_session(
upload_id: str, expected_mode: Optional[str] = None
) -> PresignUploadSession:
"""获取并验证会话"""
session = await PresignUploadSession.filter(upload_id=upload_id).first()
if not session:
raise HTTPException(404, "上传会话不存在")
if session.mode == "cancelled":
raise HTTPException(409, "上传已取消,等待后台清理")
if await session.is_expired():
await session.delete()
await release_storage(f"presign:{upload_id}")
raise HTTPException(404, "上传会话已过期")
if expected_mode and session.mode != expected_mode:
raise HTTPException(400, f"此会话不支持{expected_mode}模式")
return session
@presign_api.post("/upload/init", dependencies=[Depends(share_required_login)])
async def presign_upload_init(
data: PresignUploadInitRequest, ip: str = Depends(ip_limit["upload"])
):
"""初始化预签名上传,S3返回直传URL,其他存储返回代理URL"""
validate_file_type(data.file_name)
if data.file_size > settings.uploadSize:
raise HTTPException(
403,
f"文件大小超过限制,最大为 {settings.uploadSize / (1024 * 1024):.2f} MB",
)
validate_expire_style(data.expire_style)
upload_id = uuid.uuid4().hex
reservation_token = f"presign:{upload_id}"
await reserve_storage(
reservation_token, data.file_size, ttl_seconds=PRESIGN_SESSION_EXPIRES
)
try:
path, _, _, filename, save_path = await FileUploadService.generate_file_path(
data.file_name, upload_id
)
storage: FileStorageInterface = storages[settings.file_storage]()
presigned_url = await storage.generate_presigned_upload_url(
save_path, PRESIGN_SESSION_EXPIRES
)
mode = "direct" if presigned_url else "proxy"
proxy_urls = build_proxy_upload_urls(upload_id)
upload_url = presigned_url or proxy_urls["proxy_upload_url"]
await PresignUploadSession.create(
upload_id=upload_id,
file_name=filename,
file_size=data.file_size,
save_path=save_path,
mode=mode,
expire_value=data.expire_value,
expire_style=data.expire_style,
expires_at=await get_now() + timedelta(seconds=PRESIGN_SESSION_EXPIRES),
)
except Exception:
await release_storage(reservation_token)
raise
ip_limit["upload"].add_ip(ip)
detail = {
"upload_id": upload_id,
"upload_url": upload_url,
"mode": mode,
"expires_in": PRESIGN_SESSION_EXPIRES,
}
if mode == "proxy":
detail.update(proxy_urls)
return APIResponse(
detail=detail
)
@presign_api.put(
"/upload/proxy/{upload_id}", dependencies=[Depends(share_required_login)]
)
@serialize_upload
async def presign_upload_proxy(
upload_id: str, file: UploadFile = File(...), ip: str = Depends(ip_limit["upload"])
):
"""代理模式上传,服务器转存到存储后端"""
session = await _get_valid_session(upload_id, expected_mode="proxy")
await reserve_storage(
f"presign:{upload_id}",
session.file_size,
ttl_seconds=PRESIGN_SESSION_EXPIRES,
)
file_size = await validate_file_size(file, settings.uploadSize)
await validate_upload_file(file)
if abs(file_size - session.file_size) > 1024:
raise HTTPException(400, "文件大小与声明不符")
storage: FileStorageInterface = storages[settings.file_storage]()
try:
await storage.save_file(file, session.save_path)
except Exception as e:
raise HTTPException(500, f"文件保存失败: {str(e)}")
if is_cancelling(upload_id):
raise HTTPException(409, "上传正在取消")
try:
code = await FileUploadService.create_file_record(
session.file_name,
file_size,
os.path.dirname(session.save_path),
session.expire_value,
session.expire_style,
ip=ip,
upload_id=upload_id,
)
except Exception:
try:
await storage.delete_file(
FileCodes(
file_path=os.path.dirname(session.save_path),
uuid_file_name=os.path.basename(session.save_path),
)
)
except Exception:
pass
raise
await session.delete()
await release_storage(f"presign:{upload_id}")
ip_limit["upload"].add_ip(ip)
return APIResponse(detail={"code": code, "name": session.file_name})
@presign_api.post(
"/upload/confirm/{upload_id}", dependencies=[Depends(share_required_login)]
)
@serialize_upload
async def presign_upload_confirm(upload_id: str, ip: str = Depends(ip_limit["upload"])):
"""直传确认,客户端完成S3直传后调用获取分享码"""
session = await _get_valid_session(upload_id, expected_mode="direct")
try:
await reserve_storage(
f"presign:{upload_id}",
session.file_size,
ttl_seconds=PRESIGN_SESSION_EXPIRES,
)
except HTTPException:
storage: FileStorageInterface = storages[settings.file_storage]()
try:
if await storage.file_exists(session.save_path):
await storage.delete_file(
FileCodes(
file_path=os.path.dirname(session.save_path),
uuid_file_name=os.path.basename(session.save_path),
)
)
finally:
await session.delete()
await release_storage(f"presign:{upload_id}")
raise
storage: FileStorageInterface = storages[settings.file_storage]()
if not await storage.file_exists(session.save_path):
raise HTTPException(404, "文件未上传或上传失败")
if is_cancelling(upload_id):
raise HTTPException(409, "上传正在取消")
try:
code = await FileUploadService.create_file_record(
session.file_name,
session.file_size,
os.path.dirname(session.save_path),
session.expire_value,
session.expire_style,
ip=ip,
upload_id=upload_id,
)
except Exception:
try:
await storage.delete_file(
FileCodes(
file_path=os.path.dirname(session.save_path),
uuid_file_name=os.path.basename(session.save_path),
)
)
except Exception:
pass
raise
await session.delete()
await release_storage(f"presign:{upload_id}")
ip_limit["upload"].add_ip(ip)
return APIResponse(detail={"code": code, "name": session.file_name})
@presign_api.get(
"/upload/status/{upload_id}", dependencies=[Depends(share_required_login)]
)
async def presign_upload_status(upload_id: str):
"""查询上传会话状态"""
session = await PresignUploadSession.filter(upload_id=upload_id).first()
if not session:
raise HTTPException(404, "上传会话不存在")
return APIResponse(
detail={
"upload_id": session.upload_id,
"file_name": session.file_name,
"file_size": session.file_size,
"mode": session.mode,
"created_at": session.created_at.isoformat(),
"expires_at": session.expires_at.isoformat(),
"is_expired": await session.is_expired(),
}
)
@presign_api.delete("/upload/{upload_id}", dependencies=[Depends(share_required_login)])
async def presign_upload_cancel(upload_id: str):
if is_cancelling(upload_id):
raise HTTPException(409, "上传正在取消,请稍后重试")
begin_cancel(upload_id)
try:
return await _presign_upload_cancel(upload_id)
finally:
end_cancel(upload_id)
@serialize_upload
async def _presign_upload_cancel(upload_id):
session = await PresignUploadSession.filter(upload_id=upload_id).first()
published = await FileCodes.filter(upload_id=upload_id).first()
if published:
if session: await session.delete()
await release_storage(f"presign:{upload_id}")
return APIResponse(detail={"completed":True,"code":published.code,"name":published.prefix+published.suffix})
if not session:
return APIResponse(detail={"cancelled":True})
storage = storages[settings.file_storage]()
if session.mode in {"direct", "cancelled"}:
# Browser abort cannot revoke a pre-signed PUT already committing remotely.
# Keep a durable tombstone and quota until the signed URL expires.
session.mode = "cancelled"
await session.save(update_fields=["mode"])
return APIResponse(detail={"cancelled":True,"cleanup_pending":True,"message":"传输已中断,临时文件将在上传授权到期后自动清理"})
if await storage.file_exists(session.save_path):
await storage.delete_file(FileCodes(file_path=os.path.dirname(session.save_path),uuid_file_name=os.path.basename(session.save_path)))
await session.delete()
await release_storage(f"presign:{upload_id}")
return APIResponse(detail={"cancelled":True,"message":"上传已取消"})