Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/diffusion/query/DiffusionFileFutureQuery.php
12242 views
1
<?php
2
3
abstract class DiffusionFileFutureQuery
4
extends DiffusionQuery {
5
6
private $timeout;
7
private $byteLimit;
8
9
private $didHitByteLimit = false;
10
private $didHitTimeLimit = false;
11
12
public static function getConduitParameters() {
13
return array(
14
'timeout' => 'optional int',
15
'byteLimit' => 'optional int',
16
);
17
}
18
19
public function setTimeout($timeout) {
20
$this->timeout = $timeout;
21
return $this;
22
}
23
24
public function getTimeout() {
25
return $this->timeout;
26
}
27
28
public function setByteLimit($byte_limit) {
29
$this->byteLimit = $byte_limit;
30
return $this;
31
}
32
33
public function getByteLimit() {
34
return $this->byteLimit;
35
}
36
37
final public function getExceededByteLimit() {
38
return $this->didHitByteLimit;
39
}
40
41
final public function getExceededTimeLimit() {
42
return $this->didHitTimeLimit;
43
}
44
45
abstract protected function newQueryFuture();
46
47
final public function respondToConduitRequest(ConduitAPIRequest $request) {
48
$drequest = $this->getRequest();
49
50
$timeout = $request->getValue('timeout');
51
if ($timeout) {
52
$this->setTimeout($timeout);
53
}
54
55
$byte_limit = $request->getValue('byteLimit');
56
if ($byte_limit) {
57
$this->setByteLimit($byte_limit);
58
}
59
60
$file = $this->execute();
61
62
$too_slow = (bool)$this->getExceededTimeLimit();
63
$too_huge = (bool)$this->getExceededByteLimit();
64
65
$file_phid = null;
66
if (!$too_slow && !$too_huge) {
67
$repository = $drequest->getRepository();
68
$repository_phid = $repository->getPHID();
69
70
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
71
$file->attachToObject($repository_phid);
72
unset($unguarded);
73
74
$file_phid = $file->getPHID();
75
}
76
77
return array(
78
'tooSlow' => $too_slow,
79
'tooHuge' => $too_huge,
80
'filePHID' => $file_phid,
81
);
82
}
83
84
final public function executeInline() {
85
$future = $this->newConfiguredQueryFuture();
86
list($stdout) = $future->resolvex();
87
return $stdout;
88
}
89
90
final protected function executeQuery() {
91
$future = $this->newConfiguredQueryFuture();
92
93
$drequest = $this->getRequest();
94
95
$name = '';
96
if ($drequest->getPath() !== null) {
97
$name = basename($drequest->getPath());
98
}
99
$relative_ttl = phutil_units('48 hours in seconds');
100
101
try {
102
$threshold = PhabricatorFileStorageEngine::getChunkThreshold();
103
$future->setReadBufferSize($threshold);
104
105
$source = id(new PhabricatorExecFutureFileUploadSource())
106
->setName($name)
107
->setRelativeTTL($relative_ttl)
108
->setViewPolicy(PhabricatorPolicies::POLICY_NOONE)
109
->setExecFuture($future);
110
111
$byte_limit = $this->getByteLimit();
112
if ($byte_limit) {
113
$source->setByteLimit($byte_limit);
114
}
115
116
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
117
$file = $source->uploadFile();
118
unset($unguarded);
119
120
} catch (CommandException $ex) {
121
if (!$future->getWasKilledByTimeout()) {
122
throw $ex;
123
}
124
125
$this->didHitTimeLimit = true;
126
$file = null;
127
} catch (PhabricatorFileUploadSourceByteLimitException $ex) {
128
$this->didHitByteLimit = true;
129
$file = null;
130
}
131
132
return $file;
133
}
134
135
private function newConfiguredQueryFuture() {
136
$future = $this->newQueryFuture();
137
138
if ($this->getTimeout()) {
139
$future->setTimeout($this->getTimeout());
140
}
141
142
return $future;
143
}
144
145
}
146
147