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
|
|
|
),
|
2015-09-04 19:34:14 +02:00
|
|
|
'temporary' => array(
|
|
|
|
'help' => pht(
|
|
|
|
'Mark the file as temporary. Temporary files will be deleted '.
|
|
|
|
'automatically after 24 hours.'),
|
|
|
|
),
|
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() {
|
2015-09-04 19:34:14 +02:00
|
|
|
$is_temporary = $this->getArgument('temporary');
|
|
|
|
|
2011-07-29 19:05:07 +02:00
|
|
|
$conduit = $this->getConduit();
|
2011-07-30 05:17:49 +02:00
|
|
|
$results = array();
|
|
|
|
|
2015-05-27 19:25:53 +02:00
|
|
|
$uploader = id(new ArcanistFileUploader())
|
|
|
|
->setConduitClient($conduit);
|
2015-03-13 19:30:50 +01:00
|
|
|
|
2015-09-04 19:34:14 +02:00
|
|
|
foreach ($this->paths as $key => $path) {
|
2015-05-27 19:25:53 +02:00
|
|
|
$file = id(new ArcanistFileDataRef())
|
|
|
|
->setName(basename($path))
|
|
|
|
->setPath($path);
|
2015-03-13 19:30:50 +01:00
|
|
|
|
2015-09-04 19:34:14 +02:00
|
|
|
if ($is_temporary) {
|
2015-09-07 21:44:59 +02:00
|
|
|
$expires_at = time() + phutil_units('24 hours in seconds');
|
|
|
|
$file->setDeleteAfterEpoch($expires_at);
|
2015-09-04 19:34:14 +02:00
|
|
|
}
|
2015-09-07 21:44:59 +02:00
|
|
|
|
|
|
|
$uploader->addFile($file);
|
2015-05-27 19:25:53 +02:00
|
|
|
}
|
2015-03-13 19:30:50 +01:00
|
|
|
|
2015-05-27 19:25:53 +02:00
|
|
|
$files = $uploader->uploadFiles();
|
2015-03-19 03:06:27 +01:00
|
|
|
|
2015-05-27 19:25:53 +02:00
|
|
|
$results = array();
|
|
|
|
foreach ($files as $file) {
|
|
|
|
// TODO: This could be handled more gracefully; just preserving behavior
|
|
|
|
// until we introduce `file.query` and modernize this.
|
|
|
|
if ($file->getErrors()) {
|
|
|
|
throw new Exception(implode("\n", $file->getErrors()));
|
2011-07-29 19:05:07 +02:00
|
|
|
}
|
2015-05-27 19:25:53 +02:00
|
|
|
$phid = $file->getPHID();
|
|
|
|
$name = $file->getName();
|
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(
|
2015-11-02 11:28:15 +01:00
|
|
|
'Resuming upload (%s of %s chunks remain).',
|
|
|
|
phutil_count($remaining),
|
|
|
|
phutil_count($chunks)));
|
2015-03-13 19:30:50 +01:00
|
|
|
} else {
|
|
|
|
$this->writeStatus(
|
|
|
|
pht(
|
2015-11-02 11:28:15 +01:00
|
|
|
'Uploading chunks (%s chunks to upload).',
|
|
|
|
phutil_count($remaining)));
|
2015-03-13 19:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
2015-05-13 10:05:15 +02:00
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'Failed to %s!',
|
|
|
|
'fseek()'));
|
2015-03-13 19:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = fread($f, $chunk['byteEnd'] - $chunk['byteStart']);
|
|
|
|
if ($data === false) {
|
2015-05-13 10:05:15 +02:00
|
|
|
throw new Exception(
|
|
|
|
pht(
|
|
|
|
'Failed to %s!',
|
|
|
|
'fread()'));
|
2015-03-13 19:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
}
|