mirror of
https://we.phorge.it/source/arcanist.git
synced 2025-03-11 20:04:52 +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 $conduit;
|
||||||
private $files;
|
private $files;
|
||||||
|
private $config = array();
|
||||||
|
|
||||||
|
|
||||||
/* -( Configuring the Uploader )------------------------------------------- */
|
/* -( 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 )---------------------------------------------------- */
|
/* -( Uploading Files )---------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
|
@ -116,13 +144,20 @@ final class ArcanistFileUploader extends Phobject {
|
||||||
$conduit = $this->conduit;
|
$conduit = $this->conduit;
|
||||||
$futures = array();
|
$futures = array();
|
||||||
foreach ($files as $key => $file) {
|
foreach ($files as $key => $file) {
|
||||||
$futures[$key] = $conduit->callMethod(
|
$config = idx($this->config, $key, array());
|
||||||
'file.allocate',
|
|
||||||
array(
|
$params = array(
|
||||||
'name' => $file->getName(),
|
'name' => $file->getName(),
|
||||||
'contentLength' => $file->getByteSize(),
|
'contentLength' => $file->getByteSize(),
|
||||||
'contentHash' => $file->getContentHash(),
|
'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);
|
$iterator = id(new FutureIterator($futures))->limit(4);
|
||||||
|
|
|
@ -32,6 +32,11 @@ EOTEXT
|
||||||
'json' => array(
|
'json' => array(
|
||||||
'help' => pht('Output upload information in JSON format.'),
|
'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',
|
'*' => 'paths',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -51,18 +56,26 @@ EOTEXT
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run() {
|
public function run() {
|
||||||
|
$is_temporary = $this->getArgument('temporary');
|
||||||
|
|
||||||
$conduit = $this->getConduit();
|
$conduit = $this->getConduit();
|
||||||
$results = array();
|
$results = array();
|
||||||
|
|
||||||
$uploader = id(new ArcanistFileUploader())
|
$uploader = id(new ArcanistFileUploader())
|
||||||
->setConduitClient($conduit);
|
->setConduitClient($conduit);
|
||||||
|
|
||||||
foreach ($this->paths as $path) {
|
foreach ($this->paths as $key => $path) {
|
||||||
$file = id(new ArcanistFileDataRef())
|
$file = id(new ArcanistFileDataRef())
|
||||||
->setName(basename($path))
|
->setName(basename($path))
|
||||||
->setPath($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();
|
$files = $uploader->uploadFiles();
|
||||||
|
|
Loading…
Add table
Reference in a new issue