mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-26 08:42:40 +01:00
Add temporary file support to ArcanistFileUploader
Summary: Ref T7148. Depends on D14055. Allows files to be marked as temporary when uploaded. Test Plan: Used `arc upload --temporary` to upload temporary files. Reviewers: chad Reviewed By: chad Maniphest Tasks: T7148 Differential Revision: https://secure.phabricator.com/D14056
This commit is contained in:
parent
7e677c27ec
commit
9896908685
2 changed files with 57 additions and 9 deletions
|
@ -27,6 +27,7 @@ final class ArcanistFileUploader extends Phobject {
|
|||
|
||||
private $conduit;
|
||||
private $files;
|
||||
private $config = array();
|
||||
|
||||
|
||||
/* -( Configuring the Uploader )------------------------------------------- */
|
||||
|
@ -78,6 +79,33 @@ final class ArcanistFileUploader extends Phobject {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Configure a file to be temporary instead of permanent.
|
||||
*
|
||||
* By default, files are retained indefinitely until explicitly deleted. If
|
||||
* you want to upload a temporary file instead, you can specify an epoch
|
||||
* timestamp. The file will be deleted after this time.
|
||||
*
|
||||
* @param string Key identifying the file you want to make temporary, as
|
||||
* passed to @{method:addFile}.
|
||||
* @param int Epoch timestamp to retain the file until.
|
||||
* @return this
|
||||
* @task add
|
||||
*/
|
||||
public function setDeleteFileAfterEpoch($file_key, $epoch) {
|
||||
if (empty($this->files[$file_key])) {
|
||||
throw new Exception(
|
||||
pht(
|
||||
'No file with given key ("%s") has been added to this uploader.',
|
||||
$file_key));
|
||||
}
|
||||
|
||||
$this->config[$file_key]['deleteAfterEpoch'] = $epoch;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/* -( Uploading Files )---------------------------------------------------- */
|
||||
|
||||
|
||||
|
@ -116,13 +144,20 @@ final class ArcanistFileUploader extends Phobject {
|
|||
$conduit = $this->conduit;
|
||||
$futures = array();
|
||||
foreach ($files as $key => $file) {
|
||||
$futures[$key] = $conduit->callMethod(
|
||||
'file.allocate',
|
||||
array(
|
||||
'name' => $file->getName(),
|
||||
'contentLength' => $file->getByteSize(),
|
||||
'contentHash' => $file->getContentHash(),
|
||||
));
|
||||
$config = idx($this->config, $key, array());
|
||||
|
||||
$params = array(
|
||||
'name' => $file->getName(),
|
||||
'contentLength' => $file->getByteSize(),
|
||||
'contentHash' => $file->getContentHash(),
|
||||
);
|
||||
|
||||
$delete_after = idx($config, 'deleteAfterEpoch');
|
||||
if ($delete_after !== null) {
|
||||
$params['deleteAfterEpoch'] = $delete_after;
|
||||
}
|
||||
|
||||
$futures[$key] = $conduit->callMethod('file.allocate', $params);
|
||||
}
|
||||
|
||||
$iterator = id(new FutureIterator($futures))->limit(4);
|
||||
|
|
|
@ -32,6 +32,11 @@ EOTEXT
|
|||
'json' => array(
|
||||
'help' => pht('Output upload information in JSON format.'),
|
||||
),
|
||||
'temporary' => array(
|
||||
'help' => pht(
|
||||
'Mark the file as temporary. Temporary files will be deleted '.
|
||||
'automatically after 24 hours.'),
|
||||
),
|
||||
'*' => 'paths',
|
||||
);
|
||||
}
|
||||
|
@ -51,18 +56,26 @@ EOTEXT
|
|||
}
|
||||
|
||||
public function run() {
|
||||
$is_temporary = $this->getArgument('temporary');
|
||||
|
||||
$conduit = $this->getConduit();
|
||||
$results = array();
|
||||
|
||||
$uploader = id(new ArcanistFileUploader())
|
||||
->setConduitClient($conduit);
|
||||
|
||||
foreach ($this->paths as $path) {
|
||||
foreach ($this->paths as $key => $path) {
|
||||
$file = id(new ArcanistFileDataRef())
|
||||
->setName(basename($path))
|
||||
->setPath($path);
|
||||
|
||||
$uploader->addFile($file);
|
||||
$uploader->addFile($file, $key);
|
||||
|
||||
if ($is_temporary) {
|
||||
$uploader->setDeleteFileAfterEpoch(
|
||||
$key,
|
||||
time() + phutil_units('24 hours in seconds'));
|
||||
}
|
||||
}
|
||||
|
||||
$files = $uploader->uploadFiles();
|
||||
|
|
Loading…
Reference in a new issue