]> git.ipfire.org Git - pakfire.git/commitdiff
hub: Show progress bar on file uploads
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 May 2022 09:57:02 +0000 (09:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 May 2022 09:57:02 +0000 (09:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/hub.py

index 0b5c689e100bbe0d6b6fb77a4de35b4ac5692347..e47a1db88d385be931ac14e7d0bc0266b3ca115d 100644 (file)
@@ -27,7 +27,9 @@ import os.path
 import tornado.httpclient
 import urllib.parse
 
+from . import _pakfire
 from .constants import *
+from .i18n import _
 
 # Setup logging
 log = logging.getLogger("pakfire.hub")
@@ -131,7 +133,7 @@ class Hub(object):
 
        # File uploads
 
-       async def upload(self, path, filename=None):
+       async def upload(self, path, filename=None, show_progress=True):
                """
                        Uploads the file to the hub returning the upload ID
                """
@@ -144,11 +146,24 @@ class Hub(object):
                # Determine the filesize
                size = os.path.getsize(path)
 
+               # Make progressbar
+               if show_progress:
+                       p = _pakfire.Progressbar()
+                       p.add_string(_("Uploading %s") % filename)
+                       p.add_percentage()
+                       p.add_bar()
+                       p.add_transfer_speed()
+                       p.add_string("|")
+                       p.add_bytes_transferred()
+                       p.add_eta()
+               else:
+                       p = None
+
                # Compute a digest
                digest = self._compute_digest("blake2b", path)
 
                # Prepare the file for streaming
-               body_producer = functools.partial(self._stream_file, path)
+               body_producer = functools.partial(self._stream_file, path, size, p)
 
                # Perform upload
                response = await self._request("PUT", "/upload",
@@ -160,14 +175,28 @@ class Hub(object):
                return response.get("id")
 
        @staticmethod
-       def _stream_file(path, write):
-               with open(path, "rb") as f:
-                       while True:
-                               buf = f.read(64 * 1024)
-                               if not buf:
-                                       break
-
-                               write(buf)
+       def _stream_file(path, size, p, write):
+               # Start the progressbar
+               if p:
+                       p.start(size)
+
+               try:
+                       with open(path, "rb") as f:
+                               while True:
+                                       buf = f.read(64 * 1024)
+                                       if not buf:
+                                               break
+
+                                       # Update progressbar
+                                       if p:
+                                               l = len(buf)
+                                               p.increment(l)
+
+                                       write(buf)
+               finally:
+                       # Finish the progressbar
+                       if p:
+                               p.finish()
 
        @staticmethod
        def _compute_digest(algo, path):