2011-07-29 19:05:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-03-05 19:02:37 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-07-29 19:05:07 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload a file to Phabricator.
|
|
|
|
*
|
|
|
|
* @group workflow
|
|
|
|
*/
|
|
|
|
final class ArcanistUploadWorkflow extends ArcanistBaseWorkflow {
|
|
|
|
|
|
|
|
private $paths;
|
2011-07-30 05:17:49 +02:00
|
|
|
private $json;
|
2011-07-29 19:05:07 +02:00
|
|
|
|
2012-03-05 19:02:37 +01:00
|
|
|
public function getCommandSynopses() {
|
2011-07-29 19:05:07 +02:00
|
|
|
return phutil_console_format(<<<EOTEXT
|
2011-07-30 05:17:49 +02:00
|
|
|
**upload** __file__ [__file__ ...] [--json]
|
2012-03-05 19:02:37 +01:00
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCommandHelp() {
|
|
|
|
return phutil_console_format(<<<EOTEXT
|
2011-07-29 19:05:07 +02:00
|
|
|
Supports: filesystems
|
|
|
|
Upload a file from local disk.
|
|
|
|
EOTEXT
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getArguments() {
|
|
|
|
return array(
|
2011-07-30 05:17:49 +02:00
|
|
|
'json' => array(
|
|
|
|
'help' => 'Output upload information in JSON format.',
|
|
|
|
),
|
2011-07-29 19:05:07 +02:00
|
|
|
'*' => 'paths',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function didParseArguments() {
|
|
|
|
if (!$this->getArgument('paths')) {
|
|
|
|
throw new ArcanistUsageException("Specify one or more files to upload.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->paths = $this->getArgument('paths');
|
2011-07-30 05:17:49 +02:00
|
|
|
$this->json = $this->getArgument('json');
|
2011-07-29 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function requiresAuthentication() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPaths() {
|
|
|
|
return $this->paths;
|
|
|
|
}
|
|
|
|
|
2011-07-30 05:17:49 +02:00
|
|
|
private function getJSON() {
|
|
|
|
return $this->json;
|
|
|
|
}
|
|
|
|
|
2011-07-29 19:05:07 +02:00
|
|
|
public function run() {
|
|
|
|
|
|
|
|
$conduit = $this->getConduit();
|
|
|
|
|
2011-07-30 05:17:49 +02:00
|
|
|
$results = array();
|
|
|
|
|
2011-07-29 19:05:07 +02:00
|
|
|
foreach ($this->paths as $path) {
|
|
|
|
$name = basename($path);
|
2011-07-30 05:17:49 +02:00
|
|
|
$this->writeStatusMessage("Uploading '{$name}'...\n");
|
2011-07-29 19:05:07 +02:00
|
|
|
try {
|
|
|
|
$data = Filesystem::readFile($path);
|
|
|
|
} catch (FilesystemException $ex) {
|
2011-07-30 05:17:49 +02:00
|
|
|
$this->writeStatusMessage(
|
|
|
|
"Unable to upload file: ".$ex->getMessage()."\n");
|
|
|
|
$results[$path] = null;
|
2011-07-29 19:05:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$phid = $conduit->callMethodSynchronous(
|
|
|
|
'file.upload',
|
|
|
|
array(
|
|
|
|
'data_base64' => base64_encode($data),
|
|
|
|
'name' => $name,
|
|
|
|
));
|
|
|
|
$info = $conduit->callMethodSynchronous(
|
|
|
|
'file.info',
|
|
|
|
array(
|
|
|
|
'phid' => $phid,
|
|
|
|
));
|
|
|
|
|
2011-07-30 05:17:49 +02:00
|
|
|
$results[$path] = $info;
|
|
|
|
|
|
|
|
if (!$this->getJSON()) {
|
|
|
|
echo " {$name}: ".$info['uri']."\n\n";
|
|
|
|
}
|
2011-07-29 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
2011-07-30 05:17:49 +02:00
|
|
|
if ($this->getJSON()) {
|
|
|
|
echo json_encode($results)."\n";
|
|
|
|
} else {
|
|
|
|
$this->writeStatusMessage("Done.\n");
|
|
|
|
}
|
2011-07-29 19:05:07 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|