Coverage for seedboxsync/core/sync/download_progress.py: 100%
16 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-26 17:46 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-26 17:46 +0000
1#
2# Copyright (C) 2015-2026 Guillaume Kulakowski <guillaume@kulakowski.fr>
3#
4# For the full copyright and license information, please view the LICENSE
5# file that was distributed with this source code.
6#
7"""Callback for tracking and persisting download progress."""
9from seedboxsync.core import current_app
10from seedboxsync.core.dao import Download
11from seedboxsync.core.taskmanager import heartbeat
14class DownloadProgress:
15 """Track download progress and update its database record."""
17 def __init__(self, download: Download) -> None:
18 """
19 Initialize the download progress tracker.
21 Args:
22 download: The download record to update.
23 """
24 self.app = current_app
25 self._download = download
26 self._last_saved_size = 0
28 def __call__(self, transferred: int, total: int) -> None:
29 """
30 Update the download progress and persist it when appropriate.
32 Args:
33 transferred: Number of bytes transferred so far.
34 total: Total number of bytes to transfer.
35 """
36 # Persist progress every 100 MiB and when the download completes.
37 if transferred == total or transferred - self._last_saved_size >= 100 * 1024 * 1024:
38 heartbeat() # Call heartbeat
39 self._download.local_size = transferred
40 percent = (transferred / total) * 100 if total > 0 else 0
41 self.app.logger.debug(f"Download progress: {transferred} / {total} ({percent:.2f}%)")
42 self._download.save()
43 self._last_saved_size = transferred