2011-07-29 19:05:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload a file to Phabricator.
|
|
|
|
*/
|
2014-07-21 23:49:15 +02:00
|
|
|
final class ArcanistUploadWorkflow extends ArcanistWorkflow {
|
2011-07-29 19:05:07 +02:00
|
|
|
|
|
|
|
private $paths;
|
2011-07-30 05:17:49 +02:00
|
|
|
private $json;
|
2011-07-29 19:05:07 +02:00
|
|
|
|
Make Arcanist workflow names explicit
Summary:
Currently, adding a new workflow requires you to override ArcanistConfiguration, which is messy. Instead, just load everything that extends ArcanistBaseWorkflow.
Remove all the rules tying workflow names to class names through arcane incantations.
This has a very small performance cost in that we need to load every Workflow class every time now, but we don't hit __init__ and such anymore and it was pretty negligible on my machine (98ms vs 104ms or something).
Test Plan: Ran "arc help", "arc which", "arc diff", etc.
Reviewers: edward, vrana, btrahan
Reviewed By: edward
CC: aran, zeeg
Differential Revision: https://secure.phabricator.com/D3691
2012-10-17 17:35:03 +02:00
|
|
|
public function getWorkflowName() {
|
|
|
|
return 'upload';
|
|
|
|
}
|
|
|
|
|
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(
|
2015-02-01 11:51:12 +01:00
|
|
|
'help' => pht('Output upload information in JSON format.'),
|
2011-07-30 05:17:49 +02:00
|
|
|
),
|
2011-07-29 19:05:07 +02:00
|
|
|
'*' => 'paths',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function didParseArguments() {
|
|
|
|
if (!$this->getArgument('paths')) {
|
2015-02-01 11:51:12 +01:00
|
|
|
throw new ArcanistUsageException(
|
|
|
|
pht('Specify one or more files to upload.'));
|
2011-07-29 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-03-13 19:30:50 +01:00
|
|
|
$path = Filesystem::resolvePath($path);
|
|
|
|
|
2011-07-29 19:05:07 +02:00
|
|
|
$name = basename($path);
|
2015-03-13 19:30:50 +01:00
|
|
|
$this->writeStatus(pht("Uploading '%s'...", $name));
|
|
|
|
|
|
|
|
$hash = @sha1_file($path);
|
|
|
|
if (!$hash) {
|
|
|
|
throw new Exception(pht('Unable to read file "%s"!', $path));
|
|
|
|
}
|
|
|
|
$length = filesize($path);
|
2015-02-01 11:51:12 +01:00
|
|
|
|
2015-03-13 19:30:50 +01:00
|
|
|
$phid = null;
|
2011-07-29 19:05:07 +02:00
|
|
|
try {
|
2015-03-13 19:30:50 +01:00
|
|
|
$result = $conduit->callMethodSynchronous(
|
|
|
|
'file.allocate',
|
|
|
|
array(
|
|
|
|
'name' => $name,
|
|
|
|
'contentLength' => $length,
|
|
|
|
'contentHash' => $hash,
|
|
|
|
));
|
|
|
|
|
|
|
|
$phid = $result['filePHID'];
|
|
|
|
if (!$result['upload']) {
|
|
|
|
if (!$phid) {
|
|
|
|
$this->writeStatus(
|
|
|
|
pht(
|
|
|
|
'Unable to upload file "%s": the server refused to accept '.
|
|
|
|
'it. This usually means it is too large.',
|
|
|
|
$name));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Otherwise, the server completed the upload by referencing known
|
|
|
|
// file data.
|
|
|
|
} else {
|
|
|
|
if ($phid) {
|
|
|
|
$this->uploadChunks($phid, $path);
|
|
|
|
} else {
|
|
|
|
// This is a small file that doesn't need to be uploaded in
|
|
|
|
// chunks, so continue normally.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$this->writeStatus(
|
|
|
|
pht('Unable to use allocate method, trying older upload method.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$phid) {
|
|
|
|
try {
|
|
|
|
$data = Filesystem::readFile($path);
|
|
|
|
} catch (FilesystemException $ex) {
|
|
|
|
$this->writeStatus(
|
|
|
|
pht('Unable to read file "%s".', $ex->getMessage()));
|
|
|
|
$results[$path] = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$phid = $conduit->callMethodSynchronous(
|
|
|
|
'file.upload',
|
|
|
|
array(
|
|
|
|
'data_base64' => base64_encode($data),
|
|
|
|
'name' => $name,
|
|
|
|
));
|
2011-07-29 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$info = $conduit->callMethodSynchronous(
|
|
|
|
'file.info',
|
|
|
|
array(
|
2015-02-01 11:51:12 +01:00
|
|
|
'phid' => $phid,
|
2011-07-29 19:05:07 +02:00
|
|
|
));
|
|
|
|
|
2011-07-30 05:17:49 +02:00
|
|
|
$results[$path] = $info;
|
|
|
|
|
2015-02-01 11:51:12 +01:00
|
|
|
if (!$this->json) {
|
2012-07-21 15:44:21 +02:00
|
|
|
$id = $info['id'];
|
|
|
|
echo " F{$id} {$name}: ".$info['uri']."\n\n";
|
2011-07-30 05:17:49 +02:00
|
|
|
}
|
2011-07-29 19:05:07 +02:00
|
|
|
}
|
|
|
|
|
2015-02-01 11:51:12 +01:00
|
|
|
if ($this->json) {
|
2011-07-30 05:17:49 +02:00
|
|
|
echo json_encode($results)."\n";
|
|
|
|
} else {
|
2015-03-13 19:30:50 +01:00
|
|
|
$this->writeStatus(pht('Done.'));
|
2011-07-30 05:17:49 +02:00
|
|
|
}
|
2011-07-29 19:05:07 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-13 19:30:50 +01:00
|
|
|
private function writeStatus($line) {
|
|
|
|
$this->writeStatusMessage($line."\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function uploadChunks($file_phid, $path) {
|
|
|
|
$conduit = $this->getConduit();
|
|
|
|
|
|
|
|
$f = @fopen($path, 'rb');
|
|
|
|
if (!$f) {
|
|
|
|
throw new Exception(pht('Unable to open file "%s"', $path));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->writeStatus(pht('Beginning chunked upload of large file...'));
|
|
|
|
$chunks = $conduit->callMethodSynchronous(
|
|
|
|
'file.querychunks',
|
|
|
|
array(
|
|
|
|
'filePHID' => $file_phid,
|
|
|
|
));
|
|
|
|
|
|
|
|
$remaining = array();
|
|
|
|
foreach ($chunks as $chunk) {
|
|
|
|
if (!$chunk['complete']) {
|
|
|
|
$remaining[] = $chunk;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$done = (count($chunks) - count($remaining));
|
|
|
|
|
|
|
|
if ($done) {
|
|
|
|
$this->writeStatus(
|
|
|
|
pht(
|
|
|
|
'Resuming upload (%d of %d chunks remain).',
|
|
|
|
new PhutilNumber(count($remaining)),
|
|
|
|
new PhutilNumber(count($chunks))));
|
|
|
|
} else {
|
|
|
|
$this->writeStatus(
|
|
|
|
pht(
|
|
|
|
'Uploading chunks (%d chunks to upload).',
|
|
|
|
new PhutilNumber(count($remaining))));
|
|
|
|
}
|
|
|
|
|
|
|
|
$progress = new PhutilConsoleProgressBar();
|
|
|
|
$progress->setTotal(count($chunks));
|
|
|
|
|
|
|
|
for ($ii = 0; $ii < $done; $ii++) {
|
|
|
|
$progress->update(1);
|
|
|
|
}
|
|
|
|
|
2015-03-15 19:31:56 +01:00
|
|
|
$progress->draw();
|
|
|
|
|
2015-03-13 19:30:50 +01:00
|
|
|
// TODO: We could do these in parallel to improve upload performance.
|
|
|
|
foreach ($remaining as $chunk) {
|
|
|
|
$offset = $chunk['byteStart'];
|
|
|
|
|
|
|
|
$ok = fseek($f, $offset);
|
|
|
|
if ($ok !== 0) {
|
|
|
|
throw new Exception(pht('Failed to fseek()!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = fread($f, $chunk['byteEnd'] - $chunk['byteStart']);
|
|
|
|
if ($data === false) {
|
|
|
|
throw new Exception(pht('Failed to fread()!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$conduit->callMethodSynchronous(
|
|
|
|
'file.uploadchunk',
|
|
|
|
array(
|
|
|
|
'filePHID' => $file_phid,
|
|
|
|
'byteStart' => $offset,
|
|
|
|
'dataEncoding' => 'base64',
|
|
|
|
'data' => base64_encode($data),
|
|
|
|
));
|
|
|
|
|
|
|
|
$progress->update(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 19:05:07 +02:00
|
|
|
}
|