✨ feat: 完善下载检测与查件生命周期
* 提供有鉴权和并发限制的轻量下载检测,不消耗次数或生成取件事件 * 支持保存检测间隔,并校验关闭选项和有效范围 * 将成功查件记录按时间并入生命周期,与实际发起取件事件区分 * 统一签名下载入口,文本与直链分享仅在下载时计数并保留直链传输 * 处理检测取消时的连接释放,补充鉴权、存储和生命周期集成验证 * 记录文件恢复和测试验收,明确先推送代码再部署测试环境
This commit is contained in:
1 parent
32b31ac271
commit
69cfa436de
11 files changed
+555
-39
No files matched your search
+25
-36
@@ -11,7 +11,7 @@ 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
|
||||
from starlette.responses import Response, RedirectResponse
|
||||
from tortoise.expressions import Case, F, Q, When
|
||||
|
||||
from apps.admin.dependencies import share_required_login
|
||||
@@ -244,7 +244,7 @@ async def get_code_file_by_code(
|
||||
file_code = await FileCodes.filter(code=normalized_code, is_trashed=False).first()
|
||||
if not file_code:
|
||||
return False, "文件不存在"
|
||||
if await file_code.is_expired() and check:
|
||||
if check and (file_code.expired_count == 0 or await file_code.is_expired()):
|
||||
return False, "文件已过期"
|
||||
return True, file_code
|
||||
|
||||
@@ -290,17 +290,30 @@ def build_file_metadata(file_code: FileCodes) -> dict:
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
if file_code.text is not None:
|
||||
download_url = None
|
||||
elif file_code.expired_count >= 0 or is_local_ref(file_code):
|
||||
# 有次数限制的文件必须经过下载接口,第三方直链无法阻止重复使用。
|
||||
download_url = await get_proxy_file_url(file_code.code)
|
||||
else:
|
||||
download_url = await file_storage.get_file_url(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,
|
||||
@@ -348,19 +361,7 @@ async def get_code_file(code: str, ip: str = Depends(ip_limit["error"])):
|
||||
if not await consume_file_usage(file_code):
|
||||
return APIResponse(code=404, detail="文件已过期")
|
||||
await record_transfer_stat("download", file_code, file_code.size, ip)
|
||||
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='')}"
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
return (build_local_ref_download(file_code) if is_local_ref(file_code) else await file_storage.get_file_response(file_code))
|
||||
return await build_download_response(file_code, file_storage)
|
||||
|
||||
|
||||
@share_api.post("/select/")
|
||||
@@ -373,15 +374,7 @@ async def select_file(data: SelectFileModel, ip: str = Depends(ip_limit["error"]
|
||||
|
||||
assert isinstance(file_code, FileCodes)
|
||||
detail = await build_select_detail(file_code, file_storage)
|
||||
download_url = detail.get("download_url")
|
||||
consumes_on_download = isinstance(download_url, str) and download_url.startswith(
|
||||
"/share/download?"
|
||||
)
|
||||
if not consumes_on_download:
|
||||
if not await consume_file_usage(file_code):
|
||||
return APIResponse(code=404, detail="文件已过期")
|
||||
await record_transfer_stat("download", file_code, file_code.size, ip)
|
||||
detail.update(build_file_metadata(file_code))
|
||||
await record_transfer_stat("lookup", file_code, 0, ip)
|
||||
return APIResponse(detail=detail)
|
||||
|
||||
|
||||
@@ -406,11 +399,7 @@ async def download_file(key: str, code: str, ip: str = Depends(ip_limit["error"]
|
||||
return APIResponse(code=404, detail="文件已过期")
|
||||
await record_transfer_stat("download", file_code, file_code.size, ip)
|
||||
|
||||
return (
|
||||
APIResponse(detail=file_code.text)
|
||||
if file_code.text
|
||||
else (build_local_ref_download(file_code) if is_local_ref(file_code) else await file_storage.get_file_response(file_code))
|
||||
)
|
||||
return await build_download_response(file_code, file_storage)
|
||||
|
||||
|
||||
chunk_api = APIRouter(prefix="/chunk", tags=["切片"])
|
||||
|
||||
Reference in new issue
Block a user