2026-07-11 17:50:26 +08:00
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
from fastapi import HTTPException
|
|
|
|
|
|
from tortoise import connections
|
2026-09-16 18:31:00 +08:00
|
|
|
|
from tortoise.expressions import Q
|
2026-09-09 19:04:12 +08:00
|
|
|
|
from tortoise.functions import Sum
|
2026-07-11 17:50:26 +08:00
|
|
|
|
|
|
|
|
|
|
from apps.base.models import FileCodes, StorageReservation
|
2026-09-16 18:31:00 +08:00
|
|
|
|
from apps.base.local_share import LOCAL_REF_MARKER
|
2026-07-11 17:50:26 +08:00
|
|
|
|
from core.settings import settings
|
|
|
|
|
|
from core.utils import get_now
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-09-16 18:31:00 +08:00
|
|
|
|
def owned_storage_queryset(queryset=None):
|
|
|
|
|
|
"""FileCodes that occupy FileCodeBox storage (exclude NAS local-ref rows).
|
|
|
|
|
|
|
|
|
|
|
|
SQL ``file_path != 'local-ref'`` drops NULLs, so keep null paths explicitly.
|
|
|
|
|
|
"""
|
|
|
|
|
|
qs = FileCodes.all() if queryset is None else queryset
|
|
|
|
|
|
return qs.filter(Q(file_path__isnull=True) | ~Q(file_path=LOCAL_REF_MARKER))
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-07-23 19:38:50 +08:00
|
|
|
|
def _detect_sql_dialect() -> str:
|
|
|
|
|
|
"""识别当前默认连接的 SQL 方言。
|
|
|
|
|
|
|
|
|
|
|
|
优先使用 Tortoise capabilities.dialect;
|
|
|
|
|
|
回退到 db_config 中的 engine 路径字符串。
|
|
|
|
|
|
"""
|
|
|
|
|
|
conn = connections.get("default")
|
|
|
|
|
|
dialect = getattr(getattr(conn, "capabilities", None), "dialect", "") or ""
|
|
|
|
|
|
if dialect:
|
|
|
|
|
|
return dialect.lower()
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
engine = str(connections.db_config.get("default", {}).get("engine", "")).lower()
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
engine = ""
|
|
|
|
|
|
if "postgres" in engine or "asyncpg" in engine or "psycopg" in engine:
|
|
|
|
|
|
return "postgres"
|
|
|
|
|
|
if "mysql" in engine:
|
|
|
|
|
|
return "mysql"
|
|
|
|
|
|
if "sqlite" in engine:
|
|
|
|
|
|
return "sqlite"
|
|
|
|
|
|
return "sqlite"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _sql_placeholders(count: int) -> list[str]:
|
|
|
|
|
|
"""根据数据库方言生成参数占位符(多数据库兼容)。
|
|
|
|
|
|
|
|
|
|
|
|
SQLite 用 ?,PostgreSQL 用 $1/$2/...,MySQL 用 %s。
|
|
|
|
|
|
"""
|
|
|
|
|
|
dialect = _detect_sql_dialect()
|
|
|
|
|
|
if dialect in {"postgres", "postgresql"}:
|
|
|
|
|
|
return [f"${i}" for i in range(1, count + 1)]
|
|
|
|
|
|
if dialect == "mysql":
|
|
|
|
|
|
return ["%s"] * count
|
|
|
|
|
|
return ["?"] * count
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-07-11 17:50:26 +08:00
|
|
|
|
def get_storage_limit() -> int:
|
|
|
|
|
|
try:
|
2026-09-09 18:09:02 +08:00
|
|
|
|
return max(0, int(getattr(settings, "storage_limit", 0)))
|
2026-07-11 17:50:26 +08:00
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_storage_usage() -> dict[str, int | None]:
|
|
|
|
|
|
now = await get_now()
|
2026-09-09 19:04:12 +08:00
|
|
|
|
# SQL 聚合:此函数在每次上传配额检查时调用,禁止全表拉取(D3)
|
2026-09-16 18:31:00 +08:00
|
|
|
|
used_rows = await owned_storage_queryset().annotate(total=Sum("size")).values("total")
|
2026-09-09 19:04:12 +08:00
|
|
|
|
reserved_rows = await StorageReservation.filter(expires_at__gt=now).annotate(
|
|
|
|
|
|
total=Sum("size")
|
|
|
|
|
|
).values("total")
|
2026-07-11 17:50:26 +08:00
|
|
|
|
limit = get_storage_limit()
|
2026-09-09 19:04:12 +08:00
|
|
|
|
used_bytes = used_rows[0]["total"] or 0
|
|
|
|
|
|
reserved_bytes = reserved_rows[0]["total"] or 0
|
2026-07-11 17:50:26 +08:00
|
|
|
|
return {
|
|
|
|
|
|
"limit": limit,
|
|
|
|
|
|
"used": used_bytes,
|
|
|
|
|
|
"reserved": reserved_bytes,
|
|
|
|
|
|
"available": max(0, limit - used_bytes - reserved_bytes) if limit else None,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def reserve_storage(token: str, size: int, ttl_seconds: int) -> None:
|
|
|
|
|
|
requested_size = max(0, int(size))
|
|
|
|
|
|
limit = get_storage_limit()
|
|
|
|
|
|
if not limit or requested_size == 0:
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
now = await get_now()
|
|
|
|
|
|
expires_at = now + datetime.timedelta(seconds=ttl_seconds)
|
|
|
|
|
|
conn = connections.get("default")
|
|
|
|
|
|
await StorageReservation.filter(token=token, expires_at__lte=now).delete()
|
|
|
|
|
|
existing = await StorageReservation.filter(token=token, expires_at__gt=now).first()
|
|
|
|
|
|
if existing:
|
|
|
|
|
|
if existing.size == requested_size:
|
|
|
|
|
|
return
|
|
|
|
|
|
raise HTTPException(status_code=409, detail="上传容量预留信息不一致")
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
2026-07-23 19:38:50 +08:00
|
|
|
|
ph = _sql_placeholders(6)
|
2026-07-11 17:50:26 +08:00
|
|
|
|
affected, _ = await conn.execute_query(
|
2026-07-23 19:38:50 +08:00
|
|
|
|
f"""
|
2026-07-11 17:50:26 +08:00
|
|
|
|
INSERT INTO storagereservation (token, size, expires_at)
|
2026-07-23 19:38:50 +08:00
|
|
|
|
SELECT {ph[0]}, {ph[1]}, {ph[2]}
|
2026-07-11 17:50:26 +08:00
|
|
|
|
WHERE (
|
2026-09-16 18:31:00 +08:00
|
|
|
|
COALESCE((SELECT SUM(size) FROM filecodes WHERE file_path IS NULL OR file_path != '{LOCAL_REF_MARKER}'), 0)
|
2026-07-23 19:38:50 +08:00
|
|
|
|
+ COALESCE((SELECT SUM(size) FROM storagereservation WHERE expires_at > {ph[3]}), 0)
|
|
|
|
|
|
+ {ph[4]}
|
|
|
|
|
|
) <= {ph[5]}
|
2026-07-11 17:50:26 +08:00
|
|
|
|
""",
|
|
|
|
|
|
[token, requested_size, expires_at, now, requested_size, limit],
|
|
|
|
|
|
)
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
concurrent = await StorageReservation.filter(
|
|
|
|
|
|
token=token, size=requested_size, expires_at__gt=now
|
|
|
|
|
|
).exists()
|
|
|
|
|
|
if concurrent:
|
|
|
|
|
|
return
|
|
|
|
|
|
raise
|
|
|
|
|
|
if affected != 1:
|
|
|
|
|
|
concurrent = await StorageReservation.filter(
|
|
|
|
|
|
token=token, size=requested_size, expires_at__gt=now
|
|
|
|
|
|
).exists()
|
|
|
|
|
|
if concurrent:
|
|
|
|
|
|
return
|
|
|
|
|
|
raise HTTPException(status_code=507, detail="存储空间已达到管理员设置的容量上限")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def release_storage(token: str) -> None:
|
|
|
|
|
|
await StorageReservation.filter(token=token).delete()
|