]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add temporary file support to ArcanistFileUploader
authorepriestley <git@epriestley.com>
Fri, 4 Sep 2015 17:34:14 +0000 (10:34 -0700)
committerepriestley <git@epriestley.com>
Fri, 4 Sep 2015 17:34:14 +0000 (10:34 -0700)
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
src/workflow/ArcanistUploadWorkflow.php

index 955cdbdce30fd8e53ecbdf8be7290735460d5756..ba96077581e80ad2937e1427c6057aa1c1435977 100644 (file)
@@ -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);
index 64c5260db6b086ca65eb3b3e85240e828f6cd450..b4414a398196df959fd011319dde36c22c4f7a66 100644 (file)
@@ -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();