From 55d9cc70131678d1d373bb6605692cc2be475772 Mon Sep 17 00:00:00 2001 From: epriestley Date: Mon, 7 Sep 2015 12:44:59 -0700 Subject: [PATCH] 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 --- src/upload/ArcanistFileDataRef.php | 44 +++++++++++++++++++++++++ src/upload/ArcanistFileUploader.php | 37 ++++----------------- src/workflow/ArcanistUploadWorkflow.php | 9 +++-- 3 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/upload/ArcanistFileDataRef.php b/src/upload/ArcanistFileDataRef.php index 795fbd67..318418ec 100644 --- a/src/upload/ArcanistFileDataRef.php +++ b/src/upload/ArcanistFileDataRef.php @@ -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 )-------------------------------------------- */ diff --git a/src/upload/ArcanistFileUploader.php b/src/upload/ArcanistFileUploader.php index ba960775..27ec8d34 100644 --- a/src/upload/ArcanistFileUploader.php +++ b/src/upload/ArcanistFileUploader.php @@ -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); } diff --git a/src/workflow/ArcanistUploadWorkflow.php b/src/workflow/ArcanistUploadWorkflow.php index b4414a39..a31ce0f0 100644 --- a/src/workflow/ArcanistUploadWorkflow.php +++ b/src/workflow/ArcanistUploadWorkflow.php @@ -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();