Changeset View
Changeset View
Standalone View
Standalone View
src/upload/ArcanistFileUploader.php
Show All 21 Lines | |||||
* @task add Adding Files | * @task add Adding Files | ||||
* @task upload Uploading Files | * @task upload Uploading Files | ||||
* @task internal Internals | * @task internal Internals | ||||
*/ | */ | ||||
final class ArcanistFileUploader extends Phobject { | final class ArcanistFileUploader extends Phobject { | ||||
private $conduit; | private $conduit; | ||||
private $files; | private $files; | ||||
private $config = array(); | |||||
/* -( Configuring the Uploader )------------------------------------------- */ | /* -( Configuring the Uploader )------------------------------------------- */ | ||||
/** | /** | ||||
* Provide a Conduit client to choose which server to upload files to. | * Provide a Conduit client to choose which server to upload files to. | ||||
* | * | ||||
Show All 35 Lines | if ($key === null) { | ||||
} | } | ||||
$this->files[$key] = $file; | $this->files[$key] = $file; | ||||
} | } | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | |||||
* 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 )---------------------------------------------------- */ | ||||
/** | /** | ||||
* Upload files to the server. | * Upload files to the server. | ||||
* | * | ||||
* This transfers all files which have been queued with @{method:addFiles} | * This transfers all files which have been queued with @{method:addFiles} | ||||
* over the Conduit link configured with @{method:setConduitClient}. | * over the Conduit link configured with @{method:setConduitClient}. | ||||
Show All 22 Lines | foreach ($files as $key => $file) { | ||||
$file->didFail($ex->getMessage()); | $file->didFail($ex->getMessage()); | ||||
unset($files[$key]); | unset($files[$key]); | ||||
} | } | ||||
} | } | ||||
$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); | ||||
$chunks = array(); | $chunks = array(); | ||||
foreach ($iterator as $key => $future) { | foreach ($iterator as $key => $future) { | ||||
try { | try { | ||||
$result = $future->resolve(); | $result = $future->resolve(); | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
▲ Show 20 Lines • Show All 170 Lines • Show Last 20 Lines |