v1
This commit is contained in:
@@ -56,6 +56,53 @@ def get_company_upload_bytes(upload_root: str, company_id: int | None) -> int:
|
||||
return int(total)
|
||||
|
||||
|
||||
def compute_storage_usage(*, used_bytes: int, max_bytes: int | None):
|
||||
"""Compute storage usage info.
|
||||
|
||||
Args:
|
||||
used_bytes: current usage
|
||||
max_bytes: quota; if None or <=0: unlimited
|
||||
|
||||
Returns dict:
|
||||
{
|
||||
"max_bytes": int|None,
|
||||
"used_bytes": int,
|
||||
"is_limited": bool,
|
||||
"is_exceeded": bool,
|
||||
"used_ratio": float|None, # 0..1 when limited
|
||||
"used_percent": int|None, # rounded percent when limited
|
||||
"remaining_bytes": int|None,
|
||||
}
|
||||
"""
|
||||
|
||||
used = max(0, int(used_bytes or 0))
|
||||
mx = None if max_bytes is None else int(max_bytes)
|
||||
if mx is None or mx <= 0:
|
||||
return {
|
||||
"max_bytes": None,
|
||||
"used_bytes": used,
|
||||
"is_limited": False,
|
||||
"is_exceeded": False,
|
||||
"used_ratio": None,
|
||||
"used_percent": None,
|
||||
"remaining_bytes": None,
|
||||
}
|
||||
|
||||
ratio = used / mx if mx > 0 else 1.0
|
||||
percent = int(round(ratio * 100.0))
|
||||
return {
|
||||
"max_bytes": mx,
|
||||
"used_bytes": used,
|
||||
"is_limited": True,
|
||||
"is_exceeded": used >= mx,
|
||||
# Keep percent un-clamped so the UI can show e.g. 132% when exceeded.
|
||||
# Clamp only the ratio (for progress bars, etc.).
|
||||
"used_ratio": max(0.0, min(1.0, ratio)),
|
||||
"used_percent": max(0, percent),
|
||||
"remaining_bytes": max(0, mx - used),
|
||||
}
|
||||
|
||||
|
||||
def is_valid_upload_relpath(file_path: str | None) -> bool:
|
||||
"""True if file_path looks like a path we manage under /static.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user