From 98969086851365d85c8bed0596ee695b1d61a638 Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 4 Sep 2015 10:34:14 -0700 Subject: [PATCH] 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 --- src/upload/ArcanistFileUploader.php | 49 +++++++++++++++++++++---- src/workflow/ArcanistUploadWorkflow.php | 17 ++++++++- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/src/upload/ArcanistFileUploader.php b/src/upload/ArcanistFileUploader.php index 955cdbdc..ba960775 100644 --- a/src/upload/ArcanistFileUploader.php +++ b/src/upload/ArcanistFileUploader.php @@ -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); diff --git a/src/workflow/ArcanistUploadWorkflow.php b/src/workflow/ArcanistUploadWorkflow.php index 64c5260d..b4414a39 100644 --- a/src/workflow/ArcanistUploadWorkflow.php +++ b/src/workflow/ArcanistUploadWorkflow.php @@ -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();