Patchwork D7047: phabricator: add the uploadfile function

login
register
mail settings
Submitter phabricator
Date Oct. 11, 2019, 5:55 p.m.
Message ID <4c1cc6b17e63ffe8fb1aac85dcb845e0@localhost.localdomain>
Download mbox | patch
Permalink /patch/42230/
State Not Applicable
Headers show

Comments

phabricator - Oct. 11, 2019, 5:55 p.m.
Kwan added a comment.
Kwan updated this revision to Diff 17078.


  Fix some test-check-code issues, and one test-check-pyflakes unused local.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D7047?vs=17053&id=17078

BRANCH
  default

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7047/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7047

AFFECTED FILES
  hgext/phabricator.py

CHANGE DETAILS




To: Kwan, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/hgext/phabricator.py b/hgext/phabricator.py
--- a/hgext/phabricator.py
+++ b/hgext/phabricator.py
@@ -43,6 +43,7 @@ 
 
 import base64
 import contextlib
+import hashlib
 import itertools
 import json
 import operator
@@ -608,6 +609,43 @@ 
     progress.complete()
 
 
+def uploadfile(fctx):
+    """upload binary files to Phabricator"""
+    repo = fctx.repo()
+    ui = repo.ui
+    fname = fctx.path()
+    size = fctx.size()
+    fhash = pycompat.bytestr(hashlib.sha256(fctx.data()).hexdigest())
+
+    # an allocate call is required first to see if an upload is even required
+    # (Phab might already have it) and to determine if chunking is needed
+    allocateparams = {
+        b'name': fname,
+        b'contentLength': size,
+        b'contentHash': fhash,
+    }
+    filealloc = callconduit(ui, b'file.allocate', allocateparams)
+    fphid = filealloc[b'filePHID']
+
+    if filealloc[b'upload']:
+        ui.write(_(b'uploading %s\n') % bytes(fctx))
+        if not fphid:
+            uploadparams = {
+                b'name': fname,
+                b'data_base64': base64.b64encode(fctx.data()),
+            }
+            fphid = callconduit(ui, b'file.upload', uploadparams)
+        else:
+            uploadchunks(fctx, fphid)
+    else:
+        ui.debug(b'server already has %s\n' % bytes(fctx))
+
+    if not fphid:
+        raise error.Abort(b'Upload of %s failed.' % bytes(fctx))
+
+    return fphid
+
+
 def creatediff(ctx):
     """create a Differential Diff"""
     repo = ctx.repo()