phabricator: add the uploadchunks function
authorIan Moody <moz-ian@perix.co.uk>
Sun, 06 Oct 2019 15:04:54 +0100
changeset 43187 453079605242
parent 43186 f742fabad507
child 43188 24e8aac7c630
phabricator: add the uploadchunks function Differential Revision: https://phab.mercurial-scm.org/D7046
hgext/phabricator.py
--- a/hgext/phabricator.py	Sun Oct 06 14:54:46 2019 +0100
+++ b/hgext/phabricator.py	Sun Oct 06 15:04:54 2019 +0100
@@ -41,6 +41,7 @@
 
 from __future__ import absolute_import
 
+import base64
 import contextlib
 import itertools
 import json
@@ -579,6 +580,34 @@
         )
 
 
+def uploadchunks(fctx, fphid):
+    """upload large binary files as separate chunks.
+    Phab requests chunking over 8MiB, and splits into 4MiB chunks
+    """
+    ui = fctx.repo().ui
+    chunks = callconduit(ui, b'file.querychunks', {b'filePHID': fphid})
+    progress = ui.makeprogress(
+        _(b'uploading file chunks'), unit=_(b'chunks'), total=len(chunks)
+    )
+    for chunk in chunks:
+        progress.increment()
+        if chunk[b'complete']:
+            continue
+        bstart = int(chunk[b'byteStart'])
+        bend = int(chunk[b'byteEnd'])
+        callconduit(
+            ui,
+            b'file.uploadchunk',
+            {
+                b'filePHID': fphid,
+                b'byteStart': bstart,
+                b'data': base64.b64encode(fctx.data()[bstart:bend]),
+                b'dataEncoding': b'base64',
+            },
+        )
+    progress.complete()
+
+
 def creatediff(ctx):
     """create a Differential Diff"""
     repo = ctx.repo()