Changeset View
Changeset View
Standalone View
Standalone View
src/applications/diffusion/query/DiffusionFileFutureQuery.php
- This file was copied from src/applications/diffusion/query/filecontent/DiffusionFileContentQuery.php.
| <?php | <?php | ||||
| abstract class DiffusionFileContentQuery extends DiffusionQuery { | abstract class DiffusionFileFutureQuery | ||||
| extends DiffusionQuery { | |||||
| private $timeout; | private $timeout; | ||||
| private $byteLimit; | private $byteLimit; | ||||
| private $didHitByteLimit = false; | private $didHitByteLimit = false; | ||||
| private $didHitTimeLimit = false; | private $didHitTimeLimit = false; | ||||
| public static function getConduitParameters() { | |||||
| return array( | |||||
| 'timeout' => 'optional int', | |||||
| 'byteLimit' => 'optional int', | |||||
| ); | |||||
| } | |||||
| public function setTimeout($timeout) { | public function setTimeout($timeout) { | ||||
| $this->timeout = $timeout; | $this->timeout = $timeout; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getTimeout() { | public function getTimeout() { | ||||
| return $this->timeout; | return $this->timeout; | ||||
| } | } | ||||
| public function setByteLimit($byte_limit) { | public function setByteLimit($byte_limit) { | ||||
| $this->byteLimit = $byte_limit; | $this->byteLimit = $byte_limit; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function getByteLimit() { | public function getByteLimit() { | ||||
| return $this->byteLimit; | return $this->byteLimit; | ||||
| } | } | ||||
| final public static function newFromDiffusionRequest( | |||||
| DiffusionRequest $request) { | |||||
| return parent::newQueryObject(__CLASS__, $request); | |||||
| } | |||||
| final public function getExceededByteLimit() { | final public function getExceededByteLimit() { | ||||
| return $this->didHitByteLimit; | return $this->didHitByteLimit; | ||||
| } | } | ||||
| final public function getExceededTimeLimit() { | final public function getExceededTimeLimit() { | ||||
| return $this->didHitTimeLimit; | return $this->didHitTimeLimit; | ||||
| } | } | ||||
| abstract protected function getFileContentFuture(); | abstract protected function getFileContentFuture(); | ||||
| abstract protected function resolveFileContentFuture(Future $future); | |||||
| final protected function executeQuery() { | final public function respondToConduitRequest(ConduitAPIRequest $request) { | ||||
| $future = $this->getFileContentFuture(); | $drequest = $this->getRequest(); | ||||
| if ($this->getTimeout()) { | $timeout = $request->getValue('timeout'); | ||||
| $future->setTimeout($this->getTimeout()); | if ($timeout) { | ||||
| $this->setTimeout($timeout); | |||||
| } | } | ||||
| $byte_limit = $this->getByteLimit(); | $byte_limit = $request->getValue('byteLimit'); | ||||
| if ($byte_limit) { | if ($byte_limit) { | ||||
| $future->setStdoutSizeLimit($byte_limit + 1); | $this->setByteLimit($byte_limit); | ||||
| } | |||||
| $file = $this->execute(); | |||||
| $too_slow = (bool)$this->getExceededTimeLimit(); | |||||
| $too_huge = (bool)$this->getExceededByteLimit(); | |||||
| $file_phid = null; | |||||
| if (!$too_slow && !$too_huge) { | |||||
| $repository = $drequest->getRepository(); | |||||
| $repository_phid = $repository->getPHID(); | |||||
| $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | |||||
| $file->attachToObject($repository_phid); | |||||
| unset($unguarded); | |||||
| $file_phid = $file->getPHID(); | |||||
| } | |||||
| return array( | |||||
| 'tooSlow' => $too_slow, | |||||
| 'tooHuge' => $too_huge, | |||||
| 'filePHID' => $file_phid, | |||||
| ); | |||||
| } | } | ||||
| final public function executeInline() { | |||||
| $future = $this->getFileContentFuture(); | |||||
| list($stdout) = $future->resolvex(); | |||||
| return $future; | |||||
| } | |||||
| final protected function executeQuery() { | |||||
| $future = $this->newConfiguredFileContentFuture(); | |||||
| $drequest = $this->getRequest(); | $drequest = $this->getRequest(); | ||||
| $name = basename($drequest->getPath()); | $name = basename($drequest->getPath()); | ||||
| $ttl = PhabricatorTime::getNow() + phutil_units('48 hours in seconds'); | $ttl = PhabricatorTime::getNow() + phutil_units('48 hours in seconds'); | ||||
| try { | try { | ||||
| $threshold = PhabricatorFileStorageEngine::getChunkThreshold(); | $threshold = PhabricatorFileStorageEngine::getChunkThreshold(); | ||||
| $future->setReadBufferSize($threshold); | $future->setReadBufferSize($threshold); | ||||
| Show All 12 Lines | try { | ||||
| if (!$future->getWasKilledByTimeout()) { | if (!$future->getWasKilledByTimeout()) { | ||||
| throw $ex; | throw $ex; | ||||
| } | } | ||||
| $this->didHitTimeLimit = true; | $this->didHitTimeLimit = true; | ||||
| $file = null; | $file = null; | ||||
| } | } | ||||
| $byte_limit = $this->getByteLimit(); | |||||
| if ($byte_limit && ($file->getByteSize() > $byte_limit)) { | if ($byte_limit && ($file->getByteSize() > $byte_limit)) { | ||||
| $this->didHitByteLimit = true; | $this->didHitByteLimit = true; | ||||
| $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | ||||
| id(new PhabricatorDestructionEngine()) | id(new PhabricatorDestructionEngine()) | ||||
| ->destroyObject($file); | ->destroyObject($file); | ||||
| unset($unguarded); | unset($unguarded); | ||||
| $file = null; | $file = null; | ||||
| } | } | ||||
| return $file; | return $file; | ||||
| } | } | ||||
| private function newConfiguredFileContentFuture() { | |||||
| $future = $this->getFileContentFuture(); | |||||
| if ($this->getTimeout()) { | |||||
| $future->setTimeout($this->getTimeout()); | |||||
| } | |||||
| $byte_limit = $this->getByteLimit(); | |||||
| if ($byte_limit) { | |||||
| $future->setStdoutSizeLimit($byte_limit + 1); | |||||
| } | |||||
| return $future; | |||||
| } | |||||
| } | } | ||||