storage_service.py 743 B

12345678910111213141516171819202122232425
  1. from __future__ import annotations
  2. from app.core.config import settings
  3. def document_storage_statistics() -> dict[str, int | float]:
  4. root = settings.upload_path
  5. used_bytes = 0
  6. if root.exists():
  7. for path in root.rglob("*"):
  8. try:
  9. if path.is_file():
  10. used_bytes += path.stat().st_size
  11. except OSError:
  12. continue
  13. capacity_bytes = max(
  14. 1,
  15. int(float(settings.storage_capacity_gb) * 1024**3),
  16. )
  17. usage_percent = min(used_bytes / capacity_bytes * 100, 100.0)
  18. return {
  19. "storage_used_bytes": used_bytes,
  20. "storage_capacity_bytes": capacity_bytes,
  21. "storage_usage_percent": round(usage_percent, 1),
  22. }