Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/conduit/FileConduitAPIMethod.php
12241 views
1
<?php
2
3
abstract class FileConduitAPIMethod extends ConduitAPIMethod {
4
5
final public function getApplication() {
6
return PhabricatorApplication::getByClass('PhabricatorFilesApplication');
7
}
8
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));
16
}
17
18
return $file;
19
}
20
21
protected function loadFileChunks(
22
PhabricatorUser $viewer,
23
PhabricatorFile $file) {
24
return $this->newChunkQuery($viewer, $file)
25
->execute();
26
}
27
28
protected function loadFileChunkForUpload(
29
PhabricatorUser $viewer,
30
PhabricatorFile $file,
31
$start,
32
$end) {
33
34
$start = (int)$start;
35
$end = (int)$end;
36
37
$chunks = $this->newChunkQuery($viewer, $file)
38
->withByteRange($start, $end)
39
->execute();
40
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));
47
}
48
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));
56
}
57
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));
65
}
66
67
if ($chunk->getByteEnd() != $end) {
68
throw new Exception(
69
pht(
70
'Chunk end byte is %d, not %d.',
71
$chunk->getByteEnd(),
72
$end));
73
}
74
75
if ($chunk->getDataFilePHID()) {
76
throw new Exception(
77
pht('Chunk has already been uploaded.'));
78
}
79
80
return $chunk;
81
}
82
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!'));
87
}
88
return $data;
89
}
90
91
protected function loadAnyMissingChunk(
92
PhabricatorUser $viewer,
93
PhabricatorFile $file) {
94
95
return $this->newChunkQuery($viewer, $file)
96
->withIsComplete(false)
97
->setLimit(1)
98
->execute();
99
}
100
101
private function newChunkQuery(
102
PhabricatorUser $viewer,
103
PhabricatorFile $file) {
104
105
$engine = $file->instantiateStorageEngine();
106
if (!$engine->isChunkEngine()) {
107
throw new Exception(
108
pht(
109
'File "%s" does not have chunks!',
110
$file->getPHID()));
111
}
112
113
return id(new PhabricatorFileChunkQuery())
114
->setViewer($viewer)
115
->withChunkHandles(array($file->getStorageHandle()));
116
}
117
118
119
}
120
121