Changeset View
Changeset View
Standalone View
Standalone View
src/workflow/ArcanistDownloadWorkflow.php
| Show All 28 Lines | |||||
| EOTEXT | EOTEXT | ||||
| ); | ); | ||||
| } | } | ||||
| public function getArguments() { | public function getArguments() { | ||||
| return array( | return array( | ||||
| 'show' => array( | 'show' => array( | ||||
| 'conflicts' => array( | 'conflicts' => array( | ||||
| 'as' => | 'as' => pht( | ||||
| 'Use --show to direct the file to stdout, or --as to direct '. | 'Use %s to direct the file to stdout, or %s to direct '. | ||||
| 'it to a named location.', | 'it to a named location.', | ||||
| '--show', | |||||
| '--as'), | |||||
| ), | ), | ||||
| 'help' => 'Write file to stdout instead of to disk.', | 'help' => pht('Write file to stdout instead of to disk.'), | ||||
| ), | ), | ||||
| 'as' => array( | 'as' => array( | ||||
| 'param' => 'name', | 'param' => 'name', | ||||
| 'help' => 'Save the file with a specific name rather than the default.', | 'help' => pht( | ||||
| 'Save the file with a specific name rather than the default.'), | |||||
| ), | ), | ||||
| '*' => 'argv', | '*' => 'argv', | ||||
| ); | ); | ||||
| } | } | ||||
| protected function didParseArguments() { | protected function didParseArguments() { | ||||
| $argv = $this->getArgument('argv'); | $argv = $this->getArgument('argv'); | ||||
| if (!$argv) { | if (!$argv) { | ||||
| throw new ArcanistUsageException('Specify a file to download.'); | throw new ArcanistUsageException(pht('Specify a file to download.')); | ||||
| } | } | ||||
| if (count($argv) > 1) { | if (count($argv) > 1) { | ||||
| throw new ArcanistUsageException('Specify exactly one file to download.'); | throw new ArcanistUsageException( | ||||
| pht('Specify exactly one file to download.')); | |||||
| } | } | ||||
| $file = reset($argv); | $file = reset($argv); | ||||
| if (!preg_match('/^F?\d+$/', $file)) { | if (!preg_match('/^F?\d+$/', $file)) { | ||||
| throw new ArcanistUsageException('Specify file by ID, e.g. F123.'); | throw new ArcanistUsageException( | ||||
| pht('Specify file by ID, e.g. %s.', 'F123')); | |||||
| } | } | ||||
| $this->id = (int)ltrim($file, 'F'); | $this->id = (int)ltrim($file, 'F'); | ||||
| $this->saveAs = $this->getArgument('as'); | $this->saveAs = $this->getArgument('as'); | ||||
| $this->show = $this->getArgument('show'); | $this->show = $this->getArgument('show'); | ||||
| } | } | ||||
| public function requiresAuthentication() { | public function requiresAuthentication() { | ||||
| return true; | return true; | ||||
| } | } | ||||
| public function run() { | public function run() { | ||||
| $conduit = $this->getConduit(); | $conduit = $this->getConduit(); | ||||
| $this->writeStatusMessage("Getting file information...\n"); | $this->writeStatusMessage(pht('Getting file information...')."\n"); | ||||
| $info = $conduit->callMethodSynchronous( | $info = $conduit->callMethodSynchronous( | ||||
| 'file.info', | 'file.info', | ||||
| array( | array( | ||||
| 'id' => $this->id, | 'id' => $this->id, | ||||
| )); | )); | ||||
| $bytes = number_format($info['byteSize']); | $bytes = number_format($info['byteSize']); | ||||
| $desc = '('.$bytes.' bytes)'; | $desc = '('.$bytes.' bytes)'; | ||||
| if ($info['name']) { | if ($info['name']) { | ||||
| $desc = "'".$info['name']."' ".$desc; | $desc = "'".$info['name']."' ".$desc; | ||||
| } | } | ||||
| $this->writeStatusMessage("Downloading file {$desc}...\n"); | $this->writeStatusMessage(pht('Downloading file %s...', $desc)."\n"); | ||||
| $data = $conduit->callMethodSynchronous( | $data = $conduit->callMethodSynchronous( | ||||
| 'file.download', | 'file.download', | ||||
| array( | array( | ||||
| 'phid' => $info['phid'], | 'phid' => $info['phid'], | ||||
| )); | )); | ||||
| $data = base64_decode($data); | $data = base64_decode($data); | ||||
| if ($this->show) { | if ($this->show) { | ||||
| echo $data; | echo $data; | ||||
| } else { | } else { | ||||
| $path = Filesystem::writeUniqueFile( | $path = Filesystem::writeUniqueFile( | ||||
| nonempty($this->saveAs, $info['name'], 'file'), | nonempty($this->saveAs, $info['name'], 'file'), | ||||
| $data); | $data); | ||||
| $this->writeStatusMessage("Saved file as '{$path}'.\n"); | $this->writeStatusMessage(pht("Saved file as '%s'.", $path)."\n"); | ||||
| } | } | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| } | } | ||||