Remove all "FileHasObject" edge reads and writes
[phabricator.git] / src / applications / files / conduit / FileConduitAPIMethod.php
blob420ed65bfb32e0ed463d92238af1d98ca491e3ba
1 <?php
3 abstract class FileConduitAPIMethod extends ConduitAPIMethod {
5 final public function getApplication() {
6 return PhabricatorApplication::getByClass('PhabricatorFilesApplication');
9 protected function loadFileByPHID(PhabricatorUser $viewer, $file_phid) {
10 $file = id(new PhabricatorFileQuery())
11 ->setViewer($viewer)
12 ->withPHIDs(array($file_phid))
13 ->executeOne();
14 if (!$file) {
15 throw new Exception(pht('No such file "%s"!', $file_phid));
18 return $file;
21 protected function loadFileChunks(
22 PhabricatorUser $viewer,
23 PhabricatorFile $file) {
24 return $this->newChunkQuery($viewer, $file)
25 ->execute();
28 protected function loadFileChunkForUpload(
29 PhabricatorUser $viewer,
30 PhabricatorFile $file,
31 $start,
32 $end) {
34 $start = (int)$start;
35 $end = (int)$end;
37 $chunks = $this->newChunkQuery($viewer, $file)
38 ->withByteRange($start, $end)
39 ->execute();
41 if (!$chunks) {
42 throw new Exception(
43 pht(
44 'There are no file data chunks in byte range %d - %d.',
45 $start,
46 $end));
49 if (count($chunks) !== 1) {
50 phlog($chunks);
51 throw new Exception(
52 pht(
53 'There are multiple chunks in byte range %d - %d.',
54 $start,
55 $end));
58 $chunk = head($chunks);
59 if ($chunk->getByteStart() != $start) {
60 throw new Exception(
61 pht(
62 'Chunk start byte is %d, not %d.',
63 $chunk->getByteStart(),
64 $start));
67 if ($chunk->getByteEnd() != $end) {
68 throw new Exception(
69 pht(
70 'Chunk end byte is %d, not %d.',
71 $chunk->getByteEnd(),
72 $end));
75 if ($chunk->getDataFilePHID()) {
76 throw new Exception(
77 pht('Chunk has already been uploaded.'));
80 return $chunk;
83 protected function decodeBase64($data) {
84 $data = base64_decode($data, $strict = true);
85 if ($data === false) {
86 throw new Exception(pht('Unable to decode base64 data!'));
88 return $data;
91 protected function loadAnyMissingChunk(
92 PhabricatorUser $viewer,
93 PhabricatorFile $file) {
95 return $this->newChunkQuery($viewer, $file)
96 ->withIsComplete(false)
97 ->setLimit(1)
98 ->execute();
101 private function newChunkQuery(
102 PhabricatorUser $viewer,
103 PhabricatorFile $file) {
105 $engine = $file->instantiateStorageEngine();
106 if (!$engine->isChunkEngine()) {
107 throw new Exception(
108 pht(
109 'File "%s" does not have chunks!',
110 $file->getPHID()));
113 return id(new PhabricatorFileChunkQuery())
114 ->setViewer($viewer)
115 ->withChunkHandles(array($file->getStorageHandle()));