Path: blob/master/src/applications/files/conduit/FileConduitAPIMethod.php
12241 views
<?php12abstract class FileConduitAPIMethod extends ConduitAPIMethod {34final public function getApplication() {5return PhabricatorApplication::getByClass('PhabricatorFilesApplication');6}78protected function loadFileByPHID(PhabricatorUser $viewer, $file_phid) {9$file = id(new PhabricatorFileQuery())10->setViewer($viewer)11->withPHIDs(array($file_phid))12->executeOne();13if (!$file) {14throw new Exception(pht('No such file "%s"!', $file_phid));15}1617return $file;18}1920protected function loadFileChunks(21PhabricatorUser $viewer,22PhabricatorFile $file) {23return $this->newChunkQuery($viewer, $file)24->execute();25}2627protected function loadFileChunkForUpload(28PhabricatorUser $viewer,29PhabricatorFile $file,30$start,31$end) {3233$start = (int)$start;34$end = (int)$end;3536$chunks = $this->newChunkQuery($viewer, $file)37->withByteRange($start, $end)38->execute();3940if (!$chunks) {41throw new Exception(42pht(43'There are no file data chunks in byte range %d - %d.',44$start,45$end));46}4748if (count($chunks) !== 1) {49phlog($chunks);50throw new Exception(51pht(52'There are multiple chunks in byte range %d - %d.',53$start,54$end));55}5657$chunk = head($chunks);58if ($chunk->getByteStart() != $start) {59throw new Exception(60pht(61'Chunk start byte is %d, not %d.',62$chunk->getByteStart(),63$start));64}6566if ($chunk->getByteEnd() != $end) {67throw new Exception(68pht(69'Chunk end byte is %d, not %d.',70$chunk->getByteEnd(),71$end));72}7374if ($chunk->getDataFilePHID()) {75throw new Exception(76pht('Chunk has already been uploaded.'));77}7879return $chunk;80}8182protected function decodeBase64($data) {83$data = base64_decode($data, $strict = true);84if ($data === false) {85throw new Exception(pht('Unable to decode base64 data!'));86}87return $data;88}8990protected function loadAnyMissingChunk(91PhabricatorUser $viewer,92PhabricatorFile $file) {9394return $this->newChunkQuery($viewer, $file)95->withIsComplete(false)96->setLimit(1)97->execute();98}99100private function newChunkQuery(101PhabricatorUser $viewer,102PhabricatorFile $file) {103104$engine = $file->instantiateStorageEngine();105if (!$engine->isChunkEngine()) {106throw new Exception(107pht(108'File "%s" does not have chunks!',109$file->getPHID()));110}111112return id(new PhabricatorFileChunkQuery())113->setViewer($viewer)114->withChunkHandles(array($file->getStorageHandle()));115}116117118}119120121