1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-12-29 17:00:58 +01:00

Improve temporary file upload API and add viewPolicy support

Summary: Ref T7148. In D14056, I let `arc upload` upload temporary files, but this is a better way to do some of the internals. Also add support for setting a `viewPolicy`.

Test Plan: Used `arc upload`, `arc upload --temporary`.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T7148

Differential Revision: https://secure.phabricator.com/D14075
This commit is contained in:
epriestley 2015-09-07 12:44:59 -07:00
parent 9896908685
commit 55d9cc7013
3 changed files with 54 additions and 36 deletions

View file

@ -27,6 +27,8 @@ final class ArcanistFileDataRef extends Phobject {
private $errors = array();
private $phid;
private $fileHandle;
private $deleteAfterEpoch;
private $viewPolicy;
/* -( Configuring File References )---------------------------------------- */
@ -107,6 +109,48 @@ final class ArcanistFileDataRef extends Phobject {
}
/**
* @task config
*/
public function setViewPolicy($view_policy) {
$this->viewPolicy = $view_policy;
return $this;
}
/**
* @task config
*/
public function getViewPolicy() {
return $this->viewPolicy;
}
/**
* 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 int Epoch timestamp to retain the file until.
* @return this
* @task config
*/
public function setDeleteAfterEpoch($epoch) {
$this->deleteAfterEpoch = $epoch;
return $this;
}
/**
* @task config
*/
public function getDeleteAfterEpoch() {
return $this->deleteAfterEpoch;
}
/* -( Handling Upload Results )-------------------------------------------- */

View file

@ -27,7 +27,6 @@ final class ArcanistFileUploader extends Phobject {
private $conduit;
private $files;
private $config = array();
/* -( Configuring the Uploader )------------------------------------------- */
@ -79,33 +78,6 @@ 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 )---------------------------------------------------- */
@ -144,19 +116,22 @@ final class ArcanistFileUploader extends Phobject {
$conduit = $this->conduit;
$futures = array();
foreach ($files as $key => $file) {
$config = idx($this->config, $key, array());
$params = array(
'name' => $file->getName(),
'contentLength' => $file->getByteSize(),
'contentHash' => $file->getContentHash(),
);
$delete_after = idx($config, 'deleteAfterEpoch');
$delete_after = $file->getDeleteAfterEpoch();
if ($delete_after !== null) {
$params['deleteAfterEpoch'] = $delete_after;
}
$view_policy = $file->getViewPolicy();
if ($view_policy !== null) {
$params['viewPolicy'] = $view_policy;
}
$futures[$key] = $conduit->callMethod('file.allocate', $params);
}

View file

@ -69,13 +69,12 @@ EOTEXT
->setName(basename($path))
->setPath($path);
$uploader->addFile($file, $key);
if ($is_temporary) {
$uploader->setDeleteFileAfterEpoch(
$key,
time() + phutil_units('24 hours in seconds'));
$expires_at = time() + phutil_units('24 hours in seconds');
$file->setDeleteAfterEpoch($expires_at);
}
$uploader->addFile($file);
}
$files = $uploader->uploadFiles();