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