@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3abstract class FileConduitAPIMethod extends ConduitAPIMethod {
4
5 final public function getApplication() {
6 return PhabricatorApplication::getByClass(
7 PhabricatorFilesApplication::class);
8 }
9
10 protected function loadFileByPHID(PhabricatorUser $viewer, $file_phid) {
11 $file = id(new PhabricatorFileQuery())
12 ->setViewer($viewer)
13 ->withPHIDs(array($file_phid))
14 ->executeOne();
15 if (!$file) {
16 throw new Exception(pht('No such file "%s"!', $file_phid));
17 }
18
19 return $file;
20 }
21
22 protected function loadFileChunks(
23 PhabricatorUser $viewer,
24 PhabricatorFile $file) {
25 return $this->newChunkQuery($viewer, $file)
26 ->execute();
27 }
28
29 protected function loadFileChunkForUpload(
30 PhabricatorUser $viewer,
31 PhabricatorFile $file,
32 $start,
33 $end) {
34
35 $start = (int)$start;
36 $end = (int)$end;
37
38 $chunks = $this->newChunkQuery($viewer, $file)
39 ->withByteRange($start, $end)
40 ->execute();
41
42 if (!$chunks) {
43 throw new Exception(
44 pht(
45 'There are no file data chunks in byte range %d - %d.',
46 $start,
47 $end));
48 }
49
50 if (count($chunks) !== 1) {
51 phlog($chunks);
52 throw new Exception(
53 pht(
54 'There are multiple chunks in byte range %d - %d.',
55 $start,
56 $end));
57 }
58
59 $chunk = head($chunks);
60 if ($chunk->getByteStart() != $start) {
61 throw new Exception(
62 pht(
63 'Chunk start byte is %d, not %d.',
64 $chunk->getByteStart(),
65 $start));
66 }
67
68 if ($chunk->getByteEnd() != $end) {
69 throw new Exception(
70 pht(
71 'Chunk end byte is %d, not %d.',
72 $chunk->getByteEnd(),
73 $end));
74 }
75
76 if ($chunk->getDataFilePHID()) {
77 throw new Exception(
78 pht('Chunk has already been uploaded.'));
79 }
80
81 return $chunk;
82 }
83
84 protected function decodeBase64($data) {
85 $data = base64_decode($data, $strict = true);
86 if ($data === false) {
87 throw new Exception(pht('Unable to decode base64 data!'));
88 }
89 return $data;
90 }
91
92 protected function loadAnyMissingChunk(
93 PhabricatorUser $viewer,
94 PhabricatorFile $file) {
95
96 return $this->newChunkQuery($viewer, $file)
97 ->withIsComplete(false)
98 ->setLimit(1)
99 ->execute();
100 }
101
102 private function newChunkQuery(
103 PhabricatorUser $viewer,
104 PhabricatorFile $file) {
105
106 $engine = $file->instantiateStorageEngine();
107 if (!$engine->isChunkEngine()) {
108 throw new Exception(
109 pht(
110 'File "%s" does not have chunks!',
111 $file->getPHID()));
112 }
113
114 return id(new PhabricatorFileChunkQuery())
115 ->setViewer($viewer)
116 ->withChunkHandles(array($file->getStorageHandle()));
117 }
118
119
120}