Path: blob/master/src/applications/diffusion/query/DiffusionFileFutureQuery.php
12242 views
<?php12abstract class DiffusionFileFutureQuery3extends DiffusionQuery {45private $timeout;6private $byteLimit;78private $didHitByteLimit = false;9private $didHitTimeLimit = false;1011public static function getConduitParameters() {12return array(13'timeout' => 'optional int',14'byteLimit' => 'optional int',15);16}1718public function setTimeout($timeout) {19$this->timeout = $timeout;20return $this;21}2223public function getTimeout() {24return $this->timeout;25}2627public function setByteLimit($byte_limit) {28$this->byteLimit = $byte_limit;29return $this;30}3132public function getByteLimit() {33return $this->byteLimit;34}3536final public function getExceededByteLimit() {37return $this->didHitByteLimit;38}3940final public function getExceededTimeLimit() {41return $this->didHitTimeLimit;42}4344abstract protected function newQueryFuture();4546final public function respondToConduitRequest(ConduitAPIRequest $request) {47$drequest = $this->getRequest();4849$timeout = $request->getValue('timeout');50if ($timeout) {51$this->setTimeout($timeout);52}5354$byte_limit = $request->getValue('byteLimit');55if ($byte_limit) {56$this->setByteLimit($byte_limit);57}5859$file = $this->execute();6061$too_slow = (bool)$this->getExceededTimeLimit();62$too_huge = (bool)$this->getExceededByteLimit();6364$file_phid = null;65if (!$too_slow && !$too_huge) {66$repository = $drequest->getRepository();67$repository_phid = $repository->getPHID();6869$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();70$file->attachToObject($repository_phid);71unset($unguarded);7273$file_phid = $file->getPHID();74}7576return array(77'tooSlow' => $too_slow,78'tooHuge' => $too_huge,79'filePHID' => $file_phid,80);81}8283final public function executeInline() {84$future = $this->newConfiguredQueryFuture();85list($stdout) = $future->resolvex();86return $stdout;87}8889final protected function executeQuery() {90$future = $this->newConfiguredQueryFuture();9192$drequest = $this->getRequest();9394$name = '';95if ($drequest->getPath() !== null) {96$name = basename($drequest->getPath());97}98$relative_ttl = phutil_units('48 hours in seconds');99100try {101$threshold = PhabricatorFileStorageEngine::getChunkThreshold();102$future->setReadBufferSize($threshold);103104$source = id(new PhabricatorExecFutureFileUploadSource())105->setName($name)106->setRelativeTTL($relative_ttl)107->setViewPolicy(PhabricatorPolicies::POLICY_NOONE)108->setExecFuture($future);109110$byte_limit = $this->getByteLimit();111if ($byte_limit) {112$source->setByteLimit($byte_limit);113}114115$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();116$file = $source->uploadFile();117unset($unguarded);118119} catch (CommandException $ex) {120if (!$future->getWasKilledByTimeout()) {121throw $ex;122}123124$this->didHitTimeLimit = true;125$file = null;126} catch (PhabricatorFileUploadSourceByteLimitException $ex) {127$this->didHitByteLimit = true;128$file = null;129}130131return $file;132}133134private function newConfiguredQueryFuture() {135$future = $this->newQueryFuture();136137if ($this->getTimeout()) {138$future->setTimeout($this->getTimeout());139}140141return $future;142}143144}145146147