~ubuntu-branches/ubuntu/vivid/phabricator/vivid-proposed

« back to all changes in this revision

Viewing changes to arcanist/src/workflow/ArcanistUploadWorkflow.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2014-10-23 20:49:26 UTC
  • mfrom: (0.2.1) (0.1.1)
  • Revision ID: package-import@ubuntu.com-20141023204926-vq80u1op4df44azb
Tags: 0~git20141023-1
Initial release (closes: #703046)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * Upload a file to Phabricator.
 
5
 */
 
6
final class ArcanistUploadWorkflow extends ArcanistWorkflow {
 
7
 
 
8
  private $paths;
 
9
  private $json;
 
10
 
 
11
  public function getWorkflowName() {
 
12
    return 'upload';
 
13
  }
 
14
 
 
15
  public function getCommandSynopses() {
 
16
    return phutil_console_format(<<<EOTEXT
 
17
      **upload** __file__ [__file__ ...] [--json]
 
18
EOTEXT
 
19
      );
 
20
  }
 
21
 
 
22
  public function getCommandHelp() {
 
23
    return phutil_console_format(<<<EOTEXT
 
24
          Supports: filesystems
 
25
          Upload a file from local disk.
 
26
EOTEXT
 
27
      );
 
28
  }
 
29
 
 
30
  public function getArguments() {
 
31
    return array(
 
32
      'json' => array(
 
33
        'help' => 'Output upload information in JSON format.',
 
34
      ),
 
35
      '*' => 'paths',
 
36
    );
 
37
  }
 
38
 
 
39
  protected function didParseArguments() {
 
40
    if (!$this->getArgument('paths')) {
 
41
      throw new ArcanistUsageException('Specify one or more files to upload.');
 
42
    }
 
43
 
 
44
    $this->paths = $this->getArgument('paths');
 
45
    $this->json = $this->getArgument('json');
 
46
  }
 
47
 
 
48
  public function requiresAuthentication() {
 
49
    return true;
 
50
  }
 
51
 
 
52
  private function getPaths() {
 
53
    return $this->paths;
 
54
  }
 
55
 
 
56
  private function getJSON() {
 
57
    return $this->json;
 
58
  }
 
59
 
 
60
  public function run() {
 
61
    $conduit = $this->getConduit();
 
62
 
 
63
    $results = array();
 
64
 
 
65
    foreach ($this->paths as $path) {
 
66
      $name = basename($path);
 
67
      $this->writeStatusMessage("Uploading '{$name}'...\n");
 
68
      try {
 
69
        $data = Filesystem::readFile($path);
 
70
      } catch (FilesystemException $ex) {
 
71
        $this->writeStatusMessage(
 
72
          "Unable to upload file: ".$ex->getMessage()."\n");
 
73
        $results[$path] = null;
 
74
        continue;
 
75
      }
 
76
 
 
77
      $phid = $conduit->callMethodSynchronous(
 
78
        'file.upload',
 
79
        array(
 
80
          'data_base64' => base64_encode($data),
 
81
          'name'        => $name,
 
82
        ));
 
83
      $info = $conduit->callMethodSynchronous(
 
84
        'file.info',
 
85
        array(
 
86
          'phid'        => $phid,
 
87
        ));
 
88
 
 
89
      $results[$path] = $info;
 
90
 
 
91
      if (!$this->getJSON()) {
 
92
        $id = $info['id'];
 
93
        echo "  F{$id} {$name}: ".$info['uri']."\n\n";
 
94
      }
 
95
    }
 
96
 
 
97
    if ($this->getJSON()) {
 
98
      echo json_encode($results)."\n";
 
99
    } else {
 
100
      $this->writeStatusMessage("Done.\n");
 
101
    }
 
102
 
 
103
    return 0;
 
104
  }
 
105
 
 
106
}