@@ -402,6 +402,9 @@ |
||
402 | 402 | } |
403 | 403 | } |
404 | 404 | |
405 | + /** |
|
406 | + * @param string $path |
|
407 | + */ |
|
405 | 408 | private function getParentPath($path) { |
406 | 409 | $parent = dirname($path); |
407 | 410 | if ($parent === '.') { |
@@ -52,780 +52,780 @@ |
||
52 | 52 | * - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater |
53 | 53 | */ |
54 | 54 | class Cache implements ICache { |
55 | - use MoveFromCacheTrait { |
|
56 | - MoveFromCacheTrait::moveFromCache as moveFromCacheFallback; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @var array partial data for the cache |
|
61 | - */ |
|
62 | - protected $partial = array(); |
|
63 | - |
|
64 | - /** |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - protected $storageId; |
|
68 | - |
|
69 | - /** |
|
70 | - * @var Storage $storageCache |
|
71 | - */ |
|
72 | - protected $storageCache; |
|
73 | - |
|
74 | - /** @var IMimeTypeLoader */ |
|
75 | - protected $mimetypeLoader; |
|
76 | - |
|
77 | - /** |
|
78 | - * @var IDBConnection |
|
79 | - */ |
|
80 | - protected $connection; |
|
81 | - |
|
82 | - /** |
|
83 | - * @param \OC\Files\Storage\Storage|string $storage |
|
84 | - */ |
|
85 | - public function __construct($storage) { |
|
86 | - if ($storage instanceof \OC\Files\Storage\Storage) { |
|
87 | - $this->storageId = $storage->getId(); |
|
88 | - } else { |
|
89 | - $this->storageId = $storage; |
|
90 | - } |
|
91 | - if (strlen($this->storageId) > 64) { |
|
92 | - $this->storageId = md5($this->storageId); |
|
93 | - } |
|
94 | - |
|
95 | - $this->storageCache = new Storage($storage); |
|
96 | - $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
97 | - $this->connection = \OC::$server->getDatabaseConnection(); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Get the numeric storage id for this cache's storage |
|
102 | - * |
|
103 | - * @return int |
|
104 | - */ |
|
105 | - public function getNumericStorageId() { |
|
106 | - return $this->storageCache->getNumericId(); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * get the stored metadata of a file or folder |
|
111 | - * |
|
112 | - * @param string | int $file either the path of a file or folder or the file id for a file or folder |
|
113 | - * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache |
|
114 | - */ |
|
115 | - public function get($file) { |
|
116 | - if (is_string($file) or $file == '') { |
|
117 | - // normalize file |
|
118 | - $file = $this->normalize($file); |
|
119 | - |
|
120 | - $where = 'WHERE `storage` = ? AND `path_hash` = ?'; |
|
121 | - $params = array($this->getNumericStorageId(), md5($file)); |
|
122 | - } else { //file id |
|
123 | - $where = 'WHERE `fileid` = ?'; |
|
124 | - $params = array($file); |
|
125 | - } |
|
126 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
55 | + use MoveFromCacheTrait { |
|
56 | + MoveFromCacheTrait::moveFromCache as moveFromCacheFallback; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @var array partial data for the cache |
|
61 | + */ |
|
62 | + protected $partial = array(); |
|
63 | + |
|
64 | + /** |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + protected $storageId; |
|
68 | + |
|
69 | + /** |
|
70 | + * @var Storage $storageCache |
|
71 | + */ |
|
72 | + protected $storageCache; |
|
73 | + |
|
74 | + /** @var IMimeTypeLoader */ |
|
75 | + protected $mimetypeLoader; |
|
76 | + |
|
77 | + /** |
|
78 | + * @var IDBConnection |
|
79 | + */ |
|
80 | + protected $connection; |
|
81 | + |
|
82 | + /** |
|
83 | + * @param \OC\Files\Storage\Storage|string $storage |
|
84 | + */ |
|
85 | + public function __construct($storage) { |
|
86 | + if ($storage instanceof \OC\Files\Storage\Storage) { |
|
87 | + $this->storageId = $storage->getId(); |
|
88 | + } else { |
|
89 | + $this->storageId = $storage; |
|
90 | + } |
|
91 | + if (strlen($this->storageId) > 64) { |
|
92 | + $this->storageId = md5($this->storageId); |
|
93 | + } |
|
94 | + |
|
95 | + $this->storageCache = new Storage($storage); |
|
96 | + $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
97 | + $this->connection = \OC::$server->getDatabaseConnection(); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Get the numeric storage id for this cache's storage |
|
102 | + * |
|
103 | + * @return int |
|
104 | + */ |
|
105 | + public function getNumericStorageId() { |
|
106 | + return $this->storageCache->getNumericId(); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * get the stored metadata of a file or folder |
|
111 | + * |
|
112 | + * @param string | int $file either the path of a file or folder or the file id for a file or folder |
|
113 | + * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache |
|
114 | + */ |
|
115 | + public function get($file) { |
|
116 | + if (is_string($file) or $file == '') { |
|
117 | + // normalize file |
|
118 | + $file = $this->normalize($file); |
|
119 | + |
|
120 | + $where = 'WHERE `storage` = ? AND `path_hash` = ?'; |
|
121 | + $params = array($this->getNumericStorageId(), md5($file)); |
|
122 | + } else { //file id |
|
123 | + $where = 'WHERE `fileid` = ?'; |
|
124 | + $params = array($file); |
|
125 | + } |
|
126 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
127 | 127 | `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
128 | 128 | FROM `*PREFIX*filecache` ' . $where; |
129 | - $result = $this->connection->executeQuery($sql, $params); |
|
130 | - $data = $result->fetch(); |
|
131 | - |
|
132 | - //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO |
|
133 | - //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false |
|
134 | - if ($data === null) { |
|
135 | - $data = false; |
|
136 | - } |
|
137 | - |
|
138 | - //merge partial data |
|
139 | - if (!$data and is_string($file)) { |
|
140 | - if (isset($this->partial[$file])) { |
|
141 | - $data = $this->partial[$file]; |
|
142 | - } |
|
143 | - return $data; |
|
144 | - } else { |
|
145 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Create a CacheEntry from database row |
|
151 | - * |
|
152 | - * @param array $data |
|
153 | - * @param IMimeTypeLoader $mimetypeLoader |
|
154 | - * @return CacheEntry |
|
155 | - */ |
|
156 | - public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
|
157 | - //fix types |
|
158 | - $data['fileid'] = (int)$data['fileid']; |
|
159 | - $data['parent'] = (int)$data['parent']; |
|
160 | - $data['size'] = 0 + $data['size']; |
|
161 | - $data['mtime'] = (int)$data['mtime']; |
|
162 | - $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
163 | - $data['encryptedVersion'] = (int)$data['encrypted']; |
|
164 | - $data['encrypted'] = (bool)$data['encrypted']; |
|
165 | - $data['storage_id'] = $data['storage']; |
|
166 | - $data['storage'] = (int)$data['storage']; |
|
167 | - $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
|
168 | - $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
|
169 | - if ($data['storage_mtime'] == 0) { |
|
170 | - $data['storage_mtime'] = $data['mtime']; |
|
171 | - } |
|
172 | - $data['permissions'] = (int)$data['permissions']; |
|
173 | - return new CacheEntry($data); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * get the metadata of all files stored in $folder |
|
178 | - * |
|
179 | - * @param string $folder |
|
180 | - * @return ICacheEntry[] |
|
181 | - */ |
|
182 | - public function getFolderContents($folder) { |
|
183 | - $fileId = $this->getId($folder); |
|
184 | - return $this->getFolderContentsById($fileId); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * get the metadata of all files stored in $folder |
|
189 | - * |
|
190 | - * @param int $fileId the file id of the folder |
|
191 | - * @return ICacheEntry[] |
|
192 | - */ |
|
193 | - public function getFolderContentsById($fileId) { |
|
194 | - if ($fileId > -1) { |
|
195 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
129 | + $result = $this->connection->executeQuery($sql, $params); |
|
130 | + $data = $result->fetch(); |
|
131 | + |
|
132 | + //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO |
|
133 | + //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false |
|
134 | + if ($data === null) { |
|
135 | + $data = false; |
|
136 | + } |
|
137 | + |
|
138 | + //merge partial data |
|
139 | + if (!$data and is_string($file)) { |
|
140 | + if (isset($this->partial[$file])) { |
|
141 | + $data = $this->partial[$file]; |
|
142 | + } |
|
143 | + return $data; |
|
144 | + } else { |
|
145 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Create a CacheEntry from database row |
|
151 | + * |
|
152 | + * @param array $data |
|
153 | + * @param IMimeTypeLoader $mimetypeLoader |
|
154 | + * @return CacheEntry |
|
155 | + */ |
|
156 | + public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
|
157 | + //fix types |
|
158 | + $data['fileid'] = (int)$data['fileid']; |
|
159 | + $data['parent'] = (int)$data['parent']; |
|
160 | + $data['size'] = 0 + $data['size']; |
|
161 | + $data['mtime'] = (int)$data['mtime']; |
|
162 | + $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
163 | + $data['encryptedVersion'] = (int)$data['encrypted']; |
|
164 | + $data['encrypted'] = (bool)$data['encrypted']; |
|
165 | + $data['storage_id'] = $data['storage']; |
|
166 | + $data['storage'] = (int)$data['storage']; |
|
167 | + $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
|
168 | + $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
|
169 | + if ($data['storage_mtime'] == 0) { |
|
170 | + $data['storage_mtime'] = $data['mtime']; |
|
171 | + } |
|
172 | + $data['permissions'] = (int)$data['permissions']; |
|
173 | + return new CacheEntry($data); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * get the metadata of all files stored in $folder |
|
178 | + * |
|
179 | + * @param string $folder |
|
180 | + * @return ICacheEntry[] |
|
181 | + */ |
|
182 | + public function getFolderContents($folder) { |
|
183 | + $fileId = $this->getId($folder); |
|
184 | + return $this->getFolderContentsById($fileId); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * get the metadata of all files stored in $folder |
|
189 | + * |
|
190 | + * @param int $fileId the file id of the folder |
|
191 | + * @return ICacheEntry[] |
|
192 | + */ |
|
193 | + public function getFolderContentsById($fileId) { |
|
194 | + if ($fileId > -1) { |
|
195 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
196 | 196 | `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
197 | 197 | FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC'; |
198 | - $result = $this->connection->executeQuery($sql, [$fileId]); |
|
199 | - $files = $result->fetchAll(); |
|
200 | - return array_map(function (array $data) { |
|
201 | - return self::cacheEntryFromData($data, $this->mimetypeLoader);; |
|
202 | - }, $files); |
|
203 | - } else { |
|
204 | - return array(); |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * insert or update meta data for a file or folder |
|
210 | - * |
|
211 | - * @param string $file |
|
212 | - * @param array $data |
|
213 | - * |
|
214 | - * @return int file id |
|
215 | - * @throws \RuntimeException |
|
216 | - */ |
|
217 | - public function put($file, array $data) { |
|
218 | - if (($id = $this->getId($file)) > -1) { |
|
219 | - $this->update($id, $data); |
|
220 | - return $id; |
|
221 | - } else { |
|
222 | - return $this->insert($file, $data); |
|
223 | - } |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * insert meta data for a new file or folder |
|
228 | - * |
|
229 | - * @param string $file |
|
230 | - * @param array $data |
|
231 | - * |
|
232 | - * @return int file id |
|
233 | - * @throws \RuntimeException |
|
234 | - */ |
|
235 | - public function insert($file, array $data) { |
|
236 | - // normalize file |
|
237 | - $file = $this->normalize($file); |
|
238 | - |
|
239 | - if (isset($this->partial[$file])) { //add any saved partial data |
|
240 | - $data = array_merge($this->partial[$file], $data); |
|
241 | - unset($this->partial[$file]); |
|
242 | - } |
|
243 | - |
|
244 | - $requiredFields = array('size', 'mtime', 'mimetype'); |
|
245 | - foreach ($requiredFields as $field) { |
|
246 | - if (!isset($data[$field])) { //data not complete save as partial and return |
|
247 | - $this->partial[$file] = $data; |
|
248 | - return -1; |
|
249 | - } |
|
250 | - } |
|
251 | - |
|
252 | - $data['path'] = $file; |
|
253 | - $data['parent'] = $this->getParentId($file); |
|
254 | - $data['name'] = \OC_Util::basename($file); |
|
255 | - |
|
256 | - list($queryParts, $params) = $this->buildParts($data); |
|
257 | - $queryParts[] = '`storage`'; |
|
258 | - $params[] = $this->getNumericStorageId(); |
|
259 | - |
|
260 | - $queryParts = array_map(function ($item) { |
|
261 | - return trim($item, "`"); |
|
262 | - }, $queryParts); |
|
263 | - $values = array_combine($queryParts, $params); |
|
264 | - if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [ |
|
265 | - 'storage', |
|
266 | - 'path_hash', |
|
267 | - ]) |
|
268 | - ) { |
|
269 | - return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
270 | - } |
|
271 | - |
|
272 | - // The file was created in the mean time |
|
273 | - if (($id = $this->getId($file)) > -1) { |
|
274 | - $this->update($id, $data); |
|
275 | - return $id; |
|
276 | - } else { |
|
277 | - throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.'); |
|
278 | - } |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * update the metadata of an existing file or folder in the cache |
|
283 | - * |
|
284 | - * @param int $id the fileid of the existing file or folder |
|
285 | - * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged |
|
286 | - */ |
|
287 | - public function update($id, array $data) { |
|
288 | - |
|
289 | - if (isset($data['path'])) { |
|
290 | - // normalize path |
|
291 | - $data['path'] = $this->normalize($data['path']); |
|
292 | - } |
|
293 | - |
|
294 | - if (isset($data['name'])) { |
|
295 | - // normalize path |
|
296 | - $data['name'] = $this->normalize($data['name']); |
|
297 | - } |
|
298 | - |
|
299 | - list($queryParts, $params) = $this->buildParts($data); |
|
300 | - // duplicate $params because we need the parts twice in the SQL statement |
|
301 | - // once for the SET part, once in the WHERE clause |
|
302 | - $params = array_merge($params, $params); |
|
303 | - $params[] = $id; |
|
304 | - |
|
305 | - // don't update if the data we try to set is the same as the one in the record |
|
306 | - // some databases (Postgres) don't like superfluous updates |
|
307 | - $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
308 | - 'WHERE (' . |
|
309 | - implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
310 | - implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
311 | - ') AND `fileid` = ? '; |
|
312 | - $this->connection->executeQuery($sql, $params); |
|
313 | - |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * extract query parts and params array from data array |
|
318 | - * |
|
319 | - * @param array $data |
|
320 | - * @return array [$queryParts, $params] |
|
321 | - * $queryParts: string[], the (escaped) column names to be set in the query |
|
322 | - * $params: mixed[], the new values for the columns, to be passed as params to the query |
|
323 | - */ |
|
324 | - protected function buildParts(array $data) { |
|
325 | - $fields = array( |
|
326 | - 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', |
|
327 | - 'etag', 'permissions', 'checksum'); |
|
328 | - |
|
329 | - $doNotCopyStorageMTime = false; |
|
330 | - if (array_key_exists('mtime', $data) && $data['mtime'] === null) { |
|
331 | - // this horrific magic tells it to not copy storage_mtime to mtime |
|
332 | - unset($data['mtime']); |
|
333 | - $doNotCopyStorageMTime = true; |
|
334 | - } |
|
335 | - |
|
336 | - $params = array(); |
|
337 | - $queryParts = array(); |
|
338 | - foreach ($data as $name => $value) { |
|
339 | - if (array_search($name, $fields) !== false) { |
|
340 | - if ($name === 'path') { |
|
341 | - $params[] = md5($value); |
|
342 | - $queryParts[] = '`path_hash`'; |
|
343 | - } elseif ($name === 'mimetype') { |
|
344 | - $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/'))); |
|
345 | - $queryParts[] = '`mimepart`'; |
|
346 | - $value = $this->mimetypeLoader->getId($value); |
|
347 | - } elseif ($name === 'storage_mtime') { |
|
348 | - if (!$doNotCopyStorageMTime && !isset($data['mtime'])) { |
|
349 | - $params[] = $value; |
|
350 | - $queryParts[] = '`mtime`'; |
|
351 | - } |
|
352 | - } elseif ($name === 'encrypted') { |
|
353 | - if(isset($data['encryptedVersion'])) { |
|
354 | - $value = $data['encryptedVersion']; |
|
355 | - } else { |
|
356 | - // Boolean to integer conversion |
|
357 | - $value = $value ? 1 : 0; |
|
358 | - } |
|
359 | - } |
|
360 | - $params[] = $value; |
|
361 | - $queryParts[] = '`' . $name . '`'; |
|
362 | - } |
|
363 | - } |
|
364 | - return array($queryParts, $params); |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * get the file id for a file |
|
369 | - * |
|
370 | - * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file |
|
371 | - * |
|
372 | - * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing |
|
373 | - * |
|
374 | - * @param string $file |
|
375 | - * @return int |
|
376 | - */ |
|
377 | - public function getId($file) { |
|
378 | - // normalize file |
|
379 | - $file = $this->normalize($file); |
|
380 | - |
|
381 | - $pathHash = md5($file); |
|
382 | - |
|
383 | - $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
384 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
385 | - if ($row = $result->fetch()) { |
|
386 | - return $row['fileid']; |
|
387 | - } else { |
|
388 | - return -1; |
|
389 | - } |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * get the id of the parent folder of a file |
|
394 | - * |
|
395 | - * @param string $file |
|
396 | - * @return int |
|
397 | - */ |
|
398 | - public function getParentId($file) { |
|
399 | - if ($file === '') { |
|
400 | - return -1; |
|
401 | - } else { |
|
402 | - $parent = $this->getParentPath($file); |
|
403 | - return (int)$this->getId($parent); |
|
404 | - } |
|
405 | - } |
|
406 | - |
|
407 | - private function getParentPath($path) { |
|
408 | - $parent = dirname($path); |
|
409 | - if ($parent === '.') { |
|
410 | - $parent = ''; |
|
411 | - } |
|
412 | - return $parent; |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * check if a file is available in the cache |
|
417 | - * |
|
418 | - * @param string $file |
|
419 | - * @return bool |
|
420 | - */ |
|
421 | - public function inCache($file) { |
|
422 | - return $this->getId($file) != -1; |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * remove a file or folder from the cache |
|
427 | - * |
|
428 | - * when removing a folder from the cache all files and folders inside the folder will be removed as well |
|
429 | - * |
|
430 | - * @param string $file |
|
431 | - */ |
|
432 | - public function remove($file) { |
|
433 | - $entry = $this->get($file); |
|
434 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
435 | - $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
436 | - if ($entry['mimetype'] === 'httpd/unix-directory') { |
|
437 | - $this->removeChildren($entry); |
|
438 | - } |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * Get all sub folders of a folder |
|
443 | - * |
|
444 | - * @param array $entry the cache entry of the folder to get the subfolders for |
|
445 | - * @return array[] the cache entries for the subfolders |
|
446 | - */ |
|
447 | - private function getSubFolders($entry) { |
|
448 | - $children = $this->getFolderContentsById($entry['fileid']); |
|
449 | - return array_filter($children, function ($child) { |
|
450 | - return $child['mimetype'] === 'httpd/unix-directory'; |
|
451 | - }); |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Recursively remove all children of a folder |
|
456 | - * |
|
457 | - * @param array $entry the cache entry of the folder to remove the children of |
|
458 | - * @throws \OC\DatabaseException |
|
459 | - */ |
|
460 | - private function removeChildren($entry) { |
|
461 | - $subFolders = $this->getSubFolders($entry); |
|
462 | - foreach ($subFolders as $folder) { |
|
463 | - $this->removeChildren($folder); |
|
464 | - } |
|
465 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?'; |
|
466 | - $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
467 | - } |
|
468 | - |
|
469 | - /** |
|
470 | - * Move a file or folder in the cache |
|
471 | - * |
|
472 | - * @param string $source |
|
473 | - * @param string $target |
|
474 | - */ |
|
475 | - public function move($source, $target) { |
|
476 | - $this->moveFromCache($this, $source, $target); |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Get the storage id and path needed for a move |
|
481 | - * |
|
482 | - * @param string $path |
|
483 | - * @return array [$storageId, $internalPath] |
|
484 | - */ |
|
485 | - protected function getMoveInfo($path) { |
|
486 | - return [$this->getNumericStorageId(), $path]; |
|
487 | - } |
|
488 | - |
|
489 | - /** |
|
490 | - * Move a file or folder in the cache |
|
491 | - * |
|
492 | - * @param \OCP\Files\Cache\ICache $sourceCache |
|
493 | - * @param string $sourcePath |
|
494 | - * @param string $targetPath |
|
495 | - * @throws \OC\DatabaseException |
|
496 | - */ |
|
497 | - public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { |
|
498 | - if ($sourceCache instanceof Cache) { |
|
499 | - // normalize source and target |
|
500 | - $sourcePath = $this->normalize($sourcePath); |
|
501 | - $targetPath = $this->normalize($targetPath); |
|
502 | - |
|
503 | - $sourceData = $sourceCache->get($sourcePath); |
|
504 | - $sourceId = $sourceData['fileid']; |
|
505 | - $newParentId = $this->getParentId($targetPath); |
|
506 | - |
|
507 | - list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); |
|
508 | - list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); |
|
509 | - |
|
510 | - // sql for final update |
|
511 | - $moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?'; |
|
512 | - |
|
513 | - if ($sourceData['mimetype'] === 'httpd/unix-directory') { |
|
514 | - //find all child entries |
|
515 | - $sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?'; |
|
516 | - $result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']); |
|
517 | - $childEntries = $result->fetchAll(); |
|
518 | - $sourceLength = strlen($sourcePath); |
|
519 | - $this->connection->beginTransaction(); |
|
520 | - $query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?'); |
|
521 | - |
|
522 | - foreach ($childEntries as $child) { |
|
523 | - $newTargetPath = $targetPath . substr($child['path'], $sourceLength); |
|
524 | - $query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]); |
|
525 | - } |
|
526 | - $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); |
|
527 | - $this->connection->commit(); |
|
528 | - } else { |
|
529 | - $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); |
|
530 | - } |
|
531 | - } else { |
|
532 | - $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath); |
|
533 | - } |
|
534 | - } |
|
535 | - |
|
536 | - /** |
|
537 | - * remove all entries for files that are stored on the storage from the cache |
|
538 | - */ |
|
539 | - public function clear() { |
|
540 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
541 | - $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
542 | - |
|
543 | - $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'; |
|
544 | - $this->connection->executeQuery($sql, array($this->storageId)); |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * Get the scan status of a file |
|
549 | - * |
|
550 | - * - Cache::NOT_FOUND: File is not in the cache |
|
551 | - * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known |
|
552 | - * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned |
|
553 | - * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned |
|
554 | - * |
|
555 | - * @param string $file |
|
556 | - * |
|
557 | - * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
|
558 | - */ |
|
559 | - public function getStatus($file) { |
|
560 | - // normalize file |
|
561 | - $file = $this->normalize($file); |
|
562 | - |
|
563 | - $pathHash = md5($file); |
|
564 | - $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
565 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
566 | - if ($row = $result->fetch()) { |
|
567 | - if ((int)$row['size'] === -1) { |
|
568 | - return self::SHALLOW; |
|
569 | - } else { |
|
570 | - return self::COMPLETE; |
|
571 | - } |
|
572 | - } else { |
|
573 | - if (isset($this->partial[$file])) { |
|
574 | - return self::PARTIAL; |
|
575 | - } else { |
|
576 | - return self::NOT_FOUND; |
|
577 | - } |
|
578 | - } |
|
579 | - } |
|
580 | - |
|
581 | - /** |
|
582 | - * search for files matching $pattern |
|
583 | - * |
|
584 | - * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%') |
|
585 | - * @return ICacheEntry[] an array of cache entries where the name matches the search pattern |
|
586 | - */ |
|
587 | - public function search($pattern) { |
|
588 | - // normalize pattern |
|
589 | - $pattern = $this->normalize($pattern); |
|
590 | - |
|
591 | - |
|
592 | - $sql = ' |
|
198 | + $result = $this->connection->executeQuery($sql, [$fileId]); |
|
199 | + $files = $result->fetchAll(); |
|
200 | + return array_map(function (array $data) { |
|
201 | + return self::cacheEntryFromData($data, $this->mimetypeLoader);; |
|
202 | + }, $files); |
|
203 | + } else { |
|
204 | + return array(); |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * insert or update meta data for a file or folder |
|
210 | + * |
|
211 | + * @param string $file |
|
212 | + * @param array $data |
|
213 | + * |
|
214 | + * @return int file id |
|
215 | + * @throws \RuntimeException |
|
216 | + */ |
|
217 | + public function put($file, array $data) { |
|
218 | + if (($id = $this->getId($file)) > -1) { |
|
219 | + $this->update($id, $data); |
|
220 | + return $id; |
|
221 | + } else { |
|
222 | + return $this->insert($file, $data); |
|
223 | + } |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * insert meta data for a new file or folder |
|
228 | + * |
|
229 | + * @param string $file |
|
230 | + * @param array $data |
|
231 | + * |
|
232 | + * @return int file id |
|
233 | + * @throws \RuntimeException |
|
234 | + */ |
|
235 | + public function insert($file, array $data) { |
|
236 | + // normalize file |
|
237 | + $file = $this->normalize($file); |
|
238 | + |
|
239 | + if (isset($this->partial[$file])) { //add any saved partial data |
|
240 | + $data = array_merge($this->partial[$file], $data); |
|
241 | + unset($this->partial[$file]); |
|
242 | + } |
|
243 | + |
|
244 | + $requiredFields = array('size', 'mtime', 'mimetype'); |
|
245 | + foreach ($requiredFields as $field) { |
|
246 | + if (!isset($data[$field])) { //data not complete save as partial and return |
|
247 | + $this->partial[$file] = $data; |
|
248 | + return -1; |
|
249 | + } |
|
250 | + } |
|
251 | + |
|
252 | + $data['path'] = $file; |
|
253 | + $data['parent'] = $this->getParentId($file); |
|
254 | + $data['name'] = \OC_Util::basename($file); |
|
255 | + |
|
256 | + list($queryParts, $params) = $this->buildParts($data); |
|
257 | + $queryParts[] = '`storage`'; |
|
258 | + $params[] = $this->getNumericStorageId(); |
|
259 | + |
|
260 | + $queryParts = array_map(function ($item) { |
|
261 | + return trim($item, "`"); |
|
262 | + }, $queryParts); |
|
263 | + $values = array_combine($queryParts, $params); |
|
264 | + if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [ |
|
265 | + 'storage', |
|
266 | + 'path_hash', |
|
267 | + ]) |
|
268 | + ) { |
|
269 | + return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
270 | + } |
|
271 | + |
|
272 | + // The file was created in the mean time |
|
273 | + if (($id = $this->getId($file)) > -1) { |
|
274 | + $this->update($id, $data); |
|
275 | + return $id; |
|
276 | + } else { |
|
277 | + throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.'); |
|
278 | + } |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * update the metadata of an existing file or folder in the cache |
|
283 | + * |
|
284 | + * @param int $id the fileid of the existing file or folder |
|
285 | + * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged |
|
286 | + */ |
|
287 | + public function update($id, array $data) { |
|
288 | + |
|
289 | + if (isset($data['path'])) { |
|
290 | + // normalize path |
|
291 | + $data['path'] = $this->normalize($data['path']); |
|
292 | + } |
|
293 | + |
|
294 | + if (isset($data['name'])) { |
|
295 | + // normalize path |
|
296 | + $data['name'] = $this->normalize($data['name']); |
|
297 | + } |
|
298 | + |
|
299 | + list($queryParts, $params) = $this->buildParts($data); |
|
300 | + // duplicate $params because we need the parts twice in the SQL statement |
|
301 | + // once for the SET part, once in the WHERE clause |
|
302 | + $params = array_merge($params, $params); |
|
303 | + $params[] = $id; |
|
304 | + |
|
305 | + // don't update if the data we try to set is the same as the one in the record |
|
306 | + // some databases (Postgres) don't like superfluous updates |
|
307 | + $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
308 | + 'WHERE (' . |
|
309 | + implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
310 | + implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
311 | + ') AND `fileid` = ? '; |
|
312 | + $this->connection->executeQuery($sql, $params); |
|
313 | + |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * extract query parts and params array from data array |
|
318 | + * |
|
319 | + * @param array $data |
|
320 | + * @return array [$queryParts, $params] |
|
321 | + * $queryParts: string[], the (escaped) column names to be set in the query |
|
322 | + * $params: mixed[], the new values for the columns, to be passed as params to the query |
|
323 | + */ |
|
324 | + protected function buildParts(array $data) { |
|
325 | + $fields = array( |
|
326 | + 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', |
|
327 | + 'etag', 'permissions', 'checksum'); |
|
328 | + |
|
329 | + $doNotCopyStorageMTime = false; |
|
330 | + if (array_key_exists('mtime', $data) && $data['mtime'] === null) { |
|
331 | + // this horrific magic tells it to not copy storage_mtime to mtime |
|
332 | + unset($data['mtime']); |
|
333 | + $doNotCopyStorageMTime = true; |
|
334 | + } |
|
335 | + |
|
336 | + $params = array(); |
|
337 | + $queryParts = array(); |
|
338 | + foreach ($data as $name => $value) { |
|
339 | + if (array_search($name, $fields) !== false) { |
|
340 | + if ($name === 'path') { |
|
341 | + $params[] = md5($value); |
|
342 | + $queryParts[] = '`path_hash`'; |
|
343 | + } elseif ($name === 'mimetype') { |
|
344 | + $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/'))); |
|
345 | + $queryParts[] = '`mimepart`'; |
|
346 | + $value = $this->mimetypeLoader->getId($value); |
|
347 | + } elseif ($name === 'storage_mtime') { |
|
348 | + if (!$doNotCopyStorageMTime && !isset($data['mtime'])) { |
|
349 | + $params[] = $value; |
|
350 | + $queryParts[] = '`mtime`'; |
|
351 | + } |
|
352 | + } elseif ($name === 'encrypted') { |
|
353 | + if(isset($data['encryptedVersion'])) { |
|
354 | + $value = $data['encryptedVersion']; |
|
355 | + } else { |
|
356 | + // Boolean to integer conversion |
|
357 | + $value = $value ? 1 : 0; |
|
358 | + } |
|
359 | + } |
|
360 | + $params[] = $value; |
|
361 | + $queryParts[] = '`' . $name . '`'; |
|
362 | + } |
|
363 | + } |
|
364 | + return array($queryParts, $params); |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * get the file id for a file |
|
369 | + * |
|
370 | + * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file |
|
371 | + * |
|
372 | + * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing |
|
373 | + * |
|
374 | + * @param string $file |
|
375 | + * @return int |
|
376 | + */ |
|
377 | + public function getId($file) { |
|
378 | + // normalize file |
|
379 | + $file = $this->normalize($file); |
|
380 | + |
|
381 | + $pathHash = md5($file); |
|
382 | + |
|
383 | + $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
384 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
385 | + if ($row = $result->fetch()) { |
|
386 | + return $row['fileid']; |
|
387 | + } else { |
|
388 | + return -1; |
|
389 | + } |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * get the id of the parent folder of a file |
|
394 | + * |
|
395 | + * @param string $file |
|
396 | + * @return int |
|
397 | + */ |
|
398 | + public function getParentId($file) { |
|
399 | + if ($file === '') { |
|
400 | + return -1; |
|
401 | + } else { |
|
402 | + $parent = $this->getParentPath($file); |
|
403 | + return (int)$this->getId($parent); |
|
404 | + } |
|
405 | + } |
|
406 | + |
|
407 | + private function getParentPath($path) { |
|
408 | + $parent = dirname($path); |
|
409 | + if ($parent === '.') { |
|
410 | + $parent = ''; |
|
411 | + } |
|
412 | + return $parent; |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * check if a file is available in the cache |
|
417 | + * |
|
418 | + * @param string $file |
|
419 | + * @return bool |
|
420 | + */ |
|
421 | + public function inCache($file) { |
|
422 | + return $this->getId($file) != -1; |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * remove a file or folder from the cache |
|
427 | + * |
|
428 | + * when removing a folder from the cache all files and folders inside the folder will be removed as well |
|
429 | + * |
|
430 | + * @param string $file |
|
431 | + */ |
|
432 | + public function remove($file) { |
|
433 | + $entry = $this->get($file); |
|
434 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
435 | + $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
436 | + if ($entry['mimetype'] === 'httpd/unix-directory') { |
|
437 | + $this->removeChildren($entry); |
|
438 | + } |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * Get all sub folders of a folder |
|
443 | + * |
|
444 | + * @param array $entry the cache entry of the folder to get the subfolders for |
|
445 | + * @return array[] the cache entries for the subfolders |
|
446 | + */ |
|
447 | + private function getSubFolders($entry) { |
|
448 | + $children = $this->getFolderContentsById($entry['fileid']); |
|
449 | + return array_filter($children, function ($child) { |
|
450 | + return $child['mimetype'] === 'httpd/unix-directory'; |
|
451 | + }); |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Recursively remove all children of a folder |
|
456 | + * |
|
457 | + * @param array $entry the cache entry of the folder to remove the children of |
|
458 | + * @throws \OC\DatabaseException |
|
459 | + */ |
|
460 | + private function removeChildren($entry) { |
|
461 | + $subFolders = $this->getSubFolders($entry); |
|
462 | + foreach ($subFolders as $folder) { |
|
463 | + $this->removeChildren($folder); |
|
464 | + } |
|
465 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?'; |
|
466 | + $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
467 | + } |
|
468 | + |
|
469 | + /** |
|
470 | + * Move a file or folder in the cache |
|
471 | + * |
|
472 | + * @param string $source |
|
473 | + * @param string $target |
|
474 | + */ |
|
475 | + public function move($source, $target) { |
|
476 | + $this->moveFromCache($this, $source, $target); |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Get the storage id and path needed for a move |
|
481 | + * |
|
482 | + * @param string $path |
|
483 | + * @return array [$storageId, $internalPath] |
|
484 | + */ |
|
485 | + protected function getMoveInfo($path) { |
|
486 | + return [$this->getNumericStorageId(), $path]; |
|
487 | + } |
|
488 | + |
|
489 | + /** |
|
490 | + * Move a file or folder in the cache |
|
491 | + * |
|
492 | + * @param \OCP\Files\Cache\ICache $sourceCache |
|
493 | + * @param string $sourcePath |
|
494 | + * @param string $targetPath |
|
495 | + * @throws \OC\DatabaseException |
|
496 | + */ |
|
497 | + public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { |
|
498 | + if ($sourceCache instanceof Cache) { |
|
499 | + // normalize source and target |
|
500 | + $sourcePath = $this->normalize($sourcePath); |
|
501 | + $targetPath = $this->normalize($targetPath); |
|
502 | + |
|
503 | + $sourceData = $sourceCache->get($sourcePath); |
|
504 | + $sourceId = $sourceData['fileid']; |
|
505 | + $newParentId = $this->getParentId($targetPath); |
|
506 | + |
|
507 | + list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); |
|
508 | + list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); |
|
509 | + |
|
510 | + // sql for final update |
|
511 | + $moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?'; |
|
512 | + |
|
513 | + if ($sourceData['mimetype'] === 'httpd/unix-directory') { |
|
514 | + //find all child entries |
|
515 | + $sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?'; |
|
516 | + $result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']); |
|
517 | + $childEntries = $result->fetchAll(); |
|
518 | + $sourceLength = strlen($sourcePath); |
|
519 | + $this->connection->beginTransaction(); |
|
520 | + $query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?'); |
|
521 | + |
|
522 | + foreach ($childEntries as $child) { |
|
523 | + $newTargetPath = $targetPath . substr($child['path'], $sourceLength); |
|
524 | + $query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]); |
|
525 | + } |
|
526 | + $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); |
|
527 | + $this->connection->commit(); |
|
528 | + } else { |
|
529 | + $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); |
|
530 | + } |
|
531 | + } else { |
|
532 | + $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath); |
|
533 | + } |
|
534 | + } |
|
535 | + |
|
536 | + /** |
|
537 | + * remove all entries for files that are stored on the storage from the cache |
|
538 | + */ |
|
539 | + public function clear() { |
|
540 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
541 | + $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
542 | + |
|
543 | + $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'; |
|
544 | + $this->connection->executeQuery($sql, array($this->storageId)); |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * Get the scan status of a file |
|
549 | + * |
|
550 | + * - Cache::NOT_FOUND: File is not in the cache |
|
551 | + * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known |
|
552 | + * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned |
|
553 | + * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned |
|
554 | + * |
|
555 | + * @param string $file |
|
556 | + * |
|
557 | + * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
|
558 | + */ |
|
559 | + public function getStatus($file) { |
|
560 | + // normalize file |
|
561 | + $file = $this->normalize($file); |
|
562 | + |
|
563 | + $pathHash = md5($file); |
|
564 | + $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
565 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
566 | + if ($row = $result->fetch()) { |
|
567 | + if ((int)$row['size'] === -1) { |
|
568 | + return self::SHALLOW; |
|
569 | + } else { |
|
570 | + return self::COMPLETE; |
|
571 | + } |
|
572 | + } else { |
|
573 | + if (isset($this->partial[$file])) { |
|
574 | + return self::PARTIAL; |
|
575 | + } else { |
|
576 | + return self::NOT_FOUND; |
|
577 | + } |
|
578 | + } |
|
579 | + } |
|
580 | + |
|
581 | + /** |
|
582 | + * search for files matching $pattern |
|
583 | + * |
|
584 | + * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%') |
|
585 | + * @return ICacheEntry[] an array of cache entries where the name matches the search pattern |
|
586 | + */ |
|
587 | + public function search($pattern) { |
|
588 | + // normalize pattern |
|
589 | + $pattern = $this->normalize($pattern); |
|
590 | + |
|
591 | + |
|
592 | + $sql = ' |
|
593 | 593 | SELECT `fileid`, `storage`, `path`, `parent`, `name`, |
594 | 594 | `mimetype`, `storage_mtime`, `mimepart`, `size`, `mtime`, |
595 | 595 | `encrypted`, `etag`, `permissions`, `checksum` |
596 | 596 | FROM `*PREFIX*filecache` |
597 | 597 | WHERE `storage` = ? AND `name` ILIKE ?'; |
598 | - $result = $this->connection->executeQuery($sql, |
|
599 | - [$this->getNumericStorageId(), $pattern] |
|
600 | - ); |
|
601 | - |
|
602 | - $files = $result->fetchAll(); |
|
603 | - |
|
604 | - return array_map(function(array $data) { |
|
605 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
606 | - }, $files); |
|
607 | - } |
|
608 | - |
|
609 | - /** |
|
610 | - * search for files by mimetype |
|
611 | - * |
|
612 | - * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image') |
|
613 | - * where it will search for all mimetypes in the group ('image/*') |
|
614 | - * @return ICacheEntry[] an array of cache entries where the mimetype matches the search |
|
615 | - */ |
|
616 | - public function searchByMime($mimetype) { |
|
617 | - if (strpos($mimetype, '/')) { |
|
618 | - $where = '`mimetype` = ?'; |
|
619 | - } else { |
|
620 | - $where = '`mimepart` = ?'; |
|
621 | - } |
|
622 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
|
598 | + $result = $this->connection->executeQuery($sql, |
|
599 | + [$this->getNumericStorageId(), $pattern] |
|
600 | + ); |
|
601 | + |
|
602 | + $files = $result->fetchAll(); |
|
603 | + |
|
604 | + return array_map(function(array $data) { |
|
605 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
606 | + }, $files); |
|
607 | + } |
|
608 | + |
|
609 | + /** |
|
610 | + * search for files by mimetype |
|
611 | + * |
|
612 | + * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image') |
|
613 | + * where it will search for all mimetypes in the group ('image/*') |
|
614 | + * @return ICacheEntry[] an array of cache entries where the mimetype matches the search |
|
615 | + */ |
|
616 | + public function searchByMime($mimetype) { |
|
617 | + if (strpos($mimetype, '/')) { |
|
618 | + $where = '`mimetype` = ?'; |
|
619 | + } else { |
|
620 | + $where = '`mimepart` = ?'; |
|
621 | + } |
|
622 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
|
623 | 623 | FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'; |
624 | - $mimetype = $this->mimetypeLoader->getId($mimetype); |
|
625 | - $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
|
626 | - |
|
627 | - $files = $result->fetchAll(); |
|
628 | - |
|
629 | - return array_map(function (array $data) { |
|
630 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
631 | - }, $files); |
|
632 | - } |
|
633 | - |
|
634 | - /** |
|
635 | - * Search for files by tag of a given users. |
|
636 | - * |
|
637 | - * Note that every user can tag files differently. |
|
638 | - * |
|
639 | - * @param string|int $tag name or tag id |
|
640 | - * @param string $userId owner of the tags |
|
641 | - * @return ICacheEntry[] file data |
|
642 | - */ |
|
643 | - public function searchByTag($tag, $userId) { |
|
644 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
645 | - '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
646 | - '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
647 | - 'FROM `*PREFIX*filecache` `file`, ' . |
|
648 | - '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
649 | - '`*PREFIX*vcategory` `tag` ' . |
|
650 | - // JOIN filecache to vcategory_to_object |
|
651 | - 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
652 | - // JOIN vcategory_to_object to vcategory |
|
653 | - 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
654 | - 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
655 | - // conditions |
|
656 | - 'AND `file`.`storage` = ? ' . |
|
657 | - 'AND `tag`.`type` = \'files\' ' . |
|
658 | - 'AND `tag`.`uid` = ? '; |
|
659 | - if (is_int($tag)) { |
|
660 | - $sql .= 'AND `tag`.`id` = ? '; |
|
661 | - } else { |
|
662 | - $sql .= 'AND `tag`.`category` = ? '; |
|
663 | - } |
|
664 | - $result = $this->connection->executeQuery( |
|
665 | - $sql, |
|
666 | - [ |
|
667 | - $this->getNumericStorageId(), |
|
668 | - $userId, |
|
669 | - $tag |
|
670 | - ] |
|
671 | - ); |
|
672 | - |
|
673 | - $files = $result->fetchAll(); |
|
674 | - |
|
675 | - return array_map(function (array $data) { |
|
676 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
677 | - }, $files); |
|
678 | - } |
|
679 | - |
|
680 | - /** |
|
681 | - * Re-calculate the folder size and the size of all parent folders |
|
682 | - * |
|
683 | - * @param string|boolean $path |
|
684 | - * @param array $data (optional) meta data of the folder |
|
685 | - */ |
|
686 | - public function correctFolderSize($path, $data = null) { |
|
687 | - $this->calculateFolderSize($path, $data); |
|
688 | - if ($path !== '') { |
|
689 | - $parent = dirname($path); |
|
690 | - if ($parent === '.' or $parent === '/') { |
|
691 | - $parent = ''; |
|
692 | - } |
|
693 | - $this->correctFolderSize($parent); |
|
694 | - } |
|
695 | - } |
|
696 | - |
|
697 | - /** |
|
698 | - * calculate the size of a folder and set it in the cache |
|
699 | - * |
|
700 | - * @param string $path |
|
701 | - * @param array $entry (optional) meta data of the folder |
|
702 | - * @return int |
|
703 | - */ |
|
704 | - public function calculateFolderSize($path, $entry = null) { |
|
705 | - $totalSize = 0; |
|
706 | - if (is_null($entry) or !isset($entry['fileid'])) { |
|
707 | - $entry = $this->get($path); |
|
708 | - } |
|
709 | - if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
|
710 | - $id = $entry['fileid']; |
|
711 | - $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
712 | - 'FROM `*PREFIX*filecache` ' . |
|
713 | - 'WHERE `parent` = ? AND `storage` = ?'; |
|
714 | - $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
715 | - if ($row = $result->fetch()) { |
|
716 | - $result->closeCursor(); |
|
717 | - list($sum, $min) = array_values($row); |
|
718 | - $sum = 0 + $sum; |
|
719 | - $min = 0 + $min; |
|
720 | - if ($min === -1) { |
|
721 | - $totalSize = $min; |
|
722 | - } else { |
|
723 | - $totalSize = $sum; |
|
724 | - } |
|
725 | - $update = array(); |
|
726 | - if ($entry['size'] !== $totalSize) { |
|
727 | - $update['size'] = $totalSize; |
|
728 | - } |
|
729 | - if (count($update) > 0) { |
|
730 | - $this->update($id, $update); |
|
731 | - } |
|
732 | - } else { |
|
733 | - $result->closeCursor(); |
|
734 | - } |
|
735 | - } |
|
736 | - return $totalSize; |
|
737 | - } |
|
738 | - |
|
739 | - /** |
|
740 | - * get all file ids on the files on the storage |
|
741 | - * |
|
742 | - * @return int[] |
|
743 | - */ |
|
744 | - public function getAll() { |
|
745 | - $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
746 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
747 | - $ids = array(); |
|
748 | - while ($row = $result->fetch()) { |
|
749 | - $ids[] = $row['fileid']; |
|
750 | - } |
|
751 | - return $ids; |
|
752 | - } |
|
753 | - |
|
754 | - /** |
|
755 | - * find a folder in the cache which has not been fully scanned |
|
756 | - * |
|
757 | - * If multiple incomplete folders are in the cache, the one with the highest id will be returned, |
|
758 | - * use the one with the highest id gives the best result with the background scanner, since that is most |
|
759 | - * likely the folder where we stopped scanning previously |
|
760 | - * |
|
761 | - * @return string|bool the path of the folder or false when no folder matched |
|
762 | - */ |
|
763 | - public function getIncomplete() { |
|
764 | - $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`' |
|
765 | - . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1); |
|
766 | - $query->execute([$this->getNumericStorageId()]); |
|
767 | - if ($row = $query->fetch()) { |
|
768 | - return $row['path']; |
|
769 | - } else { |
|
770 | - return false; |
|
771 | - } |
|
772 | - } |
|
773 | - |
|
774 | - /** |
|
775 | - * get the path of a file on this storage by it's file id |
|
776 | - * |
|
777 | - * @param int $id the file id of the file or folder to search |
|
778 | - * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache |
|
779 | - */ |
|
780 | - public function getPathById($id) { |
|
781 | - $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?'; |
|
782 | - $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
783 | - if ($row = $result->fetch()) { |
|
784 | - // Oracle stores empty strings as null... |
|
785 | - if ($row['path'] === null) { |
|
786 | - return ''; |
|
787 | - } |
|
788 | - return $row['path']; |
|
789 | - } else { |
|
790 | - return null; |
|
791 | - } |
|
792 | - } |
|
793 | - |
|
794 | - /** |
|
795 | - * get the storage id of the storage for a file and the internal path of the file |
|
796 | - * unlike getPathById this does not limit the search to files on this storage and |
|
797 | - * instead does a global search in the cache table |
|
798 | - * |
|
799 | - * @param int $id |
|
800 | - * @deprecated use getPathById() instead |
|
801 | - * @return array first element holding the storage id, second the path |
|
802 | - */ |
|
803 | - static public function getById($id) { |
|
804 | - $connection = \OC::$server->getDatabaseConnection(); |
|
805 | - $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
806 | - $result = $connection->executeQuery($sql, array($id)); |
|
807 | - if ($row = $result->fetch()) { |
|
808 | - $numericId = $row['storage']; |
|
809 | - $path = $row['path']; |
|
810 | - } else { |
|
811 | - return null; |
|
812 | - } |
|
813 | - |
|
814 | - if ($id = Storage::getStorageId($numericId)) { |
|
815 | - return array($id, $path); |
|
816 | - } else { |
|
817 | - return null; |
|
818 | - } |
|
819 | - } |
|
820 | - |
|
821 | - /** |
|
822 | - * normalize the given path |
|
823 | - * |
|
824 | - * @param string $path |
|
825 | - * @return string |
|
826 | - */ |
|
827 | - public function normalize($path) { |
|
828 | - |
|
829 | - return trim(\OC_Util::normalizeUnicode($path), '/'); |
|
830 | - } |
|
624 | + $mimetype = $this->mimetypeLoader->getId($mimetype); |
|
625 | + $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
|
626 | + |
|
627 | + $files = $result->fetchAll(); |
|
628 | + |
|
629 | + return array_map(function (array $data) { |
|
630 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
631 | + }, $files); |
|
632 | + } |
|
633 | + |
|
634 | + /** |
|
635 | + * Search for files by tag of a given users. |
|
636 | + * |
|
637 | + * Note that every user can tag files differently. |
|
638 | + * |
|
639 | + * @param string|int $tag name or tag id |
|
640 | + * @param string $userId owner of the tags |
|
641 | + * @return ICacheEntry[] file data |
|
642 | + */ |
|
643 | + public function searchByTag($tag, $userId) { |
|
644 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
645 | + '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
646 | + '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
647 | + 'FROM `*PREFIX*filecache` `file`, ' . |
|
648 | + '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
649 | + '`*PREFIX*vcategory` `tag` ' . |
|
650 | + // JOIN filecache to vcategory_to_object |
|
651 | + 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
652 | + // JOIN vcategory_to_object to vcategory |
|
653 | + 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
654 | + 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
655 | + // conditions |
|
656 | + 'AND `file`.`storage` = ? ' . |
|
657 | + 'AND `tag`.`type` = \'files\' ' . |
|
658 | + 'AND `tag`.`uid` = ? '; |
|
659 | + if (is_int($tag)) { |
|
660 | + $sql .= 'AND `tag`.`id` = ? '; |
|
661 | + } else { |
|
662 | + $sql .= 'AND `tag`.`category` = ? '; |
|
663 | + } |
|
664 | + $result = $this->connection->executeQuery( |
|
665 | + $sql, |
|
666 | + [ |
|
667 | + $this->getNumericStorageId(), |
|
668 | + $userId, |
|
669 | + $tag |
|
670 | + ] |
|
671 | + ); |
|
672 | + |
|
673 | + $files = $result->fetchAll(); |
|
674 | + |
|
675 | + return array_map(function (array $data) { |
|
676 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
677 | + }, $files); |
|
678 | + } |
|
679 | + |
|
680 | + /** |
|
681 | + * Re-calculate the folder size and the size of all parent folders |
|
682 | + * |
|
683 | + * @param string|boolean $path |
|
684 | + * @param array $data (optional) meta data of the folder |
|
685 | + */ |
|
686 | + public function correctFolderSize($path, $data = null) { |
|
687 | + $this->calculateFolderSize($path, $data); |
|
688 | + if ($path !== '') { |
|
689 | + $parent = dirname($path); |
|
690 | + if ($parent === '.' or $parent === '/') { |
|
691 | + $parent = ''; |
|
692 | + } |
|
693 | + $this->correctFolderSize($parent); |
|
694 | + } |
|
695 | + } |
|
696 | + |
|
697 | + /** |
|
698 | + * calculate the size of a folder and set it in the cache |
|
699 | + * |
|
700 | + * @param string $path |
|
701 | + * @param array $entry (optional) meta data of the folder |
|
702 | + * @return int |
|
703 | + */ |
|
704 | + public function calculateFolderSize($path, $entry = null) { |
|
705 | + $totalSize = 0; |
|
706 | + if (is_null($entry) or !isset($entry['fileid'])) { |
|
707 | + $entry = $this->get($path); |
|
708 | + } |
|
709 | + if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
|
710 | + $id = $entry['fileid']; |
|
711 | + $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
712 | + 'FROM `*PREFIX*filecache` ' . |
|
713 | + 'WHERE `parent` = ? AND `storage` = ?'; |
|
714 | + $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
715 | + if ($row = $result->fetch()) { |
|
716 | + $result->closeCursor(); |
|
717 | + list($sum, $min) = array_values($row); |
|
718 | + $sum = 0 + $sum; |
|
719 | + $min = 0 + $min; |
|
720 | + if ($min === -1) { |
|
721 | + $totalSize = $min; |
|
722 | + } else { |
|
723 | + $totalSize = $sum; |
|
724 | + } |
|
725 | + $update = array(); |
|
726 | + if ($entry['size'] !== $totalSize) { |
|
727 | + $update['size'] = $totalSize; |
|
728 | + } |
|
729 | + if (count($update) > 0) { |
|
730 | + $this->update($id, $update); |
|
731 | + } |
|
732 | + } else { |
|
733 | + $result->closeCursor(); |
|
734 | + } |
|
735 | + } |
|
736 | + return $totalSize; |
|
737 | + } |
|
738 | + |
|
739 | + /** |
|
740 | + * get all file ids on the files on the storage |
|
741 | + * |
|
742 | + * @return int[] |
|
743 | + */ |
|
744 | + public function getAll() { |
|
745 | + $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
746 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
747 | + $ids = array(); |
|
748 | + while ($row = $result->fetch()) { |
|
749 | + $ids[] = $row['fileid']; |
|
750 | + } |
|
751 | + return $ids; |
|
752 | + } |
|
753 | + |
|
754 | + /** |
|
755 | + * find a folder in the cache which has not been fully scanned |
|
756 | + * |
|
757 | + * If multiple incomplete folders are in the cache, the one with the highest id will be returned, |
|
758 | + * use the one with the highest id gives the best result with the background scanner, since that is most |
|
759 | + * likely the folder where we stopped scanning previously |
|
760 | + * |
|
761 | + * @return string|bool the path of the folder or false when no folder matched |
|
762 | + */ |
|
763 | + public function getIncomplete() { |
|
764 | + $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`' |
|
765 | + . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1); |
|
766 | + $query->execute([$this->getNumericStorageId()]); |
|
767 | + if ($row = $query->fetch()) { |
|
768 | + return $row['path']; |
|
769 | + } else { |
|
770 | + return false; |
|
771 | + } |
|
772 | + } |
|
773 | + |
|
774 | + /** |
|
775 | + * get the path of a file on this storage by it's file id |
|
776 | + * |
|
777 | + * @param int $id the file id of the file or folder to search |
|
778 | + * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache |
|
779 | + */ |
|
780 | + public function getPathById($id) { |
|
781 | + $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?'; |
|
782 | + $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
783 | + if ($row = $result->fetch()) { |
|
784 | + // Oracle stores empty strings as null... |
|
785 | + if ($row['path'] === null) { |
|
786 | + return ''; |
|
787 | + } |
|
788 | + return $row['path']; |
|
789 | + } else { |
|
790 | + return null; |
|
791 | + } |
|
792 | + } |
|
793 | + |
|
794 | + /** |
|
795 | + * get the storage id of the storage for a file and the internal path of the file |
|
796 | + * unlike getPathById this does not limit the search to files on this storage and |
|
797 | + * instead does a global search in the cache table |
|
798 | + * |
|
799 | + * @param int $id |
|
800 | + * @deprecated use getPathById() instead |
|
801 | + * @return array first element holding the storage id, second the path |
|
802 | + */ |
|
803 | + static public function getById($id) { |
|
804 | + $connection = \OC::$server->getDatabaseConnection(); |
|
805 | + $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
806 | + $result = $connection->executeQuery($sql, array($id)); |
|
807 | + if ($row = $result->fetch()) { |
|
808 | + $numericId = $row['storage']; |
|
809 | + $path = $row['path']; |
|
810 | + } else { |
|
811 | + return null; |
|
812 | + } |
|
813 | + |
|
814 | + if ($id = Storage::getStorageId($numericId)) { |
|
815 | + return array($id, $path); |
|
816 | + } else { |
|
817 | + return null; |
|
818 | + } |
|
819 | + } |
|
820 | + |
|
821 | + /** |
|
822 | + * normalize the given path |
|
823 | + * |
|
824 | + * @param string $path |
|
825 | + * @return string |
|
826 | + */ |
|
827 | + public function normalize($path) { |
|
828 | + |
|
829 | + return trim(\OC_Util::normalizeUnicode($path), '/'); |
|
830 | + } |
|
831 | 831 | } |
@@ -155,21 +155,21 @@ discard block |
||
155 | 155 | */ |
156 | 156 | public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
157 | 157 | //fix types |
158 | - $data['fileid'] = (int)$data['fileid']; |
|
159 | - $data['parent'] = (int)$data['parent']; |
|
158 | + $data['fileid'] = (int) $data['fileid']; |
|
159 | + $data['parent'] = (int) $data['parent']; |
|
160 | 160 | $data['size'] = 0 + $data['size']; |
161 | - $data['mtime'] = (int)$data['mtime']; |
|
162 | - $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
163 | - $data['encryptedVersion'] = (int)$data['encrypted']; |
|
164 | - $data['encrypted'] = (bool)$data['encrypted']; |
|
161 | + $data['mtime'] = (int) $data['mtime']; |
|
162 | + $data['storage_mtime'] = (int) $data['storage_mtime']; |
|
163 | + $data['encryptedVersion'] = (int) $data['encrypted']; |
|
164 | + $data['encrypted'] = (bool) $data['encrypted']; |
|
165 | 165 | $data['storage_id'] = $data['storage']; |
166 | - $data['storage'] = (int)$data['storage']; |
|
166 | + $data['storage'] = (int) $data['storage']; |
|
167 | 167 | $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
168 | 168 | $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
169 | 169 | if ($data['storage_mtime'] == 0) { |
170 | 170 | $data['storage_mtime'] = $data['mtime']; |
171 | 171 | } |
172 | - $data['permissions'] = (int)$data['permissions']; |
|
172 | + $data['permissions'] = (int) $data['permissions']; |
|
173 | 173 | return new CacheEntry($data); |
174 | 174 | } |
175 | 175 | |
@@ -197,8 +197,8 @@ discard block |
||
197 | 197 | FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC'; |
198 | 198 | $result = $this->connection->executeQuery($sql, [$fileId]); |
199 | 199 | $files = $result->fetchAll(); |
200 | - return array_map(function (array $data) { |
|
201 | - return self::cacheEntryFromData($data, $this->mimetypeLoader);; |
|
200 | + return array_map(function(array $data) { |
|
201 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); ; |
|
202 | 202 | }, $files); |
203 | 203 | } else { |
204 | 204 | return array(); |
@@ -257,7 +257,7 @@ discard block |
||
257 | 257 | $queryParts[] = '`storage`'; |
258 | 258 | $params[] = $this->getNumericStorageId(); |
259 | 259 | |
260 | - $queryParts = array_map(function ($item) { |
|
260 | + $queryParts = array_map(function($item) { |
|
261 | 261 | return trim($item, "`"); |
262 | 262 | }, $queryParts); |
263 | 263 | $values = array_combine($queryParts, $params); |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | 'path_hash', |
267 | 267 | ]) |
268 | 268 | ) { |
269 | - return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
269 | + return (int) $this->connection->lastInsertId('*PREFIX*filecache'); |
|
270 | 270 | } |
271 | 271 | |
272 | 272 | // The file was created in the mean time |
@@ -304,10 +304,10 @@ discard block |
||
304 | 304 | |
305 | 305 | // don't update if the data we try to set is the same as the one in the record |
306 | 306 | // some databases (Postgres) don't like superfluous updates |
307 | - $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
308 | - 'WHERE (' . |
|
309 | - implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
310 | - implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
307 | + $sql = 'UPDATE `*PREFIX*filecache` SET '.implode(' = ?, ', $queryParts).'=? '. |
|
308 | + 'WHERE ('. |
|
309 | + implode(' <> ? OR ', $queryParts).' <> ? OR '. |
|
310 | + implode(' IS NULL OR ', $queryParts).' IS NULL'. |
|
311 | 311 | ') AND `fileid` = ? '; |
312 | 312 | $this->connection->executeQuery($sql, $params); |
313 | 313 | |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | $queryParts[] = '`mtime`'; |
351 | 351 | } |
352 | 352 | } elseif ($name === 'encrypted') { |
353 | - if(isset($data['encryptedVersion'])) { |
|
353 | + if (isset($data['encryptedVersion'])) { |
|
354 | 354 | $value = $data['encryptedVersion']; |
355 | 355 | } else { |
356 | 356 | // Boolean to integer conversion |
@@ -358,7 +358,7 @@ discard block |
||
358 | 358 | } |
359 | 359 | } |
360 | 360 | $params[] = $value; |
361 | - $queryParts[] = '`' . $name . '`'; |
|
361 | + $queryParts[] = '`'.$name.'`'; |
|
362 | 362 | } |
363 | 363 | } |
364 | 364 | return array($queryParts, $params); |
@@ -400,7 +400,7 @@ discard block |
||
400 | 400 | return -1; |
401 | 401 | } else { |
402 | 402 | $parent = $this->getParentPath($file); |
403 | - return (int)$this->getId($parent); |
|
403 | + return (int) $this->getId($parent); |
|
404 | 404 | } |
405 | 405 | } |
406 | 406 | |
@@ -446,7 +446,7 @@ discard block |
||
446 | 446 | */ |
447 | 447 | private function getSubFolders($entry) { |
448 | 448 | $children = $this->getFolderContentsById($entry['fileid']); |
449 | - return array_filter($children, function ($child) { |
|
449 | + return array_filter($children, function($child) { |
|
450 | 450 | return $child['mimetype'] === 'httpd/unix-directory'; |
451 | 451 | }); |
452 | 452 | } |
@@ -513,14 +513,14 @@ discard block |
||
513 | 513 | if ($sourceData['mimetype'] === 'httpd/unix-directory') { |
514 | 514 | //find all child entries |
515 | 515 | $sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?'; |
516 | - $result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']); |
|
516 | + $result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath).'/%']); |
|
517 | 517 | $childEntries = $result->fetchAll(); |
518 | 518 | $sourceLength = strlen($sourcePath); |
519 | 519 | $this->connection->beginTransaction(); |
520 | 520 | $query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?'); |
521 | 521 | |
522 | 522 | foreach ($childEntries as $child) { |
523 | - $newTargetPath = $targetPath . substr($child['path'], $sourceLength); |
|
523 | + $newTargetPath = $targetPath.substr($child['path'], $sourceLength); |
|
524 | 524 | $query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]); |
525 | 525 | } |
526 | 526 | $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); |
@@ -564,7 +564,7 @@ discard block |
||
564 | 564 | $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
565 | 565 | $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
566 | 566 | if ($row = $result->fetch()) { |
567 | - if ((int)$row['size'] === -1) { |
|
567 | + if ((int) $row['size'] === -1) { |
|
568 | 568 | return self::SHALLOW; |
569 | 569 | } else { |
570 | 570 | return self::COMPLETE; |
@@ -620,13 +620,13 @@ discard block |
||
620 | 620 | $where = '`mimepart` = ?'; |
621 | 621 | } |
622 | 622 | $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
623 | - FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'; |
|
623 | + FROM `*PREFIX*filecache` WHERE ' . $where.' AND `storage` = ?'; |
|
624 | 624 | $mimetype = $this->mimetypeLoader->getId($mimetype); |
625 | 625 | $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
626 | 626 | |
627 | 627 | $files = $result->fetchAll(); |
628 | 628 | |
629 | - return array_map(function (array $data) { |
|
629 | + return array_map(function(array $data) { |
|
630 | 630 | return self::cacheEntryFromData($data, $this->mimetypeLoader); |
631 | 631 | }, $files); |
632 | 632 | } |
@@ -641,20 +641,20 @@ discard block |
||
641 | 641 | * @return ICacheEntry[] file data |
642 | 642 | */ |
643 | 643 | public function searchByTag($tag, $userId) { |
644 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
645 | - '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
646 | - '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
647 | - 'FROM `*PREFIX*filecache` `file`, ' . |
|
648 | - '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
649 | - '`*PREFIX*vcategory` `tag` ' . |
|
644 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, '. |
|
645 | + '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, '. |
|
646 | + '`encrypted`, `etag`, `permissions`, `checksum` '. |
|
647 | + 'FROM `*PREFIX*filecache` `file`, '. |
|
648 | + '`*PREFIX*vcategory_to_object` `tagmap`, '. |
|
649 | + '`*PREFIX*vcategory` `tag` '. |
|
650 | 650 | // JOIN filecache to vcategory_to_object |
651 | - 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
651 | + 'WHERE `file`.`fileid` = `tagmap`.`objid` '. |
|
652 | 652 | // JOIN vcategory_to_object to vcategory |
653 | - 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
654 | - 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
653 | + 'AND `tagmap`.`type` = `tag`.`type` '. |
|
654 | + 'AND `tagmap`.`categoryid` = `tag`.`id` '. |
|
655 | 655 | // conditions |
656 | - 'AND `file`.`storage` = ? ' . |
|
657 | - 'AND `tag`.`type` = \'files\' ' . |
|
656 | + 'AND `file`.`storage` = ? '. |
|
657 | + 'AND `tag`.`type` = \'files\' '. |
|
658 | 658 | 'AND `tag`.`uid` = ? '; |
659 | 659 | if (is_int($tag)) { |
660 | 660 | $sql .= 'AND `tag`.`id` = ? '; |
@@ -672,7 +672,7 @@ discard block |
||
672 | 672 | |
673 | 673 | $files = $result->fetchAll(); |
674 | 674 | |
675 | - return array_map(function (array $data) { |
|
675 | + return array_map(function(array $data) { |
|
676 | 676 | return self::cacheEntryFromData($data, $this->mimetypeLoader); |
677 | 677 | }, $files); |
678 | 678 | } |
@@ -708,8 +708,8 @@ discard block |
||
708 | 708 | } |
709 | 709 | if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
710 | 710 | $id = $entry['fileid']; |
711 | - $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
712 | - 'FROM `*PREFIX*filecache` ' . |
|
711 | + $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 '. |
|
712 | + 'FROM `*PREFIX*filecache` '. |
|
713 | 713 | 'WHERE `parent` = ? AND `storage` = ?'; |
714 | 714 | $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
715 | 715 | if ($row = $result->fetch()) { |
@@ -386,6 +386,14 @@ discard block |
||
386 | 386 | return $size; |
387 | 387 | } |
388 | 388 | |
389 | + /** |
|
390 | + * @param string $path |
|
391 | + * @param boolean $recursive |
|
392 | + * @param integer $reuse |
|
393 | + * @param integer|null $folderId |
|
394 | + * @param boolean $lock |
|
395 | + * @param integer $size |
|
396 | + */ |
|
389 | 397 | private function handleChildren($path, $recursive, $reuse, $folderId, $lock, &$size) { |
390 | 398 | // we put this in it's own function so it cleans up the memory before we start recursing |
391 | 399 | $existingChildren = $this->getExistingChildren($folderId); |
@@ -485,6 +493,9 @@ discard block |
||
485 | 493 | } |
486 | 494 | } |
487 | 495 | |
496 | + /** |
|
497 | + * @param string|boolean $path |
|
498 | + */ |
|
488 | 499 | private function runBackgroundScanJob(callable $callback, $path) { |
489 | 500 | try { |
490 | 501 | $callback(); |
@@ -55,476 +55,476 @@ |
||
55 | 55 | * @package OC\Files\Cache |
56 | 56 | */ |
57 | 57 | class Scanner extends BasicEmitter implements IScanner { |
58 | - /** |
|
59 | - * @var \OC\Files\Storage\Storage $storage |
|
60 | - */ |
|
61 | - protected $storage; |
|
62 | - |
|
63 | - /** |
|
64 | - * @var string $storageId |
|
65 | - */ |
|
66 | - protected $storageId; |
|
67 | - |
|
68 | - /** |
|
69 | - * @var \OC\Files\Cache\Cache $cache |
|
70 | - */ |
|
71 | - protected $cache; |
|
72 | - |
|
73 | - /** |
|
74 | - * @var boolean $cacheActive If true, perform cache operations, if false, do not affect cache |
|
75 | - */ |
|
76 | - protected $cacheActive; |
|
77 | - |
|
78 | - /** |
|
79 | - * @var bool $useTransactions whether to use transactions |
|
80 | - */ |
|
81 | - protected $useTransactions = true; |
|
82 | - |
|
83 | - /** |
|
84 | - * @var \OCP\Lock\ILockingProvider |
|
85 | - */ |
|
86 | - protected $lockingProvider; |
|
87 | - |
|
88 | - public function __construct(\OC\Files\Storage\Storage $storage) { |
|
89 | - $this->storage = $storage; |
|
90 | - $this->storageId = $this->storage->getId(); |
|
91 | - $this->cache = $storage->getCache(); |
|
92 | - $this->cacheActive = !Config::getSystemValue('filesystem_cache_readonly', false); |
|
93 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Whether to wrap the scanning of a folder in a database transaction |
|
98 | - * On default transactions are used |
|
99 | - * |
|
100 | - * @param bool $useTransactions |
|
101 | - */ |
|
102 | - public function setUseTransactions($useTransactions) { |
|
103 | - $this->useTransactions = $useTransactions; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * get all the metadata of a file or folder |
|
108 | - * * |
|
109 | - * |
|
110 | - * @param string $path |
|
111 | - * @return array an array of metadata of the file |
|
112 | - */ |
|
113 | - protected function getData($path) { |
|
114 | - $data = $this->storage->getMetaData($path); |
|
115 | - if (is_null($data)) { |
|
116 | - \OCP\Util::writeLog('OC\Files\Cache\Scanner', "!!! Path '$path' is not accessible or present !!!", \OCP\Util::DEBUG); |
|
117 | - } |
|
118 | - return $data; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * scan a single file and store it in the cache |
|
123 | - * |
|
124 | - * @param string $file |
|
125 | - * @param int $reuseExisting |
|
126 | - * @param int $parentId |
|
127 | - * @param array | null $cacheData existing data in the cache for the file to be scanned |
|
128 | - * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
129 | - * @return array an array of metadata of the scanned file |
|
130 | - * @throws \OC\ServerNotAvailableException |
|
131 | - * @throws \OCP\Lock\LockedException |
|
132 | - */ |
|
133 | - public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) { |
|
134 | - if ($file !== '') { |
|
135 | - try { |
|
136 | - $this->storage->verifyPath(dirname($file), basename($file)); |
|
137 | - } catch (\Exception $e) { |
|
138 | - return null; |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - // only proceed if $file is not a partial file nor a blacklisted file |
|
143 | - if (!self::isPartialFile($file) and !Filesystem::isFileBlacklisted($file)) { |
|
144 | - |
|
145 | - //acquire a lock |
|
146 | - if ($lock) { |
|
147 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
148 | - $this->storage->acquireLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - try { |
|
153 | - $data = $this->getData($file); |
|
154 | - } catch (ForbiddenException $e) { |
|
155 | - return null; |
|
156 | - } |
|
157 | - |
|
158 | - if ($data) { |
|
159 | - |
|
160 | - // pre-emit only if it was a file. By that we avoid counting/treating folders as files |
|
161 | - if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
162 | - $this->emit('\OC\Files\Cache\Scanner', 'scanFile', array($file, $this->storageId)); |
|
163 | - \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
164 | - } |
|
165 | - |
|
166 | - $parent = dirname($file); |
|
167 | - if ($parent === '.' or $parent === '/') { |
|
168 | - $parent = ''; |
|
169 | - } |
|
170 | - if ($parentId === -1) { |
|
171 | - $parentId = $this->cache->getParentId($file); |
|
172 | - } |
|
173 | - |
|
174 | - // scan the parent if it's not in the cache (id -1) and the current file is not the root folder |
|
175 | - if ($file and $parentId === -1) { |
|
176 | - $parentData = $this->scanFile($parent); |
|
177 | - if (!$parentData) { |
|
178 | - return null; |
|
179 | - } |
|
180 | - $parentId = $parentData['fileid']; |
|
181 | - } |
|
182 | - if ($parent) { |
|
183 | - $data['parent'] = $parentId; |
|
184 | - } |
|
185 | - if (is_null($cacheData)) { |
|
186 | - /** @var CacheEntry $cacheData */ |
|
187 | - $cacheData = $this->cache->get($file); |
|
188 | - } |
|
189 | - if ($cacheData and $reuseExisting and isset($cacheData['fileid'])) { |
|
190 | - // prevent empty etag |
|
191 | - if (empty($cacheData['etag'])) { |
|
192 | - $etag = $data['etag']; |
|
193 | - } else { |
|
194 | - $etag = $cacheData['etag']; |
|
195 | - } |
|
196 | - $fileId = $cacheData['fileid']; |
|
197 | - $data['fileid'] = $fileId; |
|
198 | - // only reuse data if the file hasn't explicitly changed |
|
199 | - if (isset($data['storage_mtime']) && isset($cacheData['storage_mtime']) && $data['storage_mtime'] === $cacheData['storage_mtime']) { |
|
200 | - $data['mtime'] = $cacheData['mtime']; |
|
201 | - if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) { |
|
202 | - $data['size'] = $cacheData['size']; |
|
203 | - } |
|
204 | - if ($reuseExisting & self::REUSE_ETAG) { |
|
205 | - $data['etag'] = $etag; |
|
206 | - } |
|
207 | - } |
|
208 | - // Only update metadata that has changed |
|
209 | - $newData = array_diff_assoc($data, $cacheData->getData()); |
|
210 | - } else { |
|
211 | - $newData = $data; |
|
212 | - $fileId = -1; |
|
213 | - } |
|
214 | - if (!empty($newData)) { |
|
215 | - // Reset the checksum if the data has changed |
|
216 | - $newData['checksum'] = ''; |
|
217 | - $data['fileid'] = $this->addToCache($file, $newData, $fileId); |
|
218 | - } |
|
219 | - if (isset($cacheData['size'])) { |
|
220 | - $data['oldSize'] = $cacheData['size']; |
|
221 | - } else { |
|
222 | - $data['oldSize'] = 0; |
|
223 | - } |
|
224 | - |
|
225 | - if (isset($cacheData['encrypted'])) { |
|
226 | - $data['encrypted'] = $cacheData['encrypted']; |
|
227 | - } |
|
228 | - |
|
229 | - // post-emit only if it was a file. By that we avoid counting/treating folders as files |
|
230 | - if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
231 | - $this->emit('\OC\Files\Cache\Scanner', 'postScanFile', array($file, $this->storageId)); |
|
232 | - \OC_Hook::emit('\OC\Files\Cache\Scanner', 'post_scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
233 | - } |
|
234 | - |
|
235 | - } else { |
|
236 | - $this->removeFromCache($file); |
|
237 | - } |
|
238 | - |
|
239 | - //release the acquired lock |
|
240 | - if ($lock) { |
|
241 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
242 | - $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - if ($data && !isset($data['encrypted'])) { |
|
247 | - $data['encrypted'] = false; |
|
248 | - } |
|
249 | - return $data; |
|
250 | - } |
|
251 | - |
|
252 | - return null; |
|
253 | - } |
|
254 | - |
|
255 | - protected function removeFromCache($path) { |
|
256 | - \OC_Hook::emit('Scanner', 'removeFromCache', array('file' => $path)); |
|
257 | - $this->emit('\OC\Files\Cache\Scanner', 'removeFromCache', array($path)); |
|
258 | - if ($this->cacheActive) { |
|
259 | - $this->cache->remove($path); |
|
260 | - } |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * @param string $path |
|
265 | - * @param array $data |
|
266 | - * @param int $fileId |
|
267 | - * @return int the id of the added file |
|
268 | - */ |
|
269 | - protected function addToCache($path, $data, $fileId = -1) { |
|
270 | - \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
271 | - $this->emit('\OC\Files\Cache\Scanner', 'addToCache', array($path, $this->storageId, $data)); |
|
272 | - if ($this->cacheActive) { |
|
273 | - if ($fileId !== -1) { |
|
274 | - $this->cache->update($fileId, $data); |
|
275 | - return $fileId; |
|
276 | - } else { |
|
277 | - return $this->cache->put($path, $data); |
|
278 | - } |
|
279 | - } else { |
|
280 | - return -1; |
|
281 | - } |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * @param string $path |
|
286 | - * @param array $data |
|
287 | - * @param int $fileId |
|
288 | - */ |
|
289 | - protected function updateCache($path, $data, $fileId = -1) { |
|
290 | - \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
291 | - $this->emit('\OC\Files\Cache\Scanner', 'updateCache', array($path, $this->storageId, $data)); |
|
292 | - if ($this->cacheActive) { |
|
293 | - if ($fileId !== -1) { |
|
294 | - $this->cache->update($fileId, $data); |
|
295 | - } else { |
|
296 | - $this->cache->put($path, $data); |
|
297 | - } |
|
298 | - } |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * scan a folder and all it's children |
|
303 | - * |
|
304 | - * @param string $path |
|
305 | - * @param bool $recursive |
|
306 | - * @param int $reuse |
|
307 | - * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
308 | - * @return array an array of the meta data of the scanned file or folder |
|
309 | - */ |
|
310 | - public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) { |
|
311 | - if ($reuse === -1) { |
|
312 | - $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
313 | - } |
|
314 | - if ($lock) { |
|
315 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
316 | - $this->storage->acquireLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
317 | - $this->storage->acquireLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
318 | - } |
|
319 | - } |
|
320 | - $data = $this->scanFile($path, $reuse, -1, null, $lock); |
|
321 | - if ($data and $data['mimetype'] === 'httpd/unix-directory') { |
|
322 | - $size = $this->scanChildren($path, $recursive, $reuse, $data['fileid'], $lock); |
|
323 | - $data['size'] = $size; |
|
324 | - } |
|
325 | - if ($lock) { |
|
326 | - if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
327 | - $this->storage->releaseLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
328 | - $this->storage->releaseLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
329 | - } |
|
330 | - } |
|
331 | - return $data; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Get the children currently in the cache |
|
336 | - * |
|
337 | - * @param int $folderId |
|
338 | - * @return array[] |
|
339 | - */ |
|
340 | - protected function getExistingChildren($folderId) { |
|
341 | - $existingChildren = array(); |
|
342 | - $children = $this->cache->getFolderContentsById($folderId); |
|
343 | - foreach ($children as $child) { |
|
344 | - $existingChildren[$child['name']] = $child; |
|
345 | - } |
|
346 | - return $existingChildren; |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Get the children from the storage |
|
351 | - * |
|
352 | - * @param string $folder |
|
353 | - * @return string[] |
|
354 | - */ |
|
355 | - protected function getNewChildren($folder) { |
|
356 | - $children = array(); |
|
357 | - if ($dh = $this->storage->opendir($folder)) { |
|
358 | - if (is_resource($dh)) { |
|
359 | - while (($file = readdir($dh)) !== false) { |
|
360 | - if (!Filesystem::isIgnoredDir($file)) { |
|
361 | - $children[] = trim(\OC\Files\Filesystem::normalizePath($file), '/'); |
|
362 | - } |
|
363 | - } |
|
364 | - } |
|
365 | - } |
|
366 | - return $children; |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * scan all the files and folders in a folder |
|
371 | - * |
|
372 | - * @param string $path |
|
373 | - * @param bool $recursive |
|
374 | - * @param int $reuse |
|
375 | - * @param int $folderId id for the folder to be scanned |
|
376 | - * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
377 | - * @return int the size of the scanned folder or -1 if the size is unknown at this stage |
|
378 | - */ |
|
379 | - protected function scanChildren($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $folderId = null, $lock = true) { |
|
380 | - if ($reuse === -1) { |
|
381 | - $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
382 | - } |
|
383 | - $this->emit('\OC\Files\Cache\Scanner', 'scanFolder', array($path, $this->storageId)); |
|
384 | - $size = 0; |
|
385 | - if (!is_null($folderId)) { |
|
386 | - $folderId = $this->cache->getId($path); |
|
387 | - } |
|
388 | - $childQueue = $this->handleChildren($path, $recursive, $reuse, $folderId, $lock, $size); |
|
389 | - |
|
390 | - foreach ($childQueue as $child => $childId) { |
|
391 | - $childSize = $this->scanChildren($child, $recursive, $reuse, $childId, $lock); |
|
392 | - if ($childSize === -1) { |
|
393 | - $size = -1; |
|
394 | - } else if ($size !== -1) { |
|
395 | - $size += $childSize; |
|
396 | - } |
|
397 | - } |
|
398 | - if ($this->cacheActive) { |
|
399 | - $this->cache->update($folderId, array('size' => $size)); |
|
400 | - } |
|
401 | - $this->emit('\OC\Files\Cache\Scanner', 'postScanFolder', array($path, $this->storageId)); |
|
402 | - return $size; |
|
403 | - } |
|
404 | - |
|
405 | - private function handleChildren($path, $recursive, $reuse, $folderId, $lock, &$size) { |
|
406 | - // we put this in it's own function so it cleans up the memory before we start recursing |
|
407 | - $existingChildren = $this->getExistingChildren($folderId); |
|
408 | - $newChildren = $this->getNewChildren($path); |
|
409 | - |
|
410 | - if ($this->useTransactions) { |
|
411 | - \OC::$server->getDatabaseConnection()->beginTransaction(); |
|
412 | - } |
|
413 | - |
|
414 | - $exceptionOccurred = false; |
|
415 | - $childQueue = []; |
|
416 | - foreach ($newChildren as $file) { |
|
417 | - $child = ($path) ? $path . '/' . $file : $file; |
|
418 | - try { |
|
419 | - $existingData = isset($existingChildren[$file]) ? $existingChildren[$file] : null; |
|
420 | - $data = $this->scanFile($child, $reuse, $folderId, $existingData, $lock); |
|
421 | - if ($data) { |
|
422 | - if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE) { |
|
423 | - $childQueue[$child] = $data['fileid']; |
|
424 | - } else if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE_INCOMPLETE and $data['size'] === -1) { |
|
425 | - // only recurse into folders which aren't fully scanned |
|
426 | - $childQueue[$child] = $data['fileid']; |
|
427 | - } else if ($data['size'] === -1) { |
|
428 | - $size = -1; |
|
429 | - } else if ($size !== -1) { |
|
430 | - $size += $data['size']; |
|
431 | - } |
|
432 | - } |
|
433 | - } catch (\Doctrine\DBAL\DBALException $ex) { |
|
434 | - // might happen if inserting duplicate while a scanning |
|
435 | - // process is running in parallel |
|
436 | - // log and ignore |
|
437 | - \OCP\Util::writeLog('core', 'Exception while scanning file "' . $child . '": ' . $ex->getMessage(), \OCP\Util::DEBUG); |
|
438 | - $exceptionOccurred = true; |
|
439 | - } catch (\OCP\Lock\LockedException $e) { |
|
440 | - if ($this->useTransactions) { |
|
441 | - \OC::$server->getDatabaseConnection()->rollback(); |
|
442 | - } |
|
443 | - throw $e; |
|
444 | - } |
|
445 | - } |
|
446 | - $removedChildren = \array_diff(array_keys($existingChildren), $newChildren); |
|
447 | - foreach ($removedChildren as $childName) { |
|
448 | - $child = ($path) ? $path . '/' . $childName : $childName; |
|
449 | - $this->removeFromCache($child); |
|
450 | - } |
|
451 | - if ($this->useTransactions) { |
|
452 | - \OC::$server->getDatabaseConnection()->commit(); |
|
453 | - } |
|
454 | - if ($exceptionOccurred) { |
|
455 | - // It might happen that the parallel scan process has already |
|
456 | - // inserted mimetypes but those weren't available yet inside the transaction |
|
457 | - // To make sure to have the updated mime types in such cases, |
|
458 | - // we reload them here |
|
459 | - \OC::$server->getMimeTypeLoader()->reset(); |
|
460 | - } |
|
461 | - return $childQueue; |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * check if the file should be ignored when scanning |
|
466 | - * NOTE: files with a '.part' extension are ignored as well! |
|
467 | - * prevents unfinished put requests to be scanned |
|
468 | - * |
|
469 | - * @param string $file |
|
470 | - * @return boolean |
|
471 | - */ |
|
472 | - public static function isPartialFile($file) { |
|
473 | - if (pathinfo($file, PATHINFO_EXTENSION) === 'part') { |
|
474 | - return true; |
|
475 | - } |
|
476 | - if (strpos($file, '.part/') !== false) { |
|
477 | - return true; |
|
478 | - } |
|
479 | - |
|
480 | - return false; |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * walk over any folders that are not fully scanned yet and scan them |
|
485 | - */ |
|
486 | - public function backgroundScan() { |
|
487 | - if (!$this->cache->inCache('')) { |
|
488 | - $this->runBackgroundScanJob(function () { |
|
489 | - $this->scan('', self::SCAN_RECURSIVE, self::REUSE_ETAG); |
|
490 | - }, ''); |
|
491 | - } else { |
|
492 | - $lastPath = null; |
|
493 | - while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) { |
|
494 | - $this->runBackgroundScanJob(function () use ($path) { |
|
495 | - $this->scan($path, self::SCAN_RECURSIVE_INCOMPLETE, self::REUSE_ETAG | self::REUSE_SIZE); |
|
496 | - }, $path); |
|
497 | - // FIXME: this won't proceed with the next item, needs revamping of getIncomplete() |
|
498 | - // to make this possible |
|
499 | - $lastPath = $path; |
|
500 | - } |
|
501 | - } |
|
502 | - } |
|
503 | - |
|
504 | - private function runBackgroundScanJob(callable $callback, $path) { |
|
505 | - try { |
|
506 | - $callback(); |
|
507 | - \OC_Hook::emit('Scanner', 'correctFolderSize', array('path' => $path)); |
|
508 | - if ($this->cacheActive && $this->cache instanceof Cache) { |
|
509 | - $this->cache->correctFolderSize($path); |
|
510 | - } |
|
511 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
512 | - // skip unavailable storages |
|
513 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
514 | - // skip unavailable storages |
|
515 | - } catch (\OCP\Files\ForbiddenException $e) { |
|
516 | - // skip forbidden storages |
|
517 | - } catch (\OCP\Lock\LockedException $e) { |
|
518 | - // skip unavailable storages |
|
519 | - } |
|
520 | - } |
|
521 | - |
|
522 | - /** |
|
523 | - * Set whether the cache is affected by scan operations |
|
524 | - * |
|
525 | - * @param boolean $active The active state of the cache |
|
526 | - */ |
|
527 | - public function setCacheActive($active) { |
|
528 | - $this->cacheActive = $active; |
|
529 | - } |
|
58 | + /** |
|
59 | + * @var \OC\Files\Storage\Storage $storage |
|
60 | + */ |
|
61 | + protected $storage; |
|
62 | + |
|
63 | + /** |
|
64 | + * @var string $storageId |
|
65 | + */ |
|
66 | + protected $storageId; |
|
67 | + |
|
68 | + /** |
|
69 | + * @var \OC\Files\Cache\Cache $cache |
|
70 | + */ |
|
71 | + protected $cache; |
|
72 | + |
|
73 | + /** |
|
74 | + * @var boolean $cacheActive If true, perform cache operations, if false, do not affect cache |
|
75 | + */ |
|
76 | + protected $cacheActive; |
|
77 | + |
|
78 | + /** |
|
79 | + * @var bool $useTransactions whether to use transactions |
|
80 | + */ |
|
81 | + protected $useTransactions = true; |
|
82 | + |
|
83 | + /** |
|
84 | + * @var \OCP\Lock\ILockingProvider |
|
85 | + */ |
|
86 | + protected $lockingProvider; |
|
87 | + |
|
88 | + public function __construct(\OC\Files\Storage\Storage $storage) { |
|
89 | + $this->storage = $storage; |
|
90 | + $this->storageId = $this->storage->getId(); |
|
91 | + $this->cache = $storage->getCache(); |
|
92 | + $this->cacheActive = !Config::getSystemValue('filesystem_cache_readonly', false); |
|
93 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Whether to wrap the scanning of a folder in a database transaction |
|
98 | + * On default transactions are used |
|
99 | + * |
|
100 | + * @param bool $useTransactions |
|
101 | + */ |
|
102 | + public function setUseTransactions($useTransactions) { |
|
103 | + $this->useTransactions = $useTransactions; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * get all the metadata of a file or folder |
|
108 | + * * |
|
109 | + * |
|
110 | + * @param string $path |
|
111 | + * @return array an array of metadata of the file |
|
112 | + */ |
|
113 | + protected function getData($path) { |
|
114 | + $data = $this->storage->getMetaData($path); |
|
115 | + if (is_null($data)) { |
|
116 | + \OCP\Util::writeLog('OC\Files\Cache\Scanner', "!!! Path '$path' is not accessible or present !!!", \OCP\Util::DEBUG); |
|
117 | + } |
|
118 | + return $data; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * scan a single file and store it in the cache |
|
123 | + * |
|
124 | + * @param string $file |
|
125 | + * @param int $reuseExisting |
|
126 | + * @param int $parentId |
|
127 | + * @param array | null $cacheData existing data in the cache for the file to be scanned |
|
128 | + * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
129 | + * @return array an array of metadata of the scanned file |
|
130 | + * @throws \OC\ServerNotAvailableException |
|
131 | + * @throws \OCP\Lock\LockedException |
|
132 | + */ |
|
133 | + public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) { |
|
134 | + if ($file !== '') { |
|
135 | + try { |
|
136 | + $this->storage->verifyPath(dirname($file), basename($file)); |
|
137 | + } catch (\Exception $e) { |
|
138 | + return null; |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + // only proceed if $file is not a partial file nor a blacklisted file |
|
143 | + if (!self::isPartialFile($file) and !Filesystem::isFileBlacklisted($file)) { |
|
144 | + |
|
145 | + //acquire a lock |
|
146 | + if ($lock) { |
|
147 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
148 | + $this->storage->acquireLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + try { |
|
153 | + $data = $this->getData($file); |
|
154 | + } catch (ForbiddenException $e) { |
|
155 | + return null; |
|
156 | + } |
|
157 | + |
|
158 | + if ($data) { |
|
159 | + |
|
160 | + // pre-emit only if it was a file. By that we avoid counting/treating folders as files |
|
161 | + if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
162 | + $this->emit('\OC\Files\Cache\Scanner', 'scanFile', array($file, $this->storageId)); |
|
163 | + \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
164 | + } |
|
165 | + |
|
166 | + $parent = dirname($file); |
|
167 | + if ($parent === '.' or $parent === '/') { |
|
168 | + $parent = ''; |
|
169 | + } |
|
170 | + if ($parentId === -1) { |
|
171 | + $parentId = $this->cache->getParentId($file); |
|
172 | + } |
|
173 | + |
|
174 | + // scan the parent if it's not in the cache (id -1) and the current file is not the root folder |
|
175 | + if ($file and $parentId === -1) { |
|
176 | + $parentData = $this->scanFile($parent); |
|
177 | + if (!$parentData) { |
|
178 | + return null; |
|
179 | + } |
|
180 | + $parentId = $parentData['fileid']; |
|
181 | + } |
|
182 | + if ($parent) { |
|
183 | + $data['parent'] = $parentId; |
|
184 | + } |
|
185 | + if (is_null($cacheData)) { |
|
186 | + /** @var CacheEntry $cacheData */ |
|
187 | + $cacheData = $this->cache->get($file); |
|
188 | + } |
|
189 | + if ($cacheData and $reuseExisting and isset($cacheData['fileid'])) { |
|
190 | + // prevent empty etag |
|
191 | + if (empty($cacheData['etag'])) { |
|
192 | + $etag = $data['etag']; |
|
193 | + } else { |
|
194 | + $etag = $cacheData['etag']; |
|
195 | + } |
|
196 | + $fileId = $cacheData['fileid']; |
|
197 | + $data['fileid'] = $fileId; |
|
198 | + // only reuse data if the file hasn't explicitly changed |
|
199 | + if (isset($data['storage_mtime']) && isset($cacheData['storage_mtime']) && $data['storage_mtime'] === $cacheData['storage_mtime']) { |
|
200 | + $data['mtime'] = $cacheData['mtime']; |
|
201 | + if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) { |
|
202 | + $data['size'] = $cacheData['size']; |
|
203 | + } |
|
204 | + if ($reuseExisting & self::REUSE_ETAG) { |
|
205 | + $data['etag'] = $etag; |
|
206 | + } |
|
207 | + } |
|
208 | + // Only update metadata that has changed |
|
209 | + $newData = array_diff_assoc($data, $cacheData->getData()); |
|
210 | + } else { |
|
211 | + $newData = $data; |
|
212 | + $fileId = -1; |
|
213 | + } |
|
214 | + if (!empty($newData)) { |
|
215 | + // Reset the checksum if the data has changed |
|
216 | + $newData['checksum'] = ''; |
|
217 | + $data['fileid'] = $this->addToCache($file, $newData, $fileId); |
|
218 | + } |
|
219 | + if (isset($cacheData['size'])) { |
|
220 | + $data['oldSize'] = $cacheData['size']; |
|
221 | + } else { |
|
222 | + $data['oldSize'] = 0; |
|
223 | + } |
|
224 | + |
|
225 | + if (isset($cacheData['encrypted'])) { |
|
226 | + $data['encrypted'] = $cacheData['encrypted']; |
|
227 | + } |
|
228 | + |
|
229 | + // post-emit only if it was a file. By that we avoid counting/treating folders as files |
|
230 | + if ($data['mimetype'] !== 'httpd/unix-directory') { |
|
231 | + $this->emit('\OC\Files\Cache\Scanner', 'postScanFile', array($file, $this->storageId)); |
|
232 | + \OC_Hook::emit('\OC\Files\Cache\Scanner', 'post_scan_file', array('path' => $file, 'storage' => $this->storageId)); |
|
233 | + } |
|
234 | + |
|
235 | + } else { |
|
236 | + $this->removeFromCache($file); |
|
237 | + } |
|
238 | + |
|
239 | + //release the acquired lock |
|
240 | + if ($lock) { |
|
241 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
242 | + $this->storage->releaseLock($file, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + if ($data && !isset($data['encrypted'])) { |
|
247 | + $data['encrypted'] = false; |
|
248 | + } |
|
249 | + return $data; |
|
250 | + } |
|
251 | + |
|
252 | + return null; |
|
253 | + } |
|
254 | + |
|
255 | + protected function removeFromCache($path) { |
|
256 | + \OC_Hook::emit('Scanner', 'removeFromCache', array('file' => $path)); |
|
257 | + $this->emit('\OC\Files\Cache\Scanner', 'removeFromCache', array($path)); |
|
258 | + if ($this->cacheActive) { |
|
259 | + $this->cache->remove($path); |
|
260 | + } |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * @param string $path |
|
265 | + * @param array $data |
|
266 | + * @param int $fileId |
|
267 | + * @return int the id of the added file |
|
268 | + */ |
|
269 | + protected function addToCache($path, $data, $fileId = -1) { |
|
270 | + \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
271 | + $this->emit('\OC\Files\Cache\Scanner', 'addToCache', array($path, $this->storageId, $data)); |
|
272 | + if ($this->cacheActive) { |
|
273 | + if ($fileId !== -1) { |
|
274 | + $this->cache->update($fileId, $data); |
|
275 | + return $fileId; |
|
276 | + } else { |
|
277 | + return $this->cache->put($path, $data); |
|
278 | + } |
|
279 | + } else { |
|
280 | + return -1; |
|
281 | + } |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * @param string $path |
|
286 | + * @param array $data |
|
287 | + * @param int $fileId |
|
288 | + */ |
|
289 | + protected function updateCache($path, $data, $fileId = -1) { |
|
290 | + \OC_Hook::emit('Scanner', 'addToCache', array('file' => $path, 'data' => $data)); |
|
291 | + $this->emit('\OC\Files\Cache\Scanner', 'updateCache', array($path, $this->storageId, $data)); |
|
292 | + if ($this->cacheActive) { |
|
293 | + if ($fileId !== -1) { |
|
294 | + $this->cache->update($fileId, $data); |
|
295 | + } else { |
|
296 | + $this->cache->put($path, $data); |
|
297 | + } |
|
298 | + } |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * scan a folder and all it's children |
|
303 | + * |
|
304 | + * @param string $path |
|
305 | + * @param bool $recursive |
|
306 | + * @param int $reuse |
|
307 | + * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
308 | + * @return array an array of the meta data of the scanned file or folder |
|
309 | + */ |
|
310 | + public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) { |
|
311 | + if ($reuse === -1) { |
|
312 | + $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
313 | + } |
|
314 | + if ($lock) { |
|
315 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
316 | + $this->storage->acquireLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
317 | + $this->storage->acquireLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
318 | + } |
|
319 | + } |
|
320 | + $data = $this->scanFile($path, $reuse, -1, null, $lock); |
|
321 | + if ($data and $data['mimetype'] === 'httpd/unix-directory') { |
|
322 | + $size = $this->scanChildren($path, $recursive, $reuse, $data['fileid'], $lock); |
|
323 | + $data['size'] = $size; |
|
324 | + } |
|
325 | + if ($lock) { |
|
326 | + if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
327 | + $this->storage->releaseLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
|
328 | + $this->storage->releaseLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
329 | + } |
|
330 | + } |
|
331 | + return $data; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Get the children currently in the cache |
|
336 | + * |
|
337 | + * @param int $folderId |
|
338 | + * @return array[] |
|
339 | + */ |
|
340 | + protected function getExistingChildren($folderId) { |
|
341 | + $existingChildren = array(); |
|
342 | + $children = $this->cache->getFolderContentsById($folderId); |
|
343 | + foreach ($children as $child) { |
|
344 | + $existingChildren[$child['name']] = $child; |
|
345 | + } |
|
346 | + return $existingChildren; |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Get the children from the storage |
|
351 | + * |
|
352 | + * @param string $folder |
|
353 | + * @return string[] |
|
354 | + */ |
|
355 | + protected function getNewChildren($folder) { |
|
356 | + $children = array(); |
|
357 | + if ($dh = $this->storage->opendir($folder)) { |
|
358 | + if (is_resource($dh)) { |
|
359 | + while (($file = readdir($dh)) !== false) { |
|
360 | + if (!Filesystem::isIgnoredDir($file)) { |
|
361 | + $children[] = trim(\OC\Files\Filesystem::normalizePath($file), '/'); |
|
362 | + } |
|
363 | + } |
|
364 | + } |
|
365 | + } |
|
366 | + return $children; |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * scan all the files and folders in a folder |
|
371 | + * |
|
372 | + * @param string $path |
|
373 | + * @param bool $recursive |
|
374 | + * @param int $reuse |
|
375 | + * @param int $folderId id for the folder to be scanned |
|
376 | + * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
377 | + * @return int the size of the scanned folder or -1 if the size is unknown at this stage |
|
378 | + */ |
|
379 | + protected function scanChildren($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $folderId = null, $lock = true) { |
|
380 | + if ($reuse === -1) { |
|
381 | + $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : self::REUSE_ETAG; |
|
382 | + } |
|
383 | + $this->emit('\OC\Files\Cache\Scanner', 'scanFolder', array($path, $this->storageId)); |
|
384 | + $size = 0; |
|
385 | + if (!is_null($folderId)) { |
|
386 | + $folderId = $this->cache->getId($path); |
|
387 | + } |
|
388 | + $childQueue = $this->handleChildren($path, $recursive, $reuse, $folderId, $lock, $size); |
|
389 | + |
|
390 | + foreach ($childQueue as $child => $childId) { |
|
391 | + $childSize = $this->scanChildren($child, $recursive, $reuse, $childId, $lock); |
|
392 | + if ($childSize === -1) { |
|
393 | + $size = -1; |
|
394 | + } else if ($size !== -1) { |
|
395 | + $size += $childSize; |
|
396 | + } |
|
397 | + } |
|
398 | + if ($this->cacheActive) { |
|
399 | + $this->cache->update($folderId, array('size' => $size)); |
|
400 | + } |
|
401 | + $this->emit('\OC\Files\Cache\Scanner', 'postScanFolder', array($path, $this->storageId)); |
|
402 | + return $size; |
|
403 | + } |
|
404 | + |
|
405 | + private function handleChildren($path, $recursive, $reuse, $folderId, $lock, &$size) { |
|
406 | + // we put this in it's own function so it cleans up the memory before we start recursing |
|
407 | + $existingChildren = $this->getExistingChildren($folderId); |
|
408 | + $newChildren = $this->getNewChildren($path); |
|
409 | + |
|
410 | + if ($this->useTransactions) { |
|
411 | + \OC::$server->getDatabaseConnection()->beginTransaction(); |
|
412 | + } |
|
413 | + |
|
414 | + $exceptionOccurred = false; |
|
415 | + $childQueue = []; |
|
416 | + foreach ($newChildren as $file) { |
|
417 | + $child = ($path) ? $path . '/' . $file : $file; |
|
418 | + try { |
|
419 | + $existingData = isset($existingChildren[$file]) ? $existingChildren[$file] : null; |
|
420 | + $data = $this->scanFile($child, $reuse, $folderId, $existingData, $lock); |
|
421 | + if ($data) { |
|
422 | + if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE) { |
|
423 | + $childQueue[$child] = $data['fileid']; |
|
424 | + } else if ($data['mimetype'] === 'httpd/unix-directory' and $recursive === self::SCAN_RECURSIVE_INCOMPLETE and $data['size'] === -1) { |
|
425 | + // only recurse into folders which aren't fully scanned |
|
426 | + $childQueue[$child] = $data['fileid']; |
|
427 | + } else if ($data['size'] === -1) { |
|
428 | + $size = -1; |
|
429 | + } else if ($size !== -1) { |
|
430 | + $size += $data['size']; |
|
431 | + } |
|
432 | + } |
|
433 | + } catch (\Doctrine\DBAL\DBALException $ex) { |
|
434 | + // might happen if inserting duplicate while a scanning |
|
435 | + // process is running in parallel |
|
436 | + // log and ignore |
|
437 | + \OCP\Util::writeLog('core', 'Exception while scanning file "' . $child . '": ' . $ex->getMessage(), \OCP\Util::DEBUG); |
|
438 | + $exceptionOccurred = true; |
|
439 | + } catch (\OCP\Lock\LockedException $e) { |
|
440 | + if ($this->useTransactions) { |
|
441 | + \OC::$server->getDatabaseConnection()->rollback(); |
|
442 | + } |
|
443 | + throw $e; |
|
444 | + } |
|
445 | + } |
|
446 | + $removedChildren = \array_diff(array_keys($existingChildren), $newChildren); |
|
447 | + foreach ($removedChildren as $childName) { |
|
448 | + $child = ($path) ? $path . '/' . $childName : $childName; |
|
449 | + $this->removeFromCache($child); |
|
450 | + } |
|
451 | + if ($this->useTransactions) { |
|
452 | + \OC::$server->getDatabaseConnection()->commit(); |
|
453 | + } |
|
454 | + if ($exceptionOccurred) { |
|
455 | + // It might happen that the parallel scan process has already |
|
456 | + // inserted mimetypes but those weren't available yet inside the transaction |
|
457 | + // To make sure to have the updated mime types in such cases, |
|
458 | + // we reload them here |
|
459 | + \OC::$server->getMimeTypeLoader()->reset(); |
|
460 | + } |
|
461 | + return $childQueue; |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * check if the file should be ignored when scanning |
|
466 | + * NOTE: files with a '.part' extension are ignored as well! |
|
467 | + * prevents unfinished put requests to be scanned |
|
468 | + * |
|
469 | + * @param string $file |
|
470 | + * @return boolean |
|
471 | + */ |
|
472 | + public static function isPartialFile($file) { |
|
473 | + if (pathinfo($file, PATHINFO_EXTENSION) === 'part') { |
|
474 | + return true; |
|
475 | + } |
|
476 | + if (strpos($file, '.part/') !== false) { |
|
477 | + return true; |
|
478 | + } |
|
479 | + |
|
480 | + return false; |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * walk over any folders that are not fully scanned yet and scan them |
|
485 | + */ |
|
486 | + public function backgroundScan() { |
|
487 | + if (!$this->cache->inCache('')) { |
|
488 | + $this->runBackgroundScanJob(function () { |
|
489 | + $this->scan('', self::SCAN_RECURSIVE, self::REUSE_ETAG); |
|
490 | + }, ''); |
|
491 | + } else { |
|
492 | + $lastPath = null; |
|
493 | + while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) { |
|
494 | + $this->runBackgroundScanJob(function () use ($path) { |
|
495 | + $this->scan($path, self::SCAN_RECURSIVE_INCOMPLETE, self::REUSE_ETAG | self::REUSE_SIZE); |
|
496 | + }, $path); |
|
497 | + // FIXME: this won't proceed with the next item, needs revamping of getIncomplete() |
|
498 | + // to make this possible |
|
499 | + $lastPath = $path; |
|
500 | + } |
|
501 | + } |
|
502 | + } |
|
503 | + |
|
504 | + private function runBackgroundScanJob(callable $callback, $path) { |
|
505 | + try { |
|
506 | + $callback(); |
|
507 | + \OC_Hook::emit('Scanner', 'correctFolderSize', array('path' => $path)); |
|
508 | + if ($this->cacheActive && $this->cache instanceof Cache) { |
|
509 | + $this->cache->correctFolderSize($path); |
|
510 | + } |
|
511 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
512 | + // skip unavailable storages |
|
513 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
514 | + // skip unavailable storages |
|
515 | + } catch (\OCP\Files\ForbiddenException $e) { |
|
516 | + // skip forbidden storages |
|
517 | + } catch (\OCP\Lock\LockedException $e) { |
|
518 | + // skip unavailable storages |
|
519 | + } |
|
520 | + } |
|
521 | + |
|
522 | + /** |
|
523 | + * Set whether the cache is affected by scan operations |
|
524 | + * |
|
525 | + * @param boolean $active The active state of the cache |
|
526 | + */ |
|
527 | + public function setCacheActive($active) { |
|
528 | + $this->cacheActive = $active; |
|
529 | + } |
|
530 | 530 | } |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | } |
314 | 314 | if ($lock) { |
315 | 315 | if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
316 | - $this->storage->acquireLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
316 | + $this->storage->acquireLock('scanner::'.$path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
317 | 317 | $this->storage->acquireLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
318 | 318 | } |
319 | 319 | } |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | if ($lock) { |
326 | 326 | if ($this->storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
327 | 327 | $this->storage->releaseLock($path, ILockingProvider::LOCK_SHARED, $this->lockingProvider); |
328 | - $this->storage->releaseLock('scanner::' . $path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
328 | + $this->storage->releaseLock('scanner::'.$path, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider); |
|
329 | 329 | } |
330 | 330 | } |
331 | 331 | return $data; |
@@ -414,7 +414,7 @@ discard block |
||
414 | 414 | $exceptionOccurred = false; |
415 | 415 | $childQueue = []; |
416 | 416 | foreach ($newChildren as $file) { |
417 | - $child = ($path) ? $path . '/' . $file : $file; |
|
417 | + $child = ($path) ? $path.'/'.$file : $file; |
|
418 | 418 | try { |
419 | 419 | $existingData = isset($existingChildren[$file]) ? $existingChildren[$file] : null; |
420 | 420 | $data = $this->scanFile($child, $reuse, $folderId, $existingData, $lock); |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | // might happen if inserting duplicate while a scanning |
435 | 435 | // process is running in parallel |
436 | 436 | // log and ignore |
437 | - \OCP\Util::writeLog('core', 'Exception while scanning file "' . $child . '": ' . $ex->getMessage(), \OCP\Util::DEBUG); |
|
437 | + \OCP\Util::writeLog('core', 'Exception while scanning file "'.$child.'": '.$ex->getMessage(), \OCP\Util::DEBUG); |
|
438 | 438 | $exceptionOccurred = true; |
439 | 439 | } catch (\OCP\Lock\LockedException $e) { |
440 | 440 | if ($this->useTransactions) { |
@@ -445,7 +445,7 @@ discard block |
||
445 | 445 | } |
446 | 446 | $removedChildren = \array_diff(array_keys($existingChildren), $newChildren); |
447 | 447 | foreach ($removedChildren as $childName) { |
448 | - $child = ($path) ? $path . '/' . $childName : $childName; |
|
448 | + $child = ($path) ? $path.'/'.$childName : $childName; |
|
449 | 449 | $this->removeFromCache($child); |
450 | 450 | } |
451 | 451 | if ($this->useTransactions) { |
@@ -485,13 +485,13 @@ discard block |
||
485 | 485 | */ |
486 | 486 | public function backgroundScan() { |
487 | 487 | if (!$this->cache->inCache('')) { |
488 | - $this->runBackgroundScanJob(function () { |
|
488 | + $this->runBackgroundScanJob(function() { |
|
489 | 489 | $this->scan('', self::SCAN_RECURSIVE, self::REUSE_ETAG); |
490 | 490 | }, ''); |
491 | 491 | } else { |
492 | 492 | $lastPath = null; |
493 | 493 | while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) { |
494 | - $this->runBackgroundScanJob(function () use ($path) { |
|
494 | + $this->runBackgroundScanJob(function() use ($path) { |
|
495 | 495 | $this->scan($path, self::SCAN_RECURSIVE_INCOMPLETE, self::REUSE_ETAG | self::REUSE_SIZE); |
496 | 496 | }, $path); |
497 | 497 | // FIXME: this won't proceed with the next item, needs revamping of getIncomplete() |
@@ -206,7 +206,7 @@ |
||
206 | 206 | } |
207 | 207 | |
208 | 208 | /** |
209 | - * @param $fileId |
|
209 | + * @param integer $fileId |
|
210 | 210 | * @return array |
211 | 211 | * @throws \OCP\Files\NotFoundException |
212 | 212 | */ |
@@ -24,14 +24,11 @@ |
||
24 | 24 | |
25 | 25 | namespace OC\Files\Config; |
26 | 26 | |
27 | -use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
|
28 | -use OC\Files\Filesystem; |
|
29 | 27 | use OCA\Files_Sharing\SharedMount; |
30 | 28 | use OCP\DB\QueryBuilder\IQueryBuilder; |
31 | 29 | use OCP\Files\Config\ICachedMountInfo; |
32 | 30 | use OCP\Files\Config\IUserMountCache; |
33 | 31 | use OCP\Files\Mount\IMountPoint; |
34 | -use OCP\Files\Node; |
|
35 | 32 | use OCP\Files\NotFoundException; |
36 | 33 | use OCP\ICache; |
37 | 34 | use OCP\IDBConnection; |
@@ -44,289 +44,289 @@ |
||
44 | 44 | * Cache mounts points per user in the cache so we can easilly look them up |
45 | 45 | */ |
46 | 46 | class UserMountCache implements IUserMountCache { |
47 | - /** |
|
48 | - * @var IDBConnection |
|
49 | - */ |
|
50 | - private $connection; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var IUserManager |
|
54 | - */ |
|
55 | - private $userManager; |
|
56 | - |
|
57 | - /** |
|
58 | - * Cached mount info. |
|
59 | - * Map of $userId to ICachedMountInfo. |
|
60 | - * |
|
61 | - * @var ICache |
|
62 | - **/ |
|
63 | - private $mountsForUsers; |
|
64 | - |
|
65 | - /** |
|
66 | - * @var ILogger |
|
67 | - */ |
|
68 | - private $logger; |
|
69 | - |
|
70 | - /** |
|
71 | - * @var ICache |
|
72 | - */ |
|
73 | - private $cacheInfoCache; |
|
74 | - |
|
75 | - /** |
|
76 | - * UserMountCache constructor. |
|
77 | - * |
|
78 | - * @param IDBConnection $connection |
|
79 | - * @param IUserManager $userManager |
|
80 | - * @param ILogger $logger |
|
81 | - */ |
|
82 | - public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) { |
|
83 | - $this->connection = $connection; |
|
84 | - $this->userManager = $userManager; |
|
85 | - $this->logger = $logger; |
|
86 | - $this->cacheInfoCache = new CappedMemoryCache(); |
|
87 | - $this->mountsForUsers = new CappedMemoryCache(); |
|
88 | - } |
|
89 | - |
|
90 | - public function registerMounts(IUser $user, array $mounts) { |
|
91 | - // filter out non-proper storages coming from unit tests |
|
92 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
93 | - return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); |
|
94 | - }); |
|
95 | - /** @var ICachedMountInfo[] $newMounts */ |
|
96 | - $newMounts = array_map(function (IMountPoint $mount) use ($user) { |
|
97 | - // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) |
|
98 | - if ($mount->getStorageRootId() === -1) { |
|
99 | - return null; |
|
100 | - } else { |
|
101 | - return new LazyStorageMountInfo($user, $mount); |
|
102 | - } |
|
103 | - }, $mounts); |
|
104 | - $newMounts = array_values(array_filter($newMounts)); |
|
105 | - |
|
106 | - $cachedMounts = $this->getMountsForUser($user); |
|
107 | - $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
108 | - // since we are only looking for mounts for a specific user comparing on root id is enough |
|
109 | - return $mount1->getRootId() - $mount2->getRootId(); |
|
110 | - }; |
|
111 | - |
|
112 | - /** @var ICachedMountInfo[] $addedMounts */ |
|
113 | - $addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff); |
|
114 | - /** @var ICachedMountInfo[] $removedMounts */ |
|
115 | - $removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff); |
|
116 | - |
|
117 | - $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts); |
|
118 | - |
|
119 | - foreach ($addedMounts as $mount) { |
|
120 | - $this->addToCache($mount); |
|
121 | - $this->mountsForUsers[$user->getUID()][] = $mount; |
|
122 | - } |
|
123 | - foreach ($removedMounts as $mount) { |
|
124 | - $this->removeFromCache($mount); |
|
125 | - $index = array_search($mount, $this->mountsForUsers[$user->getUID()]); |
|
126 | - unset($this->mountsForUsers[$user->getUID()][$index]); |
|
127 | - } |
|
128 | - foreach ($changedMounts as $mount) { |
|
129 | - $this->updateCachedMount($mount); |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * @param ICachedMountInfo[] $newMounts |
|
135 | - * @param ICachedMountInfo[] $cachedMounts |
|
136 | - * @return ICachedMountInfo[] |
|
137 | - */ |
|
138 | - private function findChangedMounts(array $newMounts, array $cachedMounts) { |
|
139 | - $changed = []; |
|
140 | - foreach ($newMounts as $newMount) { |
|
141 | - foreach ($cachedMounts as $cachedMount) { |
|
142 | - if ( |
|
143 | - $newMount->getRootId() === $cachedMount->getRootId() && |
|
144 | - ( |
|
145 | - $newMount->getMountPoint() !== $cachedMount->getMountPoint() || |
|
146 | - $newMount->getStorageId() !== $cachedMount->getStorageId() || |
|
147 | - $newMount->getMountId() !== $cachedMount->getMountId() |
|
148 | - ) |
|
149 | - ) { |
|
150 | - $changed[] = $newMount; |
|
151 | - } |
|
152 | - } |
|
153 | - } |
|
154 | - return $changed; |
|
155 | - } |
|
156 | - |
|
157 | - private function addToCache(ICachedMountInfo $mount) { |
|
158 | - if ($mount->getStorageId() !== -1) { |
|
159 | - $this->connection->insertIfNotExist('*PREFIX*mounts', [ |
|
160 | - 'storage_id' => $mount->getStorageId(), |
|
161 | - 'root_id' => $mount->getRootId(), |
|
162 | - 'user_id' => $mount->getUser()->getUID(), |
|
163 | - 'mount_point' => $mount->getMountPoint(), |
|
164 | - 'mount_id' => $mount->getMountId() |
|
165 | - ], ['root_id', 'user_id']); |
|
166 | - } else { |
|
167 | - // in some cases this is legitimate, like orphaned shares |
|
168 | - $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - private function updateCachedMount(ICachedMountInfo $mount) { |
|
173 | - $builder = $this->connection->getQueryBuilder(); |
|
174 | - |
|
175 | - $query = $builder->update('mounts') |
|
176 | - ->set('storage_id', $builder->createNamedParameter($mount->getStorageId())) |
|
177 | - ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint())) |
|
178 | - ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT)) |
|
179 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
180 | - ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
181 | - |
|
182 | - $query->execute(); |
|
183 | - } |
|
184 | - |
|
185 | - private function removeFromCache(ICachedMountInfo $mount) { |
|
186 | - $builder = $this->connection->getQueryBuilder(); |
|
187 | - |
|
188 | - $query = $builder->delete('mounts') |
|
189 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
190 | - ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
191 | - $query->execute(); |
|
192 | - } |
|
193 | - |
|
194 | - private function dbRowToMountInfo(array $row) { |
|
195 | - $user = $this->userManager->get($row['user_id']); |
|
196 | - if (is_null($user)) { |
|
197 | - return null; |
|
198 | - } |
|
199 | - return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path'])? $row['path']:''); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * @param IUser $user |
|
204 | - * @return ICachedMountInfo[] |
|
205 | - */ |
|
206 | - public function getMountsForUser(IUser $user) { |
|
207 | - if (!isset($this->mountsForUsers[$user->getUID()])) { |
|
208 | - $builder = $this->connection->getQueryBuilder(); |
|
209 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
210 | - ->from('mounts', 'm') |
|
211 | - ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
212 | - ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); |
|
213 | - |
|
214 | - $rows = $query->execute()->fetchAll(); |
|
215 | - |
|
216 | - $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
217 | - } |
|
218 | - return $this->mountsForUsers[$user->getUID()]; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * @param int $numericStorageId |
|
223 | - * @return CachedMountInfo[] |
|
224 | - */ |
|
225 | - public function getMountsForStorageId($numericStorageId) { |
|
226 | - $builder = $this->connection->getQueryBuilder(); |
|
227 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
228 | - ->from('mounts', 'm') |
|
229 | - ->innerJoin('m', 'filecache', 'f' , $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
230 | - ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); |
|
231 | - |
|
232 | - $rows = $query->execute()->fetchAll(); |
|
233 | - |
|
234 | - return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * @param int $rootFileId |
|
239 | - * @return CachedMountInfo[] |
|
240 | - */ |
|
241 | - public function getMountsForRootId($rootFileId) { |
|
242 | - $builder = $this->connection->getQueryBuilder(); |
|
243 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
244 | - ->from('mounts', 'm') |
|
245 | - ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
246 | - ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); |
|
247 | - |
|
248 | - $rows = $query->execute()->fetchAll(); |
|
249 | - |
|
250 | - return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * @param $fileId |
|
255 | - * @return array |
|
256 | - * @throws \OCP\Files\NotFoundException |
|
257 | - */ |
|
258 | - private function getCacheInfoFromFileId($fileId) { |
|
259 | - if (!isset($this->cacheInfoCache[$fileId])) { |
|
260 | - $builder = $this->connection->getQueryBuilder(); |
|
261 | - $query = $builder->select('storage', 'path', 'mimetype') |
|
262 | - ->from('filecache') |
|
263 | - ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
264 | - |
|
265 | - $row = $query->execute()->fetch(); |
|
266 | - if (is_array($row)) { |
|
267 | - $this->cacheInfoCache[$fileId] = [ |
|
268 | - (int)$row['storage'], |
|
269 | - $row['path'], |
|
270 | - (int)$row['mimetype'] |
|
271 | - ]; |
|
272 | - } else { |
|
273 | - throw new NotFoundException('File with id "' . $fileId . '" not found'); |
|
274 | - } |
|
275 | - } |
|
276 | - return $this->cacheInfoCache[$fileId]; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * @param int $fileId |
|
281 | - * @return ICachedMountInfo[] |
|
282 | - * @since 9.0.0 |
|
283 | - */ |
|
284 | - public function getMountsForFileId($fileId) { |
|
285 | - try { |
|
286 | - list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); |
|
287 | - } catch (NotFoundException $e) { |
|
288 | - return []; |
|
289 | - } |
|
290 | - $mountsForStorage = $this->getMountsForStorageId($storageId); |
|
291 | - |
|
292 | - // filter mounts that are from the same storage but a different directory |
|
293 | - return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
294 | - if ($fileId === $mount->getRootId()) { |
|
295 | - return true; |
|
296 | - } |
|
297 | - $internalMountPath = $mount->getRootInternalPath(); |
|
298 | - |
|
299 | - return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
|
300 | - }); |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Remove all cached mounts for a user |
|
305 | - * |
|
306 | - * @param IUser $user |
|
307 | - */ |
|
308 | - public function removeUserMounts(IUser $user) { |
|
309 | - $builder = $this->connection->getQueryBuilder(); |
|
310 | - |
|
311 | - $query = $builder->delete('mounts') |
|
312 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID()))); |
|
313 | - $query->execute(); |
|
314 | - } |
|
315 | - |
|
316 | - public function removeUserStorageMount($storageId, $userId) { |
|
317 | - $builder = $this->connection->getQueryBuilder(); |
|
318 | - |
|
319 | - $query = $builder->delete('mounts') |
|
320 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId))) |
|
321 | - ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
322 | - $query->execute(); |
|
323 | - } |
|
324 | - |
|
325 | - public function remoteStorageMounts($storageId) { |
|
326 | - $builder = $this->connection->getQueryBuilder(); |
|
327 | - |
|
328 | - $query = $builder->delete('mounts') |
|
329 | - ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
330 | - $query->execute(); |
|
331 | - } |
|
47 | + /** |
|
48 | + * @var IDBConnection |
|
49 | + */ |
|
50 | + private $connection; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var IUserManager |
|
54 | + */ |
|
55 | + private $userManager; |
|
56 | + |
|
57 | + /** |
|
58 | + * Cached mount info. |
|
59 | + * Map of $userId to ICachedMountInfo. |
|
60 | + * |
|
61 | + * @var ICache |
|
62 | + **/ |
|
63 | + private $mountsForUsers; |
|
64 | + |
|
65 | + /** |
|
66 | + * @var ILogger |
|
67 | + */ |
|
68 | + private $logger; |
|
69 | + |
|
70 | + /** |
|
71 | + * @var ICache |
|
72 | + */ |
|
73 | + private $cacheInfoCache; |
|
74 | + |
|
75 | + /** |
|
76 | + * UserMountCache constructor. |
|
77 | + * |
|
78 | + * @param IDBConnection $connection |
|
79 | + * @param IUserManager $userManager |
|
80 | + * @param ILogger $logger |
|
81 | + */ |
|
82 | + public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) { |
|
83 | + $this->connection = $connection; |
|
84 | + $this->userManager = $userManager; |
|
85 | + $this->logger = $logger; |
|
86 | + $this->cacheInfoCache = new CappedMemoryCache(); |
|
87 | + $this->mountsForUsers = new CappedMemoryCache(); |
|
88 | + } |
|
89 | + |
|
90 | + public function registerMounts(IUser $user, array $mounts) { |
|
91 | + // filter out non-proper storages coming from unit tests |
|
92 | + $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
93 | + return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); |
|
94 | + }); |
|
95 | + /** @var ICachedMountInfo[] $newMounts */ |
|
96 | + $newMounts = array_map(function (IMountPoint $mount) use ($user) { |
|
97 | + // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) |
|
98 | + if ($mount->getStorageRootId() === -1) { |
|
99 | + return null; |
|
100 | + } else { |
|
101 | + return new LazyStorageMountInfo($user, $mount); |
|
102 | + } |
|
103 | + }, $mounts); |
|
104 | + $newMounts = array_values(array_filter($newMounts)); |
|
105 | + |
|
106 | + $cachedMounts = $this->getMountsForUser($user); |
|
107 | + $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
108 | + // since we are only looking for mounts for a specific user comparing on root id is enough |
|
109 | + return $mount1->getRootId() - $mount2->getRootId(); |
|
110 | + }; |
|
111 | + |
|
112 | + /** @var ICachedMountInfo[] $addedMounts */ |
|
113 | + $addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff); |
|
114 | + /** @var ICachedMountInfo[] $removedMounts */ |
|
115 | + $removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff); |
|
116 | + |
|
117 | + $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts); |
|
118 | + |
|
119 | + foreach ($addedMounts as $mount) { |
|
120 | + $this->addToCache($mount); |
|
121 | + $this->mountsForUsers[$user->getUID()][] = $mount; |
|
122 | + } |
|
123 | + foreach ($removedMounts as $mount) { |
|
124 | + $this->removeFromCache($mount); |
|
125 | + $index = array_search($mount, $this->mountsForUsers[$user->getUID()]); |
|
126 | + unset($this->mountsForUsers[$user->getUID()][$index]); |
|
127 | + } |
|
128 | + foreach ($changedMounts as $mount) { |
|
129 | + $this->updateCachedMount($mount); |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * @param ICachedMountInfo[] $newMounts |
|
135 | + * @param ICachedMountInfo[] $cachedMounts |
|
136 | + * @return ICachedMountInfo[] |
|
137 | + */ |
|
138 | + private function findChangedMounts(array $newMounts, array $cachedMounts) { |
|
139 | + $changed = []; |
|
140 | + foreach ($newMounts as $newMount) { |
|
141 | + foreach ($cachedMounts as $cachedMount) { |
|
142 | + if ( |
|
143 | + $newMount->getRootId() === $cachedMount->getRootId() && |
|
144 | + ( |
|
145 | + $newMount->getMountPoint() !== $cachedMount->getMountPoint() || |
|
146 | + $newMount->getStorageId() !== $cachedMount->getStorageId() || |
|
147 | + $newMount->getMountId() !== $cachedMount->getMountId() |
|
148 | + ) |
|
149 | + ) { |
|
150 | + $changed[] = $newMount; |
|
151 | + } |
|
152 | + } |
|
153 | + } |
|
154 | + return $changed; |
|
155 | + } |
|
156 | + |
|
157 | + private function addToCache(ICachedMountInfo $mount) { |
|
158 | + if ($mount->getStorageId() !== -1) { |
|
159 | + $this->connection->insertIfNotExist('*PREFIX*mounts', [ |
|
160 | + 'storage_id' => $mount->getStorageId(), |
|
161 | + 'root_id' => $mount->getRootId(), |
|
162 | + 'user_id' => $mount->getUser()->getUID(), |
|
163 | + 'mount_point' => $mount->getMountPoint(), |
|
164 | + 'mount_id' => $mount->getMountId() |
|
165 | + ], ['root_id', 'user_id']); |
|
166 | + } else { |
|
167 | + // in some cases this is legitimate, like orphaned shares |
|
168 | + $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + private function updateCachedMount(ICachedMountInfo $mount) { |
|
173 | + $builder = $this->connection->getQueryBuilder(); |
|
174 | + |
|
175 | + $query = $builder->update('mounts') |
|
176 | + ->set('storage_id', $builder->createNamedParameter($mount->getStorageId())) |
|
177 | + ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint())) |
|
178 | + ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT)) |
|
179 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
180 | + ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
181 | + |
|
182 | + $query->execute(); |
|
183 | + } |
|
184 | + |
|
185 | + private function removeFromCache(ICachedMountInfo $mount) { |
|
186 | + $builder = $this->connection->getQueryBuilder(); |
|
187 | + |
|
188 | + $query = $builder->delete('mounts') |
|
189 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
190 | + ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
191 | + $query->execute(); |
|
192 | + } |
|
193 | + |
|
194 | + private function dbRowToMountInfo(array $row) { |
|
195 | + $user = $this->userManager->get($row['user_id']); |
|
196 | + if (is_null($user)) { |
|
197 | + return null; |
|
198 | + } |
|
199 | + return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path'])? $row['path']:''); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * @param IUser $user |
|
204 | + * @return ICachedMountInfo[] |
|
205 | + */ |
|
206 | + public function getMountsForUser(IUser $user) { |
|
207 | + if (!isset($this->mountsForUsers[$user->getUID()])) { |
|
208 | + $builder = $this->connection->getQueryBuilder(); |
|
209 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
210 | + ->from('mounts', 'm') |
|
211 | + ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
212 | + ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); |
|
213 | + |
|
214 | + $rows = $query->execute()->fetchAll(); |
|
215 | + |
|
216 | + $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
217 | + } |
|
218 | + return $this->mountsForUsers[$user->getUID()]; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * @param int $numericStorageId |
|
223 | + * @return CachedMountInfo[] |
|
224 | + */ |
|
225 | + public function getMountsForStorageId($numericStorageId) { |
|
226 | + $builder = $this->connection->getQueryBuilder(); |
|
227 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
228 | + ->from('mounts', 'm') |
|
229 | + ->innerJoin('m', 'filecache', 'f' , $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
230 | + ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); |
|
231 | + |
|
232 | + $rows = $query->execute()->fetchAll(); |
|
233 | + |
|
234 | + return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * @param int $rootFileId |
|
239 | + * @return CachedMountInfo[] |
|
240 | + */ |
|
241 | + public function getMountsForRootId($rootFileId) { |
|
242 | + $builder = $this->connection->getQueryBuilder(); |
|
243 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
|
244 | + ->from('mounts', 'm') |
|
245 | + ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
246 | + ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); |
|
247 | + |
|
248 | + $rows = $query->execute()->fetchAll(); |
|
249 | + |
|
250 | + return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * @param $fileId |
|
255 | + * @return array |
|
256 | + * @throws \OCP\Files\NotFoundException |
|
257 | + */ |
|
258 | + private function getCacheInfoFromFileId($fileId) { |
|
259 | + if (!isset($this->cacheInfoCache[$fileId])) { |
|
260 | + $builder = $this->connection->getQueryBuilder(); |
|
261 | + $query = $builder->select('storage', 'path', 'mimetype') |
|
262 | + ->from('filecache') |
|
263 | + ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
264 | + |
|
265 | + $row = $query->execute()->fetch(); |
|
266 | + if (is_array($row)) { |
|
267 | + $this->cacheInfoCache[$fileId] = [ |
|
268 | + (int)$row['storage'], |
|
269 | + $row['path'], |
|
270 | + (int)$row['mimetype'] |
|
271 | + ]; |
|
272 | + } else { |
|
273 | + throw new NotFoundException('File with id "' . $fileId . '" not found'); |
|
274 | + } |
|
275 | + } |
|
276 | + return $this->cacheInfoCache[$fileId]; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * @param int $fileId |
|
281 | + * @return ICachedMountInfo[] |
|
282 | + * @since 9.0.0 |
|
283 | + */ |
|
284 | + public function getMountsForFileId($fileId) { |
|
285 | + try { |
|
286 | + list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); |
|
287 | + } catch (NotFoundException $e) { |
|
288 | + return []; |
|
289 | + } |
|
290 | + $mountsForStorage = $this->getMountsForStorageId($storageId); |
|
291 | + |
|
292 | + // filter mounts that are from the same storage but a different directory |
|
293 | + return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
294 | + if ($fileId === $mount->getRootId()) { |
|
295 | + return true; |
|
296 | + } |
|
297 | + $internalMountPath = $mount->getRootInternalPath(); |
|
298 | + |
|
299 | + return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
|
300 | + }); |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Remove all cached mounts for a user |
|
305 | + * |
|
306 | + * @param IUser $user |
|
307 | + */ |
|
308 | + public function removeUserMounts(IUser $user) { |
|
309 | + $builder = $this->connection->getQueryBuilder(); |
|
310 | + |
|
311 | + $query = $builder->delete('mounts') |
|
312 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID()))); |
|
313 | + $query->execute(); |
|
314 | + } |
|
315 | + |
|
316 | + public function removeUserStorageMount($storageId, $userId) { |
|
317 | + $builder = $this->connection->getQueryBuilder(); |
|
318 | + |
|
319 | + $query = $builder->delete('mounts') |
|
320 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId))) |
|
321 | + ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
322 | + $query->execute(); |
|
323 | + } |
|
324 | + |
|
325 | + public function remoteStorageMounts($storageId) { |
|
326 | + $builder = $this->connection->getQueryBuilder(); |
|
327 | + |
|
328 | + $query = $builder->delete('mounts') |
|
329 | + ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
330 | + $query->execute(); |
|
331 | + } |
|
332 | 332 | } |
@@ -89,11 +89,11 @@ discard block |
||
89 | 89 | |
90 | 90 | public function registerMounts(IUser $user, array $mounts) { |
91 | 91 | // filter out non-proper storages coming from unit tests |
92 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
92 | + $mounts = array_filter($mounts, function(IMountPoint $mount) { |
|
93 | 93 | return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); |
94 | 94 | }); |
95 | 95 | /** @var ICachedMountInfo[] $newMounts */ |
96 | - $newMounts = array_map(function (IMountPoint $mount) use ($user) { |
|
96 | + $newMounts = array_map(function(IMountPoint $mount) use ($user) { |
|
97 | 97 | // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) |
98 | 98 | if ($mount->getStorageRootId() === -1) { |
99 | 99 | return null; |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | $newMounts = array_values(array_filter($newMounts)); |
105 | 105 | |
106 | 106 | $cachedMounts = $this->getMountsForUser($user); |
107 | - $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
107 | + $mountDiff = function(ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
108 | 108 | // since we are only looking for mounts for a specific user comparing on root id is enough |
109 | 109 | return $mount1->getRootId() - $mount2->getRootId(); |
110 | 110 | }; |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | ], ['root_id', 'user_id']); |
166 | 166 | } else { |
167 | 167 | // in some cases this is legitimate, like orphaned shares |
168 | - $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); |
|
168 | + $this->logger->debug('Could not get storage info for mount at '.$mount->getMountPoint()); |
|
169 | 169 | } |
170 | 170 | } |
171 | 171 | |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | if (is_null($user)) { |
197 | 197 | return null; |
198 | 198 | } |
199 | - return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path'])? $row['path']:''); |
|
199 | + return new CachedMountInfo($user, (int) $row['storage_id'], (int) $row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path']) ? $row['path'] : ''); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | /** |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | $builder = $this->connection->getQueryBuilder(); |
227 | 227 | $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') |
228 | 228 | ->from('mounts', 'm') |
229 | - ->innerJoin('m', 'filecache', 'f' , $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
229 | + ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) |
|
230 | 230 | ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); |
231 | 231 | |
232 | 232 | $rows = $query->execute()->fetchAll(); |
@@ -265,12 +265,12 @@ discard block |
||
265 | 265 | $row = $query->execute()->fetch(); |
266 | 266 | if (is_array($row)) { |
267 | 267 | $this->cacheInfoCache[$fileId] = [ |
268 | - (int)$row['storage'], |
|
268 | + (int) $row['storage'], |
|
269 | 269 | $row['path'], |
270 | - (int)$row['mimetype'] |
|
270 | + (int) $row['mimetype'] |
|
271 | 271 | ]; |
272 | 272 | } else { |
273 | - throw new NotFoundException('File with id "' . $fileId . '" not found'); |
|
273 | + throw new NotFoundException('File with id "'.$fileId.'" not found'); |
|
274 | 274 | } |
275 | 275 | } |
276 | 276 | return $this->cacheInfoCache[$fileId]; |
@@ -290,13 +290,13 @@ discard block |
||
290 | 290 | $mountsForStorage = $this->getMountsForStorageId($storageId); |
291 | 291 | |
292 | 292 | // filter mounts that are from the same storage but a different directory |
293 | - return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
293 | + return array_filter($mountsForStorage, function(ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
294 | 294 | if ($fileId === $mount->getRootId()) { |
295 | 295 | return true; |
296 | 296 | } |
297 | 297 | $internalMountPath = $mount->getRootInternalPath(); |
298 | 298 | |
299 | - return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
|
299 | + return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath.'/'; |
|
300 | 300 | }); |
301 | 301 | } |
302 | 302 |
@@ -52,7 +52,7 @@ |
||
52 | 52 | * Magic method to first get the real rootFolder and then |
53 | 53 | * call $method with $args on it |
54 | 54 | * |
55 | - * @param $method |
|
55 | + * @param string $method |
|
56 | 56 | * @param $args |
57 | 57 | * @return mixed |
58 | 58 | */ |
@@ -20,9 +20,7 @@ |
||
20 | 20 | */ |
21 | 21 | namespace OC\Files\Node; |
22 | 22 | |
23 | -use OC\Files\Mount\MountPoint; |
|
24 | 23 | use OCP\Files\IRootFolder; |
25 | -use OCP\Files\NotPermittedException; |
|
26 | 24 | |
27 | 25 | /** |
28 | 26 | * Class LazyRoot |
@@ -34,447 +34,447 @@ |
||
34 | 34 | * @package OC\Files\Node |
35 | 35 | */ |
36 | 36 | class LazyRoot implements IRootFolder { |
37 | - /** @var \Closure */ |
|
38 | - private $rootFolderClosure; |
|
39 | - |
|
40 | - /** @var IRootFolder */ |
|
41 | - private $rootFolder; |
|
42 | - |
|
43 | - /** |
|
44 | - * LazyRoot constructor. |
|
45 | - * |
|
46 | - * @param \Closure $rootFolderClosure |
|
47 | - */ |
|
48 | - public function __construct(\Closure $rootFolderClosure) { |
|
49 | - $this->rootFolderClosure = $rootFolderClosure; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Magic method to first get the real rootFolder and then |
|
54 | - * call $method with $args on it |
|
55 | - * |
|
56 | - * @param $method |
|
57 | - * @param $args |
|
58 | - * @return mixed |
|
59 | - */ |
|
60 | - public function __call($method, $args) { |
|
61 | - if ($this->rootFolder === null) { |
|
62 | - $this->rootFolder = call_user_func($this->rootFolderClosure); |
|
63 | - } |
|
64 | - |
|
65 | - return call_user_func_array([$this->rootFolder, $method], $args); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * @inheritDoc |
|
70 | - */ |
|
71 | - public function getUser() { |
|
72 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @inheritDoc |
|
77 | - */ |
|
78 | - public function listen($scope, $method, callable $callback) { |
|
79 | - $this->__call(__FUNCTION__, func_get_args()); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @inheritDoc |
|
84 | - */ |
|
85 | - public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
86 | - $this->__call(__FUNCTION__, func_get_args()); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @inheritDoc |
|
91 | - */ |
|
92 | - public function emit($scope, $method, $arguments = array()) { |
|
93 | - $this->__call(__FUNCTION__, func_get_args()); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @inheritDoc |
|
98 | - */ |
|
99 | - public function mount($storage, $mountPoint, $arguments = array()) { |
|
100 | - $this->__call(__FUNCTION__, func_get_args()); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @inheritDoc |
|
105 | - */ |
|
106 | - public function getMount($mountPoint) { |
|
107 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @inheritDoc |
|
112 | - */ |
|
113 | - public function getMountsIn($mountPoint) { |
|
114 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @inheritDoc |
|
119 | - */ |
|
120 | - public function getMountByStorageId($storageId) { |
|
121 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @inheritDoc |
|
126 | - */ |
|
127 | - public function getMountByNumericStorageId($numericId) { |
|
128 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * @inheritDoc |
|
133 | - */ |
|
134 | - public function unMount($mount) { |
|
135 | - $this->__call(__FUNCTION__, func_get_args()); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @inheritDoc |
|
140 | - */ |
|
141 | - public function get($path) { |
|
142 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @inheritDoc |
|
147 | - */ |
|
148 | - public function rename($targetPath) { |
|
149 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @inheritDoc |
|
154 | - */ |
|
155 | - public function delete() { |
|
156 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @inheritDoc |
|
161 | - */ |
|
162 | - public function copy($targetPath) { |
|
163 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @inheritDoc |
|
168 | - */ |
|
169 | - public function touch($mtime = null) { |
|
170 | - $this->__call(__FUNCTION__, func_get_args()); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @inheritDoc |
|
175 | - */ |
|
176 | - public function getStorage() { |
|
177 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @inheritDoc |
|
182 | - */ |
|
183 | - public function getPath() { |
|
184 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * @inheritDoc |
|
189 | - */ |
|
190 | - public function getInternalPath() { |
|
191 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * @inheritDoc |
|
196 | - */ |
|
197 | - public function getId() { |
|
198 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * @inheritDoc |
|
203 | - */ |
|
204 | - public function stat() { |
|
205 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * @inheritDoc |
|
210 | - */ |
|
211 | - public function getMTime() { |
|
212 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * @inheritDoc |
|
217 | - */ |
|
218 | - public function getSize() { |
|
219 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @inheritDoc |
|
224 | - */ |
|
225 | - public function getEtag() { |
|
226 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * @inheritDoc |
|
231 | - */ |
|
232 | - public function getPermissions() { |
|
233 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * @inheritDoc |
|
238 | - */ |
|
239 | - public function isReadable() { |
|
240 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * @inheritDoc |
|
245 | - */ |
|
246 | - public function isUpdateable() { |
|
247 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * @inheritDoc |
|
252 | - */ |
|
253 | - public function isDeletable() { |
|
254 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * @inheritDoc |
|
259 | - */ |
|
260 | - public function isShareable() { |
|
261 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @inheritDoc |
|
266 | - */ |
|
267 | - public function getParent() { |
|
268 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * @inheritDoc |
|
273 | - */ |
|
274 | - public function getName() { |
|
275 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @inheritDoc |
|
280 | - */ |
|
281 | - public function getUserFolder($userId) { |
|
282 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * @inheritDoc |
|
287 | - */ |
|
288 | - public function getMimetype() { |
|
289 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * @inheritDoc |
|
294 | - */ |
|
295 | - public function getMimePart() { |
|
296 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @inheritDoc |
|
301 | - */ |
|
302 | - public function isEncrypted() { |
|
303 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * @inheritDoc |
|
308 | - */ |
|
309 | - public function getType() { |
|
310 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * @inheritDoc |
|
315 | - */ |
|
316 | - public function isShared() { |
|
317 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * @inheritDoc |
|
322 | - */ |
|
323 | - public function isMounted() { |
|
324 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * @inheritDoc |
|
329 | - */ |
|
330 | - public function getMountPoint() { |
|
331 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @inheritDoc |
|
336 | - */ |
|
337 | - public function getOwner() { |
|
338 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * @inheritDoc |
|
343 | - */ |
|
344 | - public function getChecksum() { |
|
345 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * @inheritDoc |
|
350 | - */ |
|
351 | - public function getFullPath($path) { |
|
352 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * @inheritDoc |
|
357 | - */ |
|
358 | - public function getRelativePath($path) { |
|
359 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * @inheritDoc |
|
364 | - */ |
|
365 | - public function isSubNode($node) { |
|
366 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * @inheritDoc |
|
371 | - */ |
|
372 | - public function getDirectoryListing() { |
|
373 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * @inheritDoc |
|
378 | - */ |
|
379 | - public function nodeExists($path) { |
|
380 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * @inheritDoc |
|
385 | - */ |
|
386 | - public function newFolder($path) { |
|
387 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * @inheritDoc |
|
392 | - */ |
|
393 | - public function newFile($path) { |
|
394 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
395 | - } |
|
396 | - |
|
397 | - /** |
|
398 | - * @inheritDoc |
|
399 | - */ |
|
400 | - public function search($query) { |
|
401 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * @inheritDoc |
|
406 | - */ |
|
407 | - public function searchByMime($mimetype) { |
|
408 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * @inheritDoc |
|
413 | - */ |
|
414 | - public function searchByTag($tag, $userId) { |
|
415 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * @inheritDoc |
|
420 | - */ |
|
421 | - public function getById($id) { |
|
422 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * @inheritDoc |
|
427 | - */ |
|
428 | - public function getFreeSpace() { |
|
429 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * @inheritDoc |
|
434 | - */ |
|
435 | - public function isCreatable() { |
|
436 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * @inheritDoc |
|
441 | - */ |
|
442 | - public function getNonExistingName($name) { |
|
443 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * @inheritDoc |
|
448 | - */ |
|
449 | - public function move($targetPath) { |
|
450 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * @inheritDoc |
|
455 | - */ |
|
456 | - public function lock($type) { |
|
457 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
458 | - } |
|
459 | - |
|
460 | - /** |
|
461 | - * @inheritDoc |
|
462 | - */ |
|
463 | - public function changeLock($targetType) { |
|
464 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * @inheritDoc |
|
469 | - */ |
|
470 | - public function unlock($type) { |
|
471 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
472 | - } |
|
473 | - |
|
474 | - /** |
|
475 | - * @inheritDoc |
|
476 | - */ |
|
477 | - public function getRecent($limit, $offset = 0) { |
|
478 | - return $this->__call(__FUNCTION__, func_get_args()); |
|
479 | - } |
|
37 | + /** @var \Closure */ |
|
38 | + private $rootFolderClosure; |
|
39 | + |
|
40 | + /** @var IRootFolder */ |
|
41 | + private $rootFolder; |
|
42 | + |
|
43 | + /** |
|
44 | + * LazyRoot constructor. |
|
45 | + * |
|
46 | + * @param \Closure $rootFolderClosure |
|
47 | + */ |
|
48 | + public function __construct(\Closure $rootFolderClosure) { |
|
49 | + $this->rootFolderClosure = $rootFolderClosure; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Magic method to first get the real rootFolder and then |
|
54 | + * call $method with $args on it |
|
55 | + * |
|
56 | + * @param $method |
|
57 | + * @param $args |
|
58 | + * @return mixed |
|
59 | + */ |
|
60 | + public function __call($method, $args) { |
|
61 | + if ($this->rootFolder === null) { |
|
62 | + $this->rootFolder = call_user_func($this->rootFolderClosure); |
|
63 | + } |
|
64 | + |
|
65 | + return call_user_func_array([$this->rootFolder, $method], $args); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * @inheritDoc |
|
70 | + */ |
|
71 | + public function getUser() { |
|
72 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @inheritDoc |
|
77 | + */ |
|
78 | + public function listen($scope, $method, callable $callback) { |
|
79 | + $this->__call(__FUNCTION__, func_get_args()); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @inheritDoc |
|
84 | + */ |
|
85 | + public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
86 | + $this->__call(__FUNCTION__, func_get_args()); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @inheritDoc |
|
91 | + */ |
|
92 | + public function emit($scope, $method, $arguments = array()) { |
|
93 | + $this->__call(__FUNCTION__, func_get_args()); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @inheritDoc |
|
98 | + */ |
|
99 | + public function mount($storage, $mountPoint, $arguments = array()) { |
|
100 | + $this->__call(__FUNCTION__, func_get_args()); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @inheritDoc |
|
105 | + */ |
|
106 | + public function getMount($mountPoint) { |
|
107 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @inheritDoc |
|
112 | + */ |
|
113 | + public function getMountsIn($mountPoint) { |
|
114 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @inheritDoc |
|
119 | + */ |
|
120 | + public function getMountByStorageId($storageId) { |
|
121 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @inheritDoc |
|
126 | + */ |
|
127 | + public function getMountByNumericStorageId($numericId) { |
|
128 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * @inheritDoc |
|
133 | + */ |
|
134 | + public function unMount($mount) { |
|
135 | + $this->__call(__FUNCTION__, func_get_args()); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @inheritDoc |
|
140 | + */ |
|
141 | + public function get($path) { |
|
142 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @inheritDoc |
|
147 | + */ |
|
148 | + public function rename($targetPath) { |
|
149 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @inheritDoc |
|
154 | + */ |
|
155 | + public function delete() { |
|
156 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @inheritDoc |
|
161 | + */ |
|
162 | + public function copy($targetPath) { |
|
163 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @inheritDoc |
|
168 | + */ |
|
169 | + public function touch($mtime = null) { |
|
170 | + $this->__call(__FUNCTION__, func_get_args()); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @inheritDoc |
|
175 | + */ |
|
176 | + public function getStorage() { |
|
177 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @inheritDoc |
|
182 | + */ |
|
183 | + public function getPath() { |
|
184 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * @inheritDoc |
|
189 | + */ |
|
190 | + public function getInternalPath() { |
|
191 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * @inheritDoc |
|
196 | + */ |
|
197 | + public function getId() { |
|
198 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * @inheritDoc |
|
203 | + */ |
|
204 | + public function stat() { |
|
205 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * @inheritDoc |
|
210 | + */ |
|
211 | + public function getMTime() { |
|
212 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * @inheritDoc |
|
217 | + */ |
|
218 | + public function getSize() { |
|
219 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @inheritDoc |
|
224 | + */ |
|
225 | + public function getEtag() { |
|
226 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * @inheritDoc |
|
231 | + */ |
|
232 | + public function getPermissions() { |
|
233 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * @inheritDoc |
|
238 | + */ |
|
239 | + public function isReadable() { |
|
240 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * @inheritDoc |
|
245 | + */ |
|
246 | + public function isUpdateable() { |
|
247 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * @inheritDoc |
|
252 | + */ |
|
253 | + public function isDeletable() { |
|
254 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * @inheritDoc |
|
259 | + */ |
|
260 | + public function isShareable() { |
|
261 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @inheritDoc |
|
266 | + */ |
|
267 | + public function getParent() { |
|
268 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * @inheritDoc |
|
273 | + */ |
|
274 | + public function getName() { |
|
275 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @inheritDoc |
|
280 | + */ |
|
281 | + public function getUserFolder($userId) { |
|
282 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * @inheritDoc |
|
287 | + */ |
|
288 | + public function getMimetype() { |
|
289 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * @inheritDoc |
|
294 | + */ |
|
295 | + public function getMimePart() { |
|
296 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @inheritDoc |
|
301 | + */ |
|
302 | + public function isEncrypted() { |
|
303 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * @inheritDoc |
|
308 | + */ |
|
309 | + public function getType() { |
|
310 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * @inheritDoc |
|
315 | + */ |
|
316 | + public function isShared() { |
|
317 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * @inheritDoc |
|
322 | + */ |
|
323 | + public function isMounted() { |
|
324 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * @inheritDoc |
|
329 | + */ |
|
330 | + public function getMountPoint() { |
|
331 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @inheritDoc |
|
336 | + */ |
|
337 | + public function getOwner() { |
|
338 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * @inheritDoc |
|
343 | + */ |
|
344 | + public function getChecksum() { |
|
345 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * @inheritDoc |
|
350 | + */ |
|
351 | + public function getFullPath($path) { |
|
352 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * @inheritDoc |
|
357 | + */ |
|
358 | + public function getRelativePath($path) { |
|
359 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * @inheritDoc |
|
364 | + */ |
|
365 | + public function isSubNode($node) { |
|
366 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * @inheritDoc |
|
371 | + */ |
|
372 | + public function getDirectoryListing() { |
|
373 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * @inheritDoc |
|
378 | + */ |
|
379 | + public function nodeExists($path) { |
|
380 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * @inheritDoc |
|
385 | + */ |
|
386 | + public function newFolder($path) { |
|
387 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * @inheritDoc |
|
392 | + */ |
|
393 | + public function newFile($path) { |
|
394 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
395 | + } |
|
396 | + |
|
397 | + /** |
|
398 | + * @inheritDoc |
|
399 | + */ |
|
400 | + public function search($query) { |
|
401 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * @inheritDoc |
|
406 | + */ |
|
407 | + public function searchByMime($mimetype) { |
|
408 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * @inheritDoc |
|
413 | + */ |
|
414 | + public function searchByTag($tag, $userId) { |
|
415 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * @inheritDoc |
|
420 | + */ |
|
421 | + public function getById($id) { |
|
422 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * @inheritDoc |
|
427 | + */ |
|
428 | + public function getFreeSpace() { |
|
429 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * @inheritDoc |
|
434 | + */ |
|
435 | + public function isCreatable() { |
|
436 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * @inheritDoc |
|
441 | + */ |
|
442 | + public function getNonExistingName($name) { |
|
443 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * @inheritDoc |
|
448 | + */ |
|
449 | + public function move($targetPath) { |
|
450 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * @inheritDoc |
|
455 | + */ |
|
456 | + public function lock($type) { |
|
457 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
458 | + } |
|
459 | + |
|
460 | + /** |
|
461 | + * @inheritDoc |
|
462 | + */ |
|
463 | + public function changeLock($targetType) { |
|
464 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * @inheritDoc |
|
469 | + */ |
|
470 | + public function unlock($type) { |
|
471 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
472 | + } |
|
473 | + |
|
474 | + /** |
|
475 | + * @inheritDoc |
|
476 | + */ |
|
477 | + public function getRecent($limit, $offset = 0) { |
|
478 | + return $this->__call(__FUNCTION__, func_get_args()); |
|
479 | + } |
|
480 | 480 | } |
@@ -54,6 +54,9 @@ |
||
54 | 54 | $this->flysystem->addPlugin(new GetWithMetadata()); |
55 | 55 | } |
56 | 56 | |
57 | + /** |
|
58 | + * @param string $path |
|
59 | + */ |
|
57 | 60 | protected function buildPath($path) { |
58 | 61 | $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path); |
59 | 62 | return ltrim($fullPath, '/'); |
@@ -35,223 +35,223 @@ |
||
35 | 35 | * To use: subclass and call $this->buildFlysystem with the flysystem adapter of choice |
36 | 36 | */ |
37 | 37 | abstract class Flysystem extends Common { |
38 | - /** |
|
39 | - * @var Filesystem |
|
40 | - */ |
|
41 | - protected $flysystem; |
|
38 | + /** |
|
39 | + * @var Filesystem |
|
40 | + */ |
|
41 | + protected $flysystem; |
|
42 | 42 | |
43 | - /** |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - protected $root = ''; |
|
43 | + /** |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + protected $root = ''; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Initialize the storage backend with a flyssytem adapter |
|
50 | - * |
|
51 | - * @param \League\Flysystem\AdapterInterface $adapter |
|
52 | - */ |
|
53 | - protected function buildFlySystem(AdapterInterface $adapter) { |
|
54 | - $this->flysystem = new Filesystem($adapter); |
|
55 | - $this->flysystem->addPlugin(new GetWithMetadata()); |
|
56 | - } |
|
48 | + /** |
|
49 | + * Initialize the storage backend with a flyssytem adapter |
|
50 | + * |
|
51 | + * @param \League\Flysystem\AdapterInterface $adapter |
|
52 | + */ |
|
53 | + protected function buildFlySystem(AdapterInterface $adapter) { |
|
54 | + $this->flysystem = new Filesystem($adapter); |
|
55 | + $this->flysystem->addPlugin(new GetWithMetadata()); |
|
56 | + } |
|
57 | 57 | |
58 | - protected function buildPath($path) { |
|
59 | - $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path); |
|
60 | - return ltrim($fullPath, '/'); |
|
61 | - } |
|
58 | + protected function buildPath($path) { |
|
59 | + $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path); |
|
60 | + return ltrim($fullPath, '/'); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * {@inheritdoc} |
|
65 | - */ |
|
66 | - public function file_get_contents($path) { |
|
67 | - return $this->flysystem->read($this->buildPath($path)); |
|
68 | - } |
|
63 | + /** |
|
64 | + * {@inheritdoc} |
|
65 | + */ |
|
66 | + public function file_get_contents($path) { |
|
67 | + return $this->flysystem->read($this->buildPath($path)); |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * {@inheritdoc} |
|
72 | - */ |
|
73 | - public function file_put_contents($path, $data) { |
|
74 | - return $this->flysystem->put($this->buildPath($path), $data); |
|
75 | - } |
|
70 | + /** |
|
71 | + * {@inheritdoc} |
|
72 | + */ |
|
73 | + public function file_put_contents($path, $data) { |
|
74 | + return $this->flysystem->put($this->buildPath($path), $data); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * {@inheritdoc} |
|
79 | - */ |
|
80 | - public function file_exists($path) { |
|
81 | - return $this->flysystem->has($this->buildPath($path)); |
|
82 | - } |
|
77 | + /** |
|
78 | + * {@inheritdoc} |
|
79 | + */ |
|
80 | + public function file_exists($path) { |
|
81 | + return $this->flysystem->has($this->buildPath($path)); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * {@inheritdoc} |
|
86 | - */ |
|
87 | - public function unlink($path) { |
|
88 | - if ($this->is_dir($path)) { |
|
89 | - return $this->rmdir($path); |
|
90 | - } |
|
91 | - try { |
|
92 | - return $this->flysystem->delete($this->buildPath($path)); |
|
93 | - } catch (FileNotFoundException $e) { |
|
94 | - return false; |
|
95 | - } |
|
96 | - } |
|
84 | + /** |
|
85 | + * {@inheritdoc} |
|
86 | + */ |
|
87 | + public function unlink($path) { |
|
88 | + if ($this->is_dir($path)) { |
|
89 | + return $this->rmdir($path); |
|
90 | + } |
|
91 | + try { |
|
92 | + return $this->flysystem->delete($this->buildPath($path)); |
|
93 | + } catch (FileNotFoundException $e) { |
|
94 | + return false; |
|
95 | + } |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * {@inheritdoc} |
|
100 | - */ |
|
101 | - public function rename($source, $target) { |
|
102 | - if ($this->file_exists($target)) { |
|
103 | - $this->unlink($target); |
|
104 | - } |
|
105 | - return $this->flysystem->rename($this->buildPath($source), $this->buildPath($target)); |
|
106 | - } |
|
98 | + /** |
|
99 | + * {@inheritdoc} |
|
100 | + */ |
|
101 | + public function rename($source, $target) { |
|
102 | + if ($this->file_exists($target)) { |
|
103 | + $this->unlink($target); |
|
104 | + } |
|
105 | + return $this->flysystem->rename($this->buildPath($source), $this->buildPath($target)); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * {@inheritdoc} |
|
110 | - */ |
|
111 | - public function copy($source, $target) { |
|
112 | - if ($this->file_exists($target)) { |
|
113 | - $this->unlink($target); |
|
114 | - } |
|
115 | - return $this->flysystem->copy($this->buildPath($source), $this->buildPath($target)); |
|
116 | - } |
|
108 | + /** |
|
109 | + * {@inheritdoc} |
|
110 | + */ |
|
111 | + public function copy($source, $target) { |
|
112 | + if ($this->file_exists($target)) { |
|
113 | + $this->unlink($target); |
|
114 | + } |
|
115 | + return $this->flysystem->copy($this->buildPath($source), $this->buildPath($target)); |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * {@inheritdoc} |
|
120 | - */ |
|
121 | - public function filesize($path) { |
|
122 | - if ($this->is_dir($path)) { |
|
123 | - return 0; |
|
124 | - } else { |
|
125 | - return $this->flysystem->getSize($this->buildPath($path)); |
|
126 | - } |
|
127 | - } |
|
118 | + /** |
|
119 | + * {@inheritdoc} |
|
120 | + */ |
|
121 | + public function filesize($path) { |
|
122 | + if ($this->is_dir($path)) { |
|
123 | + return 0; |
|
124 | + } else { |
|
125 | + return $this->flysystem->getSize($this->buildPath($path)); |
|
126 | + } |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * {@inheritdoc} |
|
131 | - */ |
|
132 | - public function mkdir($path) { |
|
133 | - if ($this->file_exists($path)) { |
|
134 | - return false; |
|
135 | - } |
|
136 | - return $this->flysystem->createDir($this->buildPath($path)); |
|
137 | - } |
|
129 | + /** |
|
130 | + * {@inheritdoc} |
|
131 | + */ |
|
132 | + public function mkdir($path) { |
|
133 | + if ($this->file_exists($path)) { |
|
134 | + return false; |
|
135 | + } |
|
136 | + return $this->flysystem->createDir($this->buildPath($path)); |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * {@inheritdoc} |
|
141 | - */ |
|
142 | - public function filemtime($path) { |
|
143 | - return $this->flysystem->getTimestamp($this->buildPath($path)); |
|
144 | - } |
|
139 | + /** |
|
140 | + * {@inheritdoc} |
|
141 | + */ |
|
142 | + public function filemtime($path) { |
|
143 | + return $this->flysystem->getTimestamp($this->buildPath($path)); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * {@inheritdoc} |
|
148 | - */ |
|
149 | - public function rmdir($path) { |
|
150 | - try { |
|
151 | - return @$this->flysystem->deleteDir($this->buildPath($path)); |
|
152 | - } catch (FileNotFoundException $e) { |
|
153 | - return false; |
|
154 | - } |
|
155 | - } |
|
146 | + /** |
|
147 | + * {@inheritdoc} |
|
148 | + */ |
|
149 | + public function rmdir($path) { |
|
150 | + try { |
|
151 | + return @$this->flysystem->deleteDir($this->buildPath($path)); |
|
152 | + } catch (FileNotFoundException $e) { |
|
153 | + return false; |
|
154 | + } |
|
155 | + } |
|
156 | 156 | |
157 | - /** |
|
158 | - * {@inheritdoc} |
|
159 | - */ |
|
160 | - public function opendir($path) { |
|
161 | - try { |
|
162 | - $content = $this->flysystem->listContents($this->buildPath($path)); |
|
163 | - } catch (FileNotFoundException $e) { |
|
164 | - return false; |
|
165 | - } |
|
166 | - $names = array_map(function ($object) { |
|
167 | - return $object['basename']; |
|
168 | - }, $content); |
|
169 | - return IteratorDirectory::wrap($names); |
|
170 | - } |
|
157 | + /** |
|
158 | + * {@inheritdoc} |
|
159 | + */ |
|
160 | + public function opendir($path) { |
|
161 | + try { |
|
162 | + $content = $this->flysystem->listContents($this->buildPath($path)); |
|
163 | + } catch (FileNotFoundException $e) { |
|
164 | + return false; |
|
165 | + } |
|
166 | + $names = array_map(function ($object) { |
|
167 | + return $object['basename']; |
|
168 | + }, $content); |
|
169 | + return IteratorDirectory::wrap($names); |
|
170 | + } |
|
171 | 171 | |
172 | - /** |
|
173 | - * {@inheritdoc} |
|
174 | - */ |
|
175 | - public function fopen($path, $mode) { |
|
176 | - $fullPath = $this->buildPath($path); |
|
177 | - $useExisting = true; |
|
178 | - switch ($mode) { |
|
179 | - case 'r': |
|
180 | - case 'rb': |
|
181 | - try { |
|
182 | - return $this->flysystem->readStream($fullPath); |
|
183 | - } catch (FileNotFoundException $e) { |
|
184 | - return false; |
|
185 | - } |
|
186 | - case 'w': |
|
187 | - case 'w+': |
|
188 | - case 'wb': |
|
189 | - case 'wb+': |
|
190 | - $useExisting = false; |
|
191 | - case 'a': |
|
192 | - case 'ab': |
|
193 | - case 'r+': |
|
194 | - case 'a+': |
|
195 | - case 'x': |
|
196 | - case 'x+': |
|
197 | - case 'c': |
|
198 | - case 'c+': |
|
199 | - //emulate these |
|
200 | - if ($useExisting and $this->file_exists($path)) { |
|
201 | - if (!$this->isUpdatable($path)) { |
|
202 | - return false; |
|
203 | - } |
|
204 | - $tmpFile = $this->getCachedFile($path); |
|
205 | - } else { |
|
206 | - if (!$this->isCreatable(dirname($path))) { |
|
207 | - return false; |
|
208 | - } |
|
209 | - $tmpFile = \OCP\Files::tmpFile(); |
|
210 | - } |
|
211 | - $source = fopen($tmpFile, $mode); |
|
212 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) { |
|
213 | - $this->flysystem->putStream($fullPath, fopen($tmpFile, 'r')); |
|
214 | - unlink($tmpFile); |
|
215 | - }); |
|
216 | - } |
|
217 | - return false; |
|
218 | - } |
|
172 | + /** |
|
173 | + * {@inheritdoc} |
|
174 | + */ |
|
175 | + public function fopen($path, $mode) { |
|
176 | + $fullPath = $this->buildPath($path); |
|
177 | + $useExisting = true; |
|
178 | + switch ($mode) { |
|
179 | + case 'r': |
|
180 | + case 'rb': |
|
181 | + try { |
|
182 | + return $this->flysystem->readStream($fullPath); |
|
183 | + } catch (FileNotFoundException $e) { |
|
184 | + return false; |
|
185 | + } |
|
186 | + case 'w': |
|
187 | + case 'w+': |
|
188 | + case 'wb': |
|
189 | + case 'wb+': |
|
190 | + $useExisting = false; |
|
191 | + case 'a': |
|
192 | + case 'ab': |
|
193 | + case 'r+': |
|
194 | + case 'a+': |
|
195 | + case 'x': |
|
196 | + case 'x+': |
|
197 | + case 'c': |
|
198 | + case 'c+': |
|
199 | + //emulate these |
|
200 | + if ($useExisting and $this->file_exists($path)) { |
|
201 | + if (!$this->isUpdatable($path)) { |
|
202 | + return false; |
|
203 | + } |
|
204 | + $tmpFile = $this->getCachedFile($path); |
|
205 | + } else { |
|
206 | + if (!$this->isCreatable(dirname($path))) { |
|
207 | + return false; |
|
208 | + } |
|
209 | + $tmpFile = \OCP\Files::tmpFile(); |
|
210 | + } |
|
211 | + $source = fopen($tmpFile, $mode); |
|
212 | + return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) { |
|
213 | + $this->flysystem->putStream($fullPath, fopen($tmpFile, 'r')); |
|
214 | + unlink($tmpFile); |
|
215 | + }); |
|
216 | + } |
|
217 | + return false; |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * {@inheritdoc} |
|
222 | - */ |
|
223 | - public function touch($path, $mtime = null) { |
|
224 | - if ($this->file_exists($path)) { |
|
225 | - return false; |
|
226 | - } else { |
|
227 | - $this->file_put_contents($path, ''); |
|
228 | - return true; |
|
229 | - } |
|
230 | - } |
|
220 | + /** |
|
221 | + * {@inheritdoc} |
|
222 | + */ |
|
223 | + public function touch($path, $mtime = null) { |
|
224 | + if ($this->file_exists($path)) { |
|
225 | + return false; |
|
226 | + } else { |
|
227 | + $this->file_put_contents($path, ''); |
|
228 | + return true; |
|
229 | + } |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * {@inheritdoc} |
|
234 | - */ |
|
235 | - public function stat($path) { |
|
236 | - $info = $this->flysystem->getWithMetadata($this->buildPath($path), ['timestamp', 'size']); |
|
237 | - return [ |
|
238 | - 'mtime' => $info['timestamp'], |
|
239 | - 'size' => $info['size'] |
|
240 | - ]; |
|
241 | - } |
|
232 | + /** |
|
233 | + * {@inheritdoc} |
|
234 | + */ |
|
235 | + public function stat($path) { |
|
236 | + $info = $this->flysystem->getWithMetadata($this->buildPath($path), ['timestamp', 'size']); |
|
237 | + return [ |
|
238 | + 'mtime' => $info['timestamp'], |
|
239 | + 'size' => $info['size'] |
|
240 | + ]; |
|
241 | + } |
|
242 | 242 | |
243 | - /** |
|
244 | - * {@inheritdoc} |
|
245 | - */ |
|
246 | - public function filetype($path) { |
|
247 | - if ($path === '' or $path === '/' or $path === '.') { |
|
248 | - return 'dir'; |
|
249 | - } |
|
250 | - try { |
|
251 | - $info = $this->flysystem->getMetadata($this->buildPath($path)); |
|
252 | - } catch (FileNotFoundException $e) { |
|
253 | - return false; |
|
254 | - } |
|
255 | - return $info['type']; |
|
256 | - } |
|
243 | + /** |
|
244 | + * {@inheritdoc} |
|
245 | + */ |
|
246 | + public function filetype($path) { |
|
247 | + if ($path === '' or $path === '/' or $path === '.') { |
|
248 | + return 'dir'; |
|
249 | + } |
|
250 | + try { |
|
251 | + $info = $this->flysystem->getMetadata($this->buildPath($path)); |
|
252 | + } catch (FileNotFoundException $e) { |
|
253 | + return false; |
|
254 | + } |
|
255 | + return $info['type']; |
|
256 | + } |
|
257 | 257 | } |
@@ -56,7 +56,7 @@ discard block |
||
56 | 56 | } |
57 | 57 | |
58 | 58 | protected function buildPath($path) { |
59 | - $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path); |
|
59 | + $fullPath = \OC\Files\Filesystem::normalizePath($this->root.'/'.$path); |
|
60 | 60 | return ltrim($fullPath, '/'); |
61 | 61 | } |
62 | 62 | |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | } catch (FileNotFoundException $e) { |
164 | 164 | return false; |
165 | 165 | } |
166 | - $names = array_map(function ($object) { |
|
166 | + $names = array_map(function($object) { |
|
167 | 167 | return $object['basename']; |
168 | 168 | }, $content); |
169 | 169 | return IteratorDirectory::wrap($names); |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | $tmpFile = \OCP\Files::tmpFile(); |
210 | 210 | } |
211 | 211 | $source = fopen($tmpFile, $mode); |
212 | - return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) { |
|
212 | + return CallbackWrapper::wrap($source, null, null, function() use ($tmpFile, $fullPath) { |
|
213 | 213 | $this->flysystem->putStream($fullPath, fopen($tmpFile, 'r')); |
214 | 214 | unlink($tmpFile); |
215 | 215 | }); |
@@ -159,7 +159,7 @@ |
||
159 | 159 | * Checks whether the given path is a part file |
160 | 160 | * |
161 | 161 | * @param string $path Path that may identify a .part file |
162 | - * @return string File path without .part extension |
|
162 | + * @return boolean File path without .part extension |
|
163 | 163 | * @note this is needed for reusing keys |
164 | 164 | */ |
165 | 165 | private function isPartFile($path) { |
@@ -30,172 +30,172 @@ |
||
30 | 30 | |
31 | 31 | class Quota extends Wrapper { |
32 | 32 | |
33 | - /** |
|
34 | - * @var int $quota |
|
35 | - */ |
|
36 | - protected $quota; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var string $sizeRoot |
|
40 | - */ |
|
41 | - protected $sizeRoot; |
|
42 | - |
|
43 | - /** |
|
44 | - * @param array $parameters |
|
45 | - */ |
|
46 | - public function __construct($parameters) { |
|
47 | - $this->storage = $parameters['storage']; |
|
48 | - $this->quota = $parameters['quota']; |
|
49 | - $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return int quota value |
|
54 | - */ |
|
55 | - public function getQuota() { |
|
56 | - return $this->quota; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param string $path |
|
61 | - * @param \OC\Files\Storage\Storage $storage |
|
62 | - */ |
|
63 | - protected function getSize($path, $storage = null) { |
|
64 | - if (is_null($storage)) { |
|
65 | - $cache = $this->getCache(); |
|
66 | - } else { |
|
67 | - $cache = $storage->getCache(); |
|
68 | - } |
|
69 | - $data = $cache->get($path); |
|
70 | - if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
71 | - return $data['size']; |
|
72 | - } else { |
|
73 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Get free space as limited by the quota |
|
79 | - * |
|
80 | - * @param string $path |
|
81 | - * @return int |
|
82 | - */ |
|
83 | - public function free_space($path) { |
|
84 | - if ($this->quota < 0) { |
|
85 | - return $this->storage->free_space($path); |
|
86 | - } else { |
|
87 | - $used = $this->getSize($this->sizeRoot); |
|
88 | - if ($used < 0) { |
|
89 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
90 | - } else { |
|
91 | - $free = $this->storage->free_space($path); |
|
92 | - $quotaFree = max($this->quota - $used, 0); |
|
93 | - // if free space is known |
|
94 | - if ($free >= 0) { |
|
95 | - $free = min($free, $quotaFree); |
|
96 | - } else { |
|
97 | - $free = $quotaFree; |
|
98 | - } |
|
99 | - return $free; |
|
100 | - } |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * see http://php.net/manual/en/function.file_put_contents.php |
|
106 | - * |
|
107 | - * @param string $path |
|
108 | - * @param string $data |
|
109 | - * @return bool |
|
110 | - */ |
|
111 | - public function file_put_contents($path, $data) { |
|
112 | - $free = $this->free_space(''); |
|
113 | - if ($free < 0 or strlen($data) < $free) { |
|
114 | - return $this->storage->file_put_contents($path, $data); |
|
115 | - } else { |
|
116 | - return false; |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * see http://php.net/manual/en/function.copy.php |
|
122 | - * |
|
123 | - * @param string $source |
|
124 | - * @param string $target |
|
125 | - * @return bool |
|
126 | - */ |
|
127 | - public function copy($source, $target) { |
|
128 | - $free = $this->free_space(''); |
|
129 | - if ($free < 0 or $this->getSize($source) < $free) { |
|
130 | - return $this->storage->copy($source, $target); |
|
131 | - } else { |
|
132 | - return false; |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * see http://php.net/manual/en/function.fopen.php |
|
138 | - * |
|
139 | - * @param string $path |
|
140 | - * @param string $mode |
|
141 | - * @return resource |
|
142 | - */ |
|
143 | - public function fopen($path, $mode) { |
|
144 | - $source = $this->storage->fopen($path, $mode); |
|
145 | - |
|
146 | - // don't apply quota for part files |
|
147 | - if (!$this->isPartFile($path)) { |
|
148 | - $free = $this->free_space(''); |
|
149 | - if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
150 | - // only apply quota for files, not metadata, trash or others |
|
151 | - if (strpos(ltrim($path, '/'), 'files/') === 0) { |
|
152 | - return \OC\Files\Stream\Quota::wrap($source, $free); |
|
153 | - } |
|
154 | - } |
|
155 | - } |
|
156 | - return $source; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Checks whether the given path is a part file |
|
161 | - * |
|
162 | - * @param string $path Path that may identify a .part file |
|
163 | - * @return string File path without .part extension |
|
164 | - * @note this is needed for reusing keys |
|
165 | - */ |
|
166 | - private function isPartFile($path) { |
|
167 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
168 | - |
|
169 | - return ($extension === 'part'); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @param \OCP\Files\Storage $sourceStorage |
|
174 | - * @param string $sourceInternalPath |
|
175 | - * @param string $targetInternalPath |
|
176 | - * @return bool |
|
177 | - */ |
|
178 | - public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
179 | - $free = $this->free_space(''); |
|
180 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
181 | - return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
182 | - } else { |
|
183 | - return false; |
|
184 | - } |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * @param \OCP\Files\Storage $sourceStorage |
|
189 | - * @param string $sourceInternalPath |
|
190 | - * @param string $targetInternalPath |
|
191 | - * @return bool |
|
192 | - */ |
|
193 | - public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
194 | - $free = $this->free_space(''); |
|
195 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
196 | - return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
197 | - } else { |
|
198 | - return false; |
|
199 | - } |
|
200 | - } |
|
33 | + /** |
|
34 | + * @var int $quota |
|
35 | + */ |
|
36 | + protected $quota; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var string $sizeRoot |
|
40 | + */ |
|
41 | + protected $sizeRoot; |
|
42 | + |
|
43 | + /** |
|
44 | + * @param array $parameters |
|
45 | + */ |
|
46 | + public function __construct($parameters) { |
|
47 | + $this->storage = $parameters['storage']; |
|
48 | + $this->quota = $parameters['quota']; |
|
49 | + $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return int quota value |
|
54 | + */ |
|
55 | + public function getQuota() { |
|
56 | + return $this->quota; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param string $path |
|
61 | + * @param \OC\Files\Storage\Storage $storage |
|
62 | + */ |
|
63 | + protected function getSize($path, $storage = null) { |
|
64 | + if (is_null($storage)) { |
|
65 | + $cache = $this->getCache(); |
|
66 | + } else { |
|
67 | + $cache = $storage->getCache(); |
|
68 | + } |
|
69 | + $data = $cache->get($path); |
|
70 | + if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
71 | + return $data['size']; |
|
72 | + } else { |
|
73 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Get free space as limited by the quota |
|
79 | + * |
|
80 | + * @param string $path |
|
81 | + * @return int |
|
82 | + */ |
|
83 | + public function free_space($path) { |
|
84 | + if ($this->quota < 0) { |
|
85 | + return $this->storage->free_space($path); |
|
86 | + } else { |
|
87 | + $used = $this->getSize($this->sizeRoot); |
|
88 | + if ($used < 0) { |
|
89 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
90 | + } else { |
|
91 | + $free = $this->storage->free_space($path); |
|
92 | + $quotaFree = max($this->quota - $used, 0); |
|
93 | + // if free space is known |
|
94 | + if ($free >= 0) { |
|
95 | + $free = min($free, $quotaFree); |
|
96 | + } else { |
|
97 | + $free = $quotaFree; |
|
98 | + } |
|
99 | + return $free; |
|
100 | + } |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * see http://php.net/manual/en/function.file_put_contents.php |
|
106 | + * |
|
107 | + * @param string $path |
|
108 | + * @param string $data |
|
109 | + * @return bool |
|
110 | + */ |
|
111 | + public function file_put_contents($path, $data) { |
|
112 | + $free = $this->free_space(''); |
|
113 | + if ($free < 0 or strlen($data) < $free) { |
|
114 | + return $this->storage->file_put_contents($path, $data); |
|
115 | + } else { |
|
116 | + return false; |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * see http://php.net/manual/en/function.copy.php |
|
122 | + * |
|
123 | + * @param string $source |
|
124 | + * @param string $target |
|
125 | + * @return bool |
|
126 | + */ |
|
127 | + public function copy($source, $target) { |
|
128 | + $free = $this->free_space(''); |
|
129 | + if ($free < 0 or $this->getSize($source) < $free) { |
|
130 | + return $this->storage->copy($source, $target); |
|
131 | + } else { |
|
132 | + return false; |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * see http://php.net/manual/en/function.fopen.php |
|
138 | + * |
|
139 | + * @param string $path |
|
140 | + * @param string $mode |
|
141 | + * @return resource |
|
142 | + */ |
|
143 | + public function fopen($path, $mode) { |
|
144 | + $source = $this->storage->fopen($path, $mode); |
|
145 | + |
|
146 | + // don't apply quota for part files |
|
147 | + if (!$this->isPartFile($path)) { |
|
148 | + $free = $this->free_space(''); |
|
149 | + if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
150 | + // only apply quota for files, not metadata, trash or others |
|
151 | + if (strpos(ltrim($path, '/'), 'files/') === 0) { |
|
152 | + return \OC\Files\Stream\Quota::wrap($source, $free); |
|
153 | + } |
|
154 | + } |
|
155 | + } |
|
156 | + return $source; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Checks whether the given path is a part file |
|
161 | + * |
|
162 | + * @param string $path Path that may identify a .part file |
|
163 | + * @return string File path without .part extension |
|
164 | + * @note this is needed for reusing keys |
|
165 | + */ |
|
166 | + private function isPartFile($path) { |
|
167 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
168 | + |
|
169 | + return ($extension === 'part'); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @param \OCP\Files\Storage $sourceStorage |
|
174 | + * @param string $sourceInternalPath |
|
175 | + * @param string $targetInternalPath |
|
176 | + * @return bool |
|
177 | + */ |
|
178 | + public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
179 | + $free = $this->free_space(''); |
|
180 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
181 | + return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
182 | + } else { |
|
183 | + return false; |
|
184 | + } |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * @param \OCP\Files\Storage $sourceStorage |
|
189 | + * @param string $sourceInternalPath |
|
190 | + * @param string $targetInternalPath |
|
191 | + * @return bool |
|
192 | + */ |
|
193 | + public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
194 | + $free = $this->free_space(''); |
|
195 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
196 | + return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
197 | + } else { |
|
198 | + return false; |
|
199 | + } |
|
200 | + } |
|
201 | 201 | } |
@@ -23,7 +23,6 @@ |
||
23 | 23 | |
24 | 24 | use OCP\Files\IMimeTypeLoader; |
25 | 25 | use OCP\IDBConnection; |
26 | - |
|
27 | 26 | use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
28 | 27 | |
29 | 28 | /** |
@@ -34,141 +34,141 @@ |
||
34 | 34 | */ |
35 | 35 | class Loader implements IMimeTypeLoader { |
36 | 36 | |
37 | - /** @var IDBConnection */ |
|
38 | - private $dbConnection; |
|
39 | - |
|
40 | - /** @var array [id => mimetype] */ |
|
41 | - protected $mimetypes; |
|
42 | - |
|
43 | - /** @var array [mimetype => id] */ |
|
44 | - protected $mimetypeIds; |
|
45 | - |
|
46 | - /** |
|
47 | - * @param IDBConnection $dbConnection |
|
48 | - */ |
|
49 | - public function __construct(IDBConnection $dbConnection) { |
|
50 | - $this->dbConnection = $dbConnection; |
|
51 | - $this->mimetypes = []; |
|
52 | - $this->mimetypeIds = []; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Get a mimetype from its ID |
|
57 | - * |
|
58 | - * @param int $id |
|
59 | - * @return string|null |
|
60 | - */ |
|
61 | - public function getMimetypeById($id) { |
|
62 | - if (!$this->mimetypes) { |
|
63 | - $this->loadMimetypes(); |
|
64 | - } |
|
65 | - if (isset($this->mimetypes[$id])) { |
|
66 | - return $this->mimetypes[$id]; |
|
67 | - } |
|
68 | - return null; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Get a mimetype ID, adding the mimetype to the DB if it does not exist |
|
73 | - * |
|
74 | - * @param string $mimetype |
|
75 | - * @return int |
|
76 | - */ |
|
77 | - public function getId($mimetype) { |
|
78 | - if (!$this->mimetypeIds) { |
|
79 | - $this->loadMimetypes(); |
|
80 | - } |
|
81 | - if (isset($this->mimetypeIds[$mimetype])) { |
|
82 | - return $this->mimetypeIds[$mimetype]; |
|
83 | - } |
|
84 | - return $this->store($mimetype); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Test if a mimetype exists in the database |
|
89 | - * |
|
90 | - * @param string $mimetype |
|
91 | - * @return bool |
|
92 | - */ |
|
93 | - public function exists($mimetype) { |
|
94 | - if (!$this->mimetypeIds) { |
|
95 | - $this->loadMimetypes(); |
|
96 | - } |
|
97 | - return isset($this->mimetypeIds[$mimetype]); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Clear all loaded mimetypes, allow for re-loading |
|
102 | - */ |
|
103 | - public function reset() { |
|
104 | - $this->mimetypes = []; |
|
105 | - $this->mimetypeIds = []; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Store a mimetype in the DB |
|
110 | - * |
|
111 | - * @param string $mimetype |
|
112 | - * @param int inserted ID |
|
113 | - */ |
|
114 | - protected function store($mimetype) { |
|
115 | - try { |
|
116 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
117 | - $qb->insert('mimetypes') |
|
118 | - ->values([ |
|
119 | - 'mimetype' => $qb->createNamedParameter($mimetype) |
|
120 | - ]); |
|
121 | - $qb->execute(); |
|
122 | - } catch (UniqueConstraintViolationException $e) { |
|
123 | - // something inserted it before us |
|
124 | - } |
|
125 | - |
|
126 | - $fetch = $this->dbConnection->getQueryBuilder(); |
|
127 | - $fetch->select('id') |
|
128 | - ->from('mimetypes') |
|
129 | - ->where( |
|
130 | - $fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype) |
|
131 | - )); |
|
132 | - $row = $fetch->execute()->fetch(); |
|
133 | - |
|
134 | - $this->mimetypes[$row['id']] = $mimetype; |
|
135 | - $this->mimetypeIds[$mimetype] = $row['id']; |
|
136 | - return $row['id']; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Load all mimetypes from DB |
|
141 | - */ |
|
142 | - private function loadMimetypes() { |
|
143 | - $qb = $this->dbConnection->getQueryBuilder(); |
|
144 | - $qb->select('id', 'mimetype') |
|
145 | - ->from('mimetypes'); |
|
146 | - $results = $qb->execute()->fetchAll(); |
|
147 | - |
|
148 | - foreach ($results as $row) { |
|
149 | - $this->mimetypes[$row['id']] = $row['mimetype']; |
|
150 | - $this->mimetypeIds[$row['mimetype']] = $row['id']; |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Update filecache mimetype based on file extension |
|
156 | - * |
|
157 | - * @param string $ext file extension |
|
158 | - * @param int $mimetypeId |
|
159 | - * @return int number of changed rows |
|
160 | - */ |
|
161 | - public function updateFilecache($ext, $mimetypeId) { |
|
162 | - $update = $this->dbConnection->getQueryBuilder(); |
|
163 | - $update->update('filecache') |
|
164 | - ->set('mimetype', $update->createNamedParameter($mimetypeId)) |
|
165 | - ->where($update->expr()->neq( |
|
166 | - 'mimetype', $update->createNamedParameter($mimetypeId) |
|
167 | - )) |
|
168 | - ->andWhere($update->expr()->like( |
|
169 | - $update->createFunction('LOWER(`name`)'), $update->createNamedParameter($ext) |
|
170 | - )); |
|
171 | - return $update->execute(); |
|
172 | - } |
|
37 | + /** @var IDBConnection */ |
|
38 | + private $dbConnection; |
|
39 | + |
|
40 | + /** @var array [id => mimetype] */ |
|
41 | + protected $mimetypes; |
|
42 | + |
|
43 | + /** @var array [mimetype => id] */ |
|
44 | + protected $mimetypeIds; |
|
45 | + |
|
46 | + /** |
|
47 | + * @param IDBConnection $dbConnection |
|
48 | + */ |
|
49 | + public function __construct(IDBConnection $dbConnection) { |
|
50 | + $this->dbConnection = $dbConnection; |
|
51 | + $this->mimetypes = []; |
|
52 | + $this->mimetypeIds = []; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Get a mimetype from its ID |
|
57 | + * |
|
58 | + * @param int $id |
|
59 | + * @return string|null |
|
60 | + */ |
|
61 | + public function getMimetypeById($id) { |
|
62 | + if (!$this->mimetypes) { |
|
63 | + $this->loadMimetypes(); |
|
64 | + } |
|
65 | + if (isset($this->mimetypes[$id])) { |
|
66 | + return $this->mimetypes[$id]; |
|
67 | + } |
|
68 | + return null; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Get a mimetype ID, adding the mimetype to the DB if it does not exist |
|
73 | + * |
|
74 | + * @param string $mimetype |
|
75 | + * @return int |
|
76 | + */ |
|
77 | + public function getId($mimetype) { |
|
78 | + if (!$this->mimetypeIds) { |
|
79 | + $this->loadMimetypes(); |
|
80 | + } |
|
81 | + if (isset($this->mimetypeIds[$mimetype])) { |
|
82 | + return $this->mimetypeIds[$mimetype]; |
|
83 | + } |
|
84 | + return $this->store($mimetype); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Test if a mimetype exists in the database |
|
89 | + * |
|
90 | + * @param string $mimetype |
|
91 | + * @return bool |
|
92 | + */ |
|
93 | + public function exists($mimetype) { |
|
94 | + if (!$this->mimetypeIds) { |
|
95 | + $this->loadMimetypes(); |
|
96 | + } |
|
97 | + return isset($this->mimetypeIds[$mimetype]); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Clear all loaded mimetypes, allow for re-loading |
|
102 | + */ |
|
103 | + public function reset() { |
|
104 | + $this->mimetypes = []; |
|
105 | + $this->mimetypeIds = []; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Store a mimetype in the DB |
|
110 | + * |
|
111 | + * @param string $mimetype |
|
112 | + * @param int inserted ID |
|
113 | + */ |
|
114 | + protected function store($mimetype) { |
|
115 | + try { |
|
116 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
117 | + $qb->insert('mimetypes') |
|
118 | + ->values([ |
|
119 | + 'mimetype' => $qb->createNamedParameter($mimetype) |
|
120 | + ]); |
|
121 | + $qb->execute(); |
|
122 | + } catch (UniqueConstraintViolationException $e) { |
|
123 | + // something inserted it before us |
|
124 | + } |
|
125 | + |
|
126 | + $fetch = $this->dbConnection->getQueryBuilder(); |
|
127 | + $fetch->select('id') |
|
128 | + ->from('mimetypes') |
|
129 | + ->where( |
|
130 | + $fetch->expr()->eq('mimetype', $fetch->createNamedParameter($mimetype) |
|
131 | + )); |
|
132 | + $row = $fetch->execute()->fetch(); |
|
133 | + |
|
134 | + $this->mimetypes[$row['id']] = $mimetype; |
|
135 | + $this->mimetypeIds[$mimetype] = $row['id']; |
|
136 | + return $row['id']; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Load all mimetypes from DB |
|
141 | + */ |
|
142 | + private function loadMimetypes() { |
|
143 | + $qb = $this->dbConnection->getQueryBuilder(); |
|
144 | + $qb->select('id', 'mimetype') |
|
145 | + ->from('mimetypes'); |
|
146 | + $results = $qb->execute()->fetchAll(); |
|
147 | + |
|
148 | + foreach ($results as $row) { |
|
149 | + $this->mimetypes[$row['id']] = $row['mimetype']; |
|
150 | + $this->mimetypeIds[$row['mimetype']] = $row['id']; |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Update filecache mimetype based on file extension |
|
156 | + * |
|
157 | + * @param string $ext file extension |
|
158 | + * @param int $mimetypeId |
|
159 | + * @return int number of changed rows |
|
160 | + */ |
|
161 | + public function updateFilecache($ext, $mimetypeId) { |
|
162 | + $update = $this->dbConnection->getQueryBuilder(); |
|
163 | + $update->update('filecache') |
|
164 | + ->set('mimetype', $update->createNamedParameter($mimetypeId)) |
|
165 | + ->where($update->expr()->neq( |
|
166 | + 'mimetype', $update->createNamedParameter($mimetypeId) |
|
167 | + )) |
|
168 | + ->andWhere($update->expr()->like( |
|
169 | + $update->createFunction('LOWER(`name`)'), $update->createNamedParameter($ext) |
|
170 | + )); |
|
171 | + return $update->execute(); |
|
172 | + } |
|
173 | 173 | |
174 | 174 | } |
@@ -176,7 +176,7 @@ |
||
176 | 176 | * Returns an associative array with all translations |
177 | 177 | * |
178 | 178 | * Called by \OC_L10N_String |
179 | - * @return array |
|
179 | + * @return string[] |
|
180 | 180 | */ |
181 | 181 | public function getTranslations() { |
182 | 182 | return $this->translations; |
@@ -28,190 +28,190 @@ |
||
28 | 28 | |
29 | 29 | class L10N implements IL10N { |
30 | 30 | |
31 | - /** @var IFactory */ |
|
32 | - protected $factory; |
|
33 | - |
|
34 | - /** @var string App of this object */ |
|
35 | - protected $app; |
|
36 | - |
|
37 | - /** @var string Language of this object */ |
|
38 | - protected $lang; |
|
39 | - |
|
40 | - /** @var string Plural forms (string) */ |
|
41 | - private $pluralFormString = 'nplurals=2; plural=(n != 1);'; |
|
42 | - |
|
43 | - /** @var string Plural forms (function) */ |
|
44 | - private $pluralFormFunction = null; |
|
45 | - |
|
46 | - /** @var string[] */ |
|
47 | - private $translations = []; |
|
48 | - |
|
49 | - /** |
|
50 | - * @param IFactory $factory |
|
51 | - * @param string $app |
|
52 | - * @param string $lang |
|
53 | - * @param array $files |
|
54 | - */ |
|
55 | - public function __construct(IFactory $factory, $app, $lang, array $files) { |
|
56 | - $this->factory = $factory; |
|
57 | - $this->app = $app; |
|
58 | - $this->lang = $lang; |
|
59 | - |
|
60 | - $this->translations = []; |
|
61 | - foreach ($files as $languageFile) { |
|
62 | - $this->load($languageFile); |
|
63 | - } |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * The code (en, de, ...) of the language that is used for this instance |
|
68 | - * |
|
69 | - * @return string language |
|
70 | - */ |
|
71 | - public function getLanguageCode() { |
|
72 | - return $this->lang; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Translating |
|
77 | - * @param string $text The text we need a translation for |
|
78 | - * @param array $parameters default:array() Parameters for sprintf |
|
79 | - * @return string Translation or the same text |
|
80 | - * |
|
81 | - * Returns the translation. If no translation is found, $text will be |
|
82 | - * returned. |
|
83 | - */ |
|
84 | - public function t($text, $parameters = array()) { |
|
85 | - return (string) new \OC_L10N_String($this, $text, $parameters); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Translating |
|
90 | - * @param string $text_singular the string to translate for exactly one object |
|
91 | - * @param string $text_plural the string to translate for n objects |
|
92 | - * @param integer $count Number of objects |
|
93 | - * @param array $parameters default:array() Parameters for sprintf |
|
94 | - * @return string Translation or the same text |
|
95 | - * |
|
96 | - * Returns the translation. If no translation is found, $text will be |
|
97 | - * returned. %n will be replaced with the number of objects. |
|
98 | - * |
|
99 | - * The correct plural is determined by the plural_forms-function |
|
100 | - * provided by the po file. |
|
101 | - * |
|
102 | - */ |
|
103 | - public function n($text_singular, $text_plural, $count, $parameters = array()) { |
|
104 | - $identifier = "_${text_singular}_::_${text_plural}_"; |
|
105 | - if (isset($this->translations[$identifier])) { |
|
106 | - return (string) new \OC_L10N_String($this, $identifier, $parameters, $count); |
|
107 | - } else { |
|
108 | - if ($count === 1) { |
|
109 | - return (string) new \OC_L10N_String($this, $text_singular, $parameters, $count); |
|
110 | - } else { |
|
111 | - return (string) new \OC_L10N_String($this, $text_plural, $parameters, $count); |
|
112 | - } |
|
113 | - } |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Localization |
|
118 | - * @param string $type Type of localization |
|
119 | - * @param \DateTime|int|string $data parameters for this localization |
|
120 | - * @param array $options |
|
121 | - * @return string|int|false |
|
122 | - * |
|
123 | - * Returns the localized data. |
|
124 | - * |
|
125 | - * Implemented types: |
|
126 | - * - date |
|
127 | - * - Creates a date |
|
128 | - * - params: timestamp (int/string) |
|
129 | - * - datetime |
|
130 | - * - Creates date and time |
|
131 | - * - params: timestamp (int/string) |
|
132 | - * - time |
|
133 | - * - Creates a time |
|
134 | - * - params: timestamp (int/string) |
|
135 | - * - firstday: Returns the first day of the week (0 sunday - 6 saturday) |
|
136 | - * - jsdate: Returns the short JS date format |
|
137 | - */ |
|
138 | - public function l($type, $data = null, $options = array()) { |
|
139 | - // Use the language of the instance |
|
140 | - $locale = $this->getLanguageCode(); |
|
141 | - if ($locale === 'sr@latin') { |
|
142 | - $locale = 'sr_latn'; |
|
143 | - } |
|
144 | - |
|
145 | - if ($type === 'firstday') { |
|
146 | - return (int) Calendar::getFirstWeekday($locale); |
|
147 | - } |
|
148 | - if ($type === 'jsdate') { |
|
149 | - return (string) Calendar::getDateFormat('short', $locale); |
|
150 | - } |
|
151 | - |
|
152 | - $value = new \DateTime(); |
|
153 | - if ($data instanceof \DateTime) { |
|
154 | - $value = $data; |
|
155 | - } else if (is_string($data) && !is_numeric($data)) { |
|
156 | - $data = strtotime($data); |
|
157 | - $value->setTimestamp($data); |
|
158 | - } else if ($data !== null) { |
|
159 | - $value->setTimestamp($data); |
|
160 | - } |
|
161 | - |
|
162 | - $options = array_merge(array('width' => 'long'), $options); |
|
163 | - $width = $options['width']; |
|
164 | - switch ($type) { |
|
165 | - case 'date': |
|
166 | - return (string) Calendar::formatDate($value, $width, $locale); |
|
167 | - case 'datetime': |
|
168 | - return (string) Calendar::formatDatetime($value, $width, $locale); |
|
169 | - case 'time': |
|
170 | - return (string) Calendar::formatTime($value, $width, $locale); |
|
171 | - default: |
|
172 | - return false; |
|
173 | - } |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Returns an associative array with all translations |
|
178 | - * |
|
179 | - * Called by \OC_L10N_String |
|
180 | - * @return array |
|
181 | - */ |
|
182 | - public function getTranslations() { |
|
183 | - return $this->translations; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Returnsed function accepts the argument $n |
|
188 | - * |
|
189 | - * Called by \OC_L10N_String |
|
190 | - * @return string the plural form function |
|
191 | - */ |
|
192 | - public function getPluralFormFunction() { |
|
193 | - if (is_null($this->pluralFormFunction)) { |
|
194 | - $this->pluralFormFunction = $this->factory->createPluralFunction($this->pluralFormString); |
|
195 | - } |
|
196 | - return $this->pluralFormFunction; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * @param $translationFile |
|
201 | - * @return bool |
|
202 | - */ |
|
203 | - protected function load($translationFile) { |
|
204 | - $json = json_decode(file_get_contents($translationFile), true); |
|
205 | - if (!is_array($json)) { |
|
206 | - $jsonError = json_last_error(); |
|
207 | - \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']); |
|
208 | - return false; |
|
209 | - } |
|
210 | - |
|
211 | - if (!empty($json['pluralForm'])) { |
|
212 | - $this->pluralFormString = $json['pluralForm']; |
|
213 | - } |
|
214 | - $this->translations = array_merge($this->translations, $json['translations']); |
|
215 | - return true; |
|
216 | - } |
|
31 | + /** @var IFactory */ |
|
32 | + protected $factory; |
|
33 | + |
|
34 | + /** @var string App of this object */ |
|
35 | + protected $app; |
|
36 | + |
|
37 | + /** @var string Language of this object */ |
|
38 | + protected $lang; |
|
39 | + |
|
40 | + /** @var string Plural forms (string) */ |
|
41 | + private $pluralFormString = 'nplurals=2; plural=(n != 1);'; |
|
42 | + |
|
43 | + /** @var string Plural forms (function) */ |
|
44 | + private $pluralFormFunction = null; |
|
45 | + |
|
46 | + /** @var string[] */ |
|
47 | + private $translations = []; |
|
48 | + |
|
49 | + /** |
|
50 | + * @param IFactory $factory |
|
51 | + * @param string $app |
|
52 | + * @param string $lang |
|
53 | + * @param array $files |
|
54 | + */ |
|
55 | + public function __construct(IFactory $factory, $app, $lang, array $files) { |
|
56 | + $this->factory = $factory; |
|
57 | + $this->app = $app; |
|
58 | + $this->lang = $lang; |
|
59 | + |
|
60 | + $this->translations = []; |
|
61 | + foreach ($files as $languageFile) { |
|
62 | + $this->load($languageFile); |
|
63 | + } |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * The code (en, de, ...) of the language that is used for this instance |
|
68 | + * |
|
69 | + * @return string language |
|
70 | + */ |
|
71 | + public function getLanguageCode() { |
|
72 | + return $this->lang; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Translating |
|
77 | + * @param string $text The text we need a translation for |
|
78 | + * @param array $parameters default:array() Parameters for sprintf |
|
79 | + * @return string Translation or the same text |
|
80 | + * |
|
81 | + * Returns the translation. If no translation is found, $text will be |
|
82 | + * returned. |
|
83 | + */ |
|
84 | + public function t($text, $parameters = array()) { |
|
85 | + return (string) new \OC_L10N_String($this, $text, $parameters); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Translating |
|
90 | + * @param string $text_singular the string to translate for exactly one object |
|
91 | + * @param string $text_plural the string to translate for n objects |
|
92 | + * @param integer $count Number of objects |
|
93 | + * @param array $parameters default:array() Parameters for sprintf |
|
94 | + * @return string Translation or the same text |
|
95 | + * |
|
96 | + * Returns the translation. If no translation is found, $text will be |
|
97 | + * returned. %n will be replaced with the number of objects. |
|
98 | + * |
|
99 | + * The correct plural is determined by the plural_forms-function |
|
100 | + * provided by the po file. |
|
101 | + * |
|
102 | + */ |
|
103 | + public function n($text_singular, $text_plural, $count, $parameters = array()) { |
|
104 | + $identifier = "_${text_singular}_::_${text_plural}_"; |
|
105 | + if (isset($this->translations[$identifier])) { |
|
106 | + return (string) new \OC_L10N_String($this, $identifier, $parameters, $count); |
|
107 | + } else { |
|
108 | + if ($count === 1) { |
|
109 | + return (string) new \OC_L10N_String($this, $text_singular, $parameters, $count); |
|
110 | + } else { |
|
111 | + return (string) new \OC_L10N_String($this, $text_plural, $parameters, $count); |
|
112 | + } |
|
113 | + } |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Localization |
|
118 | + * @param string $type Type of localization |
|
119 | + * @param \DateTime|int|string $data parameters for this localization |
|
120 | + * @param array $options |
|
121 | + * @return string|int|false |
|
122 | + * |
|
123 | + * Returns the localized data. |
|
124 | + * |
|
125 | + * Implemented types: |
|
126 | + * - date |
|
127 | + * - Creates a date |
|
128 | + * - params: timestamp (int/string) |
|
129 | + * - datetime |
|
130 | + * - Creates date and time |
|
131 | + * - params: timestamp (int/string) |
|
132 | + * - time |
|
133 | + * - Creates a time |
|
134 | + * - params: timestamp (int/string) |
|
135 | + * - firstday: Returns the first day of the week (0 sunday - 6 saturday) |
|
136 | + * - jsdate: Returns the short JS date format |
|
137 | + */ |
|
138 | + public function l($type, $data = null, $options = array()) { |
|
139 | + // Use the language of the instance |
|
140 | + $locale = $this->getLanguageCode(); |
|
141 | + if ($locale === 'sr@latin') { |
|
142 | + $locale = 'sr_latn'; |
|
143 | + } |
|
144 | + |
|
145 | + if ($type === 'firstday') { |
|
146 | + return (int) Calendar::getFirstWeekday($locale); |
|
147 | + } |
|
148 | + if ($type === 'jsdate') { |
|
149 | + return (string) Calendar::getDateFormat('short', $locale); |
|
150 | + } |
|
151 | + |
|
152 | + $value = new \DateTime(); |
|
153 | + if ($data instanceof \DateTime) { |
|
154 | + $value = $data; |
|
155 | + } else if (is_string($data) && !is_numeric($data)) { |
|
156 | + $data = strtotime($data); |
|
157 | + $value->setTimestamp($data); |
|
158 | + } else if ($data !== null) { |
|
159 | + $value->setTimestamp($data); |
|
160 | + } |
|
161 | + |
|
162 | + $options = array_merge(array('width' => 'long'), $options); |
|
163 | + $width = $options['width']; |
|
164 | + switch ($type) { |
|
165 | + case 'date': |
|
166 | + return (string) Calendar::formatDate($value, $width, $locale); |
|
167 | + case 'datetime': |
|
168 | + return (string) Calendar::formatDatetime($value, $width, $locale); |
|
169 | + case 'time': |
|
170 | + return (string) Calendar::formatTime($value, $width, $locale); |
|
171 | + default: |
|
172 | + return false; |
|
173 | + } |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Returns an associative array with all translations |
|
178 | + * |
|
179 | + * Called by \OC_L10N_String |
|
180 | + * @return array |
|
181 | + */ |
|
182 | + public function getTranslations() { |
|
183 | + return $this->translations; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Returnsed function accepts the argument $n |
|
188 | + * |
|
189 | + * Called by \OC_L10N_String |
|
190 | + * @return string the plural form function |
|
191 | + */ |
|
192 | + public function getPluralFormFunction() { |
|
193 | + if (is_null($this->pluralFormFunction)) { |
|
194 | + $this->pluralFormFunction = $this->factory->createPluralFunction($this->pluralFormString); |
|
195 | + } |
|
196 | + return $this->pluralFormFunction; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * @param $translationFile |
|
201 | + * @return bool |
|
202 | + */ |
|
203 | + protected function load($translationFile) { |
|
204 | + $json = json_decode(file_get_contents($translationFile), true); |
|
205 | + if (!is_array($json)) { |
|
206 | + $jsonError = json_last_error(); |
|
207 | + \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']); |
|
208 | + return false; |
|
209 | + } |
|
210 | + |
|
211 | + if (!empty($json['pluralForm'])) { |
|
212 | + $this->pluralFormString = $json['pluralForm']; |
|
213 | + } |
|
214 | + $this->translations = array_merge($this->translations, $json['translations']); |
|
215 | + return true; |
|
216 | + } |
|
217 | 217 | } |
@@ -331,7 +331,7 @@ |
||
331 | 331 | |
332 | 332 | /** |
333 | 333 | * http basic auth |
334 | - * @return string|false (username, or false on failure) |
|
334 | + * @return string (username, or false on failure) |
|
335 | 335 | */ |
336 | 336 | private static function loginUser() { |
337 | 337 | if(self::$isLoggedIn === true) { |
@@ -37,467 +37,467 @@ |
||
37 | 37 | |
38 | 38 | class OC_API { |
39 | 39 | |
40 | - /** |
|
41 | - * API authentication levels |
|
42 | - */ |
|
43 | - |
|
44 | - /** @deprecated Use \OCP\API::GUEST_AUTH instead */ |
|
45 | - const GUEST_AUTH = 0; |
|
46 | - |
|
47 | - /** @deprecated Use \OCP\API::USER_AUTH instead */ |
|
48 | - const USER_AUTH = 1; |
|
49 | - |
|
50 | - /** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */ |
|
51 | - const SUBADMIN_AUTH = 2; |
|
52 | - |
|
53 | - /** @deprecated Use \OCP\API::ADMIN_AUTH instead */ |
|
54 | - const ADMIN_AUTH = 3; |
|
55 | - |
|
56 | - /** |
|
57 | - * API Response Codes |
|
58 | - */ |
|
59 | - |
|
60 | - /** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */ |
|
61 | - const RESPOND_UNAUTHORISED = 997; |
|
62 | - |
|
63 | - /** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */ |
|
64 | - const RESPOND_SERVER_ERROR = 996; |
|
65 | - |
|
66 | - /** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */ |
|
67 | - const RESPOND_NOT_FOUND = 998; |
|
68 | - |
|
69 | - /** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */ |
|
70 | - const RESPOND_UNKNOWN_ERROR = 999; |
|
71 | - |
|
72 | - /** |
|
73 | - * api actions |
|
74 | - */ |
|
75 | - protected static $actions = array(); |
|
76 | - private static $logoutRequired = false; |
|
77 | - private static $isLoggedIn = false; |
|
78 | - |
|
79 | - /** |
|
80 | - * registers an api call |
|
81 | - * @param string $method the http method |
|
82 | - * @param string $url the url to match |
|
83 | - * @param callable $action the function to run |
|
84 | - * @param string $app the id of the app registering the call |
|
85 | - * @param int $authLevel the level of authentication required for the call |
|
86 | - * @param array $defaults |
|
87 | - * @param array $requirements |
|
88 | - */ |
|
89 | - public static function register($method, $url, $action, $app, |
|
90 | - $authLevel = API::USER_AUTH, |
|
91 | - $defaults = array(), |
|
92 | - $requirements = array()) { |
|
93 | - $name = strtolower($method).$url; |
|
94 | - $name = str_replace(array('/', '{', '}'), '_', $name); |
|
95 | - if(!isset(self::$actions[$name])) { |
|
96 | - $oldCollection = OC::$server->getRouter()->getCurrentCollection(); |
|
97 | - OC::$server->getRouter()->useCollection('ocs'); |
|
98 | - OC::$server->getRouter()->create($name, $url) |
|
99 | - ->method($method) |
|
100 | - ->defaults($defaults) |
|
101 | - ->requirements($requirements) |
|
102 | - ->action('OC_API', 'call'); |
|
103 | - self::$actions[$name] = array(); |
|
104 | - OC::$server->getRouter()->useCollection($oldCollection); |
|
105 | - } |
|
106 | - self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * handles an api call |
|
111 | - * @param array $parameters |
|
112 | - */ |
|
113 | - public static function call($parameters) { |
|
114 | - $request = \OC::$server->getRequest(); |
|
115 | - $method = $request->getMethod(); |
|
116 | - |
|
117 | - // Prepare the request variables |
|
118 | - if($method === 'PUT') { |
|
119 | - $parameters['_put'] = $request->getParams(); |
|
120 | - } else if($method === 'DELETE') { |
|
121 | - $parameters['_delete'] = $request->getParams(); |
|
122 | - } |
|
123 | - $name = $parameters['_route']; |
|
124 | - // Foreach registered action |
|
125 | - $responses = array(); |
|
126 | - foreach(self::$actions[$name] as $action) { |
|
127 | - // Check authentication and availability |
|
128 | - if(!self::isAuthorised($action)) { |
|
129 | - $responses[] = array( |
|
130 | - 'app' => $action['app'], |
|
131 | - 'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'), |
|
132 | - 'shipped' => OC_App::isShipped($action['app']), |
|
133 | - ); |
|
134 | - continue; |
|
135 | - } |
|
136 | - if(!is_callable($action['action'])) { |
|
137 | - $responses[] = array( |
|
138 | - 'app' => $action['app'], |
|
139 | - 'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'), |
|
140 | - 'shipped' => OC_App::isShipped($action['app']), |
|
141 | - ); |
|
142 | - continue; |
|
143 | - } |
|
144 | - // Run the action |
|
145 | - $responses[] = array( |
|
146 | - 'app' => $action['app'], |
|
147 | - 'response' => call_user_func($action['action'], $parameters), |
|
148 | - 'shipped' => OC_App::isShipped($action['app']), |
|
149 | - ); |
|
150 | - } |
|
151 | - $response = self::mergeResponses($responses); |
|
152 | - $format = self::requestedFormat(); |
|
153 | - if (self::$logoutRequired) { |
|
154 | - \OC::$server->getUserSession()->logout(); |
|
155 | - } |
|
156 | - |
|
157 | - self::respond($response, $format); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * merge the returned result objects into one response |
|
162 | - * @param array $responses |
|
163 | - * @return OC_OCS_Result |
|
164 | - */ |
|
165 | - public static function mergeResponses($responses) { |
|
166 | - // Sort into shipped and third-party |
|
167 | - $shipped = array( |
|
168 | - 'succeeded' => array(), |
|
169 | - 'failed' => array(), |
|
170 | - ); |
|
171 | - $thirdparty = array( |
|
172 | - 'succeeded' => array(), |
|
173 | - 'failed' => array(), |
|
174 | - ); |
|
175 | - |
|
176 | - foreach($responses as $response) { |
|
177 | - if($response['shipped'] || ($response['app'] === 'core')) { |
|
178 | - if($response['response']->succeeded()) { |
|
179 | - $shipped['succeeded'][$response['app']] = $response; |
|
180 | - } else { |
|
181 | - $shipped['failed'][$response['app']] = $response; |
|
182 | - } |
|
183 | - } else { |
|
184 | - if($response['response']->succeeded()) { |
|
185 | - $thirdparty['succeeded'][$response['app']] = $response; |
|
186 | - } else { |
|
187 | - $thirdparty['failed'][$response['app']] = $response; |
|
188 | - } |
|
189 | - } |
|
190 | - } |
|
191 | - |
|
192 | - // Remove any error responses if there is one shipped response that succeeded |
|
193 | - if(!empty($shipped['failed'])) { |
|
194 | - // Which shipped response do we use if they all failed? |
|
195 | - // They may have failed for different reasons (different status codes) |
|
196 | - // Which response code should we return? |
|
197 | - // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR |
|
198 | - // Merge failed responses if more than one |
|
199 | - $data = array(); |
|
200 | - foreach($shipped['failed'] as $failure) { |
|
201 | - $data = array_merge_recursive($data, $failure['response']->getData()); |
|
202 | - } |
|
203 | - $picked = reset($shipped['failed']); |
|
204 | - $code = $picked['response']->getStatusCode(); |
|
205 | - $meta = $picked['response']->getMeta(); |
|
206 | - $headers = $picked['response']->getHeaders(); |
|
207 | - $response = new OC_OCS_Result($data, $code, $meta['message'], $headers); |
|
208 | - return $response; |
|
209 | - } elseif(!empty($shipped['succeeded'])) { |
|
210 | - $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']); |
|
211 | - } elseif(!empty($thirdparty['failed'])) { |
|
212 | - // Merge failed responses if more than one |
|
213 | - $data = array(); |
|
214 | - foreach($thirdparty['failed'] as $failure) { |
|
215 | - $data = array_merge_recursive($data, $failure['response']->getData()); |
|
216 | - } |
|
217 | - $picked = reset($thirdparty['failed']); |
|
218 | - $code = $picked['response']->getStatusCode(); |
|
219 | - $meta = $picked['response']->getMeta(); |
|
220 | - $headers = $picked['response']->getHeaders(); |
|
221 | - $response = new OC_OCS_Result($data, $code, $meta['message'], $headers); |
|
222 | - return $response; |
|
223 | - } else { |
|
224 | - $responses = $thirdparty['succeeded']; |
|
225 | - } |
|
226 | - // Merge the successful responses |
|
227 | - $data = []; |
|
228 | - $codes = []; |
|
229 | - $header = []; |
|
230 | - |
|
231 | - foreach($responses as $response) { |
|
232 | - if($response['shipped']) { |
|
233 | - $data = array_merge_recursive($response['response']->getData(), $data); |
|
234 | - } else { |
|
235 | - $data = array_merge_recursive($data, $response['response']->getData()); |
|
236 | - } |
|
237 | - $header = array_merge_recursive($header, $response['response']->getHeaders()); |
|
238 | - $codes[] = ['code' => $response['response']->getStatusCode(), |
|
239 | - 'meta' => $response['response']->getMeta()]; |
|
240 | - } |
|
241 | - |
|
242 | - // Use any non 100 status codes |
|
243 | - $statusCode = 100; |
|
244 | - $statusMessage = null; |
|
245 | - foreach($codes as $code) { |
|
246 | - if($code['code'] != 100) { |
|
247 | - $statusCode = $code['code']; |
|
248 | - $statusMessage = $code['meta']['message']; |
|
249 | - break; |
|
250 | - } |
|
251 | - } |
|
252 | - |
|
253 | - return new OC_OCS_Result($data, $statusCode, $statusMessage, $header); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * authenticate the api call |
|
258 | - * @param array $action the action details as supplied to OC_API::register() |
|
259 | - * @return bool |
|
260 | - */ |
|
261 | - private static function isAuthorised($action) { |
|
262 | - $level = $action['authlevel']; |
|
263 | - switch($level) { |
|
264 | - case API::GUEST_AUTH: |
|
265 | - // Anyone can access |
|
266 | - return true; |
|
267 | - case API::USER_AUTH: |
|
268 | - // User required |
|
269 | - return self::loginUser(); |
|
270 | - case API::SUBADMIN_AUTH: |
|
271 | - // Check for subadmin |
|
272 | - $user = self::loginUser(); |
|
273 | - if(!$user) { |
|
274 | - return false; |
|
275 | - } else { |
|
276 | - $userObject = \OC::$server->getUserSession()->getUser(); |
|
277 | - if($userObject === null) { |
|
278 | - return false; |
|
279 | - } |
|
280 | - $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
281 | - $admin = OC_User::isAdminUser($user); |
|
282 | - if($isSubAdmin || $admin) { |
|
283 | - return true; |
|
284 | - } else { |
|
285 | - return false; |
|
286 | - } |
|
287 | - } |
|
288 | - case API::ADMIN_AUTH: |
|
289 | - // Check for admin |
|
290 | - $user = self::loginUser(); |
|
291 | - if(!$user) { |
|
292 | - return false; |
|
293 | - } else { |
|
294 | - return OC_User::isAdminUser($user); |
|
295 | - } |
|
296 | - default: |
|
297 | - // oops looks like invalid level supplied |
|
298 | - return false; |
|
299 | - } |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * http basic auth |
|
304 | - * @return string|false (username, or false on failure) |
|
305 | - */ |
|
306 | - private static function loginUser() { |
|
307 | - if(self::$isLoggedIn === true) { |
|
308 | - return \OC_User::getUser(); |
|
309 | - } |
|
310 | - |
|
311 | - // reuse existing login |
|
312 | - $loggedIn = \OC::$server->getUserSession()->isLoggedIn(); |
|
313 | - if ($loggedIn === true) { |
|
314 | - if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
|
315 | - // Do not allow access to OCS until the 2FA challenge was solved successfully |
|
316 | - return false; |
|
317 | - } |
|
318 | - $ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false; |
|
319 | - if ($ocsApiRequest) { |
|
320 | - |
|
321 | - // initialize the user's filesystem |
|
322 | - \OC_Util::setupFS(\OC_User::getUser()); |
|
323 | - self::$isLoggedIn = true; |
|
324 | - |
|
325 | - return OC_User::getUser(); |
|
326 | - } |
|
327 | - return false; |
|
328 | - } |
|
329 | - |
|
330 | - // basic auth - because OC_User::login will create a new session we shall only try to login |
|
331 | - // if user and pass are set |
|
332 | - $userSession = \OC::$server->getUserSession(); |
|
333 | - $request = \OC::$server->getRequest(); |
|
334 | - try { |
|
335 | - $loginSuccess = $userSession->tryTokenLogin($request); |
|
336 | - if (!$loginSuccess) { |
|
337 | - $loginSuccess = $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler()); |
|
338 | - } |
|
339 | - } catch (\OC\User\LoginException $e) { |
|
340 | - return false; |
|
341 | - } |
|
40 | + /** |
|
41 | + * API authentication levels |
|
42 | + */ |
|
43 | + |
|
44 | + /** @deprecated Use \OCP\API::GUEST_AUTH instead */ |
|
45 | + const GUEST_AUTH = 0; |
|
46 | + |
|
47 | + /** @deprecated Use \OCP\API::USER_AUTH instead */ |
|
48 | + const USER_AUTH = 1; |
|
49 | + |
|
50 | + /** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */ |
|
51 | + const SUBADMIN_AUTH = 2; |
|
52 | + |
|
53 | + /** @deprecated Use \OCP\API::ADMIN_AUTH instead */ |
|
54 | + const ADMIN_AUTH = 3; |
|
55 | + |
|
56 | + /** |
|
57 | + * API Response Codes |
|
58 | + */ |
|
59 | + |
|
60 | + /** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */ |
|
61 | + const RESPOND_UNAUTHORISED = 997; |
|
62 | + |
|
63 | + /** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */ |
|
64 | + const RESPOND_SERVER_ERROR = 996; |
|
65 | + |
|
66 | + /** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */ |
|
67 | + const RESPOND_NOT_FOUND = 998; |
|
68 | + |
|
69 | + /** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */ |
|
70 | + const RESPOND_UNKNOWN_ERROR = 999; |
|
71 | + |
|
72 | + /** |
|
73 | + * api actions |
|
74 | + */ |
|
75 | + protected static $actions = array(); |
|
76 | + private static $logoutRequired = false; |
|
77 | + private static $isLoggedIn = false; |
|
78 | + |
|
79 | + /** |
|
80 | + * registers an api call |
|
81 | + * @param string $method the http method |
|
82 | + * @param string $url the url to match |
|
83 | + * @param callable $action the function to run |
|
84 | + * @param string $app the id of the app registering the call |
|
85 | + * @param int $authLevel the level of authentication required for the call |
|
86 | + * @param array $defaults |
|
87 | + * @param array $requirements |
|
88 | + */ |
|
89 | + public static function register($method, $url, $action, $app, |
|
90 | + $authLevel = API::USER_AUTH, |
|
91 | + $defaults = array(), |
|
92 | + $requirements = array()) { |
|
93 | + $name = strtolower($method).$url; |
|
94 | + $name = str_replace(array('/', '{', '}'), '_', $name); |
|
95 | + if(!isset(self::$actions[$name])) { |
|
96 | + $oldCollection = OC::$server->getRouter()->getCurrentCollection(); |
|
97 | + OC::$server->getRouter()->useCollection('ocs'); |
|
98 | + OC::$server->getRouter()->create($name, $url) |
|
99 | + ->method($method) |
|
100 | + ->defaults($defaults) |
|
101 | + ->requirements($requirements) |
|
102 | + ->action('OC_API', 'call'); |
|
103 | + self::$actions[$name] = array(); |
|
104 | + OC::$server->getRouter()->useCollection($oldCollection); |
|
105 | + } |
|
106 | + self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * handles an api call |
|
111 | + * @param array $parameters |
|
112 | + */ |
|
113 | + public static function call($parameters) { |
|
114 | + $request = \OC::$server->getRequest(); |
|
115 | + $method = $request->getMethod(); |
|
116 | + |
|
117 | + // Prepare the request variables |
|
118 | + if($method === 'PUT') { |
|
119 | + $parameters['_put'] = $request->getParams(); |
|
120 | + } else if($method === 'DELETE') { |
|
121 | + $parameters['_delete'] = $request->getParams(); |
|
122 | + } |
|
123 | + $name = $parameters['_route']; |
|
124 | + // Foreach registered action |
|
125 | + $responses = array(); |
|
126 | + foreach(self::$actions[$name] as $action) { |
|
127 | + // Check authentication and availability |
|
128 | + if(!self::isAuthorised($action)) { |
|
129 | + $responses[] = array( |
|
130 | + 'app' => $action['app'], |
|
131 | + 'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'), |
|
132 | + 'shipped' => OC_App::isShipped($action['app']), |
|
133 | + ); |
|
134 | + continue; |
|
135 | + } |
|
136 | + if(!is_callable($action['action'])) { |
|
137 | + $responses[] = array( |
|
138 | + 'app' => $action['app'], |
|
139 | + 'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'), |
|
140 | + 'shipped' => OC_App::isShipped($action['app']), |
|
141 | + ); |
|
142 | + continue; |
|
143 | + } |
|
144 | + // Run the action |
|
145 | + $responses[] = array( |
|
146 | + 'app' => $action['app'], |
|
147 | + 'response' => call_user_func($action['action'], $parameters), |
|
148 | + 'shipped' => OC_App::isShipped($action['app']), |
|
149 | + ); |
|
150 | + } |
|
151 | + $response = self::mergeResponses($responses); |
|
152 | + $format = self::requestedFormat(); |
|
153 | + if (self::$logoutRequired) { |
|
154 | + \OC::$server->getUserSession()->logout(); |
|
155 | + } |
|
156 | + |
|
157 | + self::respond($response, $format); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * merge the returned result objects into one response |
|
162 | + * @param array $responses |
|
163 | + * @return OC_OCS_Result |
|
164 | + */ |
|
165 | + public static function mergeResponses($responses) { |
|
166 | + // Sort into shipped and third-party |
|
167 | + $shipped = array( |
|
168 | + 'succeeded' => array(), |
|
169 | + 'failed' => array(), |
|
170 | + ); |
|
171 | + $thirdparty = array( |
|
172 | + 'succeeded' => array(), |
|
173 | + 'failed' => array(), |
|
174 | + ); |
|
175 | + |
|
176 | + foreach($responses as $response) { |
|
177 | + if($response['shipped'] || ($response['app'] === 'core')) { |
|
178 | + if($response['response']->succeeded()) { |
|
179 | + $shipped['succeeded'][$response['app']] = $response; |
|
180 | + } else { |
|
181 | + $shipped['failed'][$response['app']] = $response; |
|
182 | + } |
|
183 | + } else { |
|
184 | + if($response['response']->succeeded()) { |
|
185 | + $thirdparty['succeeded'][$response['app']] = $response; |
|
186 | + } else { |
|
187 | + $thirdparty['failed'][$response['app']] = $response; |
|
188 | + } |
|
189 | + } |
|
190 | + } |
|
191 | + |
|
192 | + // Remove any error responses if there is one shipped response that succeeded |
|
193 | + if(!empty($shipped['failed'])) { |
|
194 | + // Which shipped response do we use if they all failed? |
|
195 | + // They may have failed for different reasons (different status codes) |
|
196 | + // Which response code should we return? |
|
197 | + // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR |
|
198 | + // Merge failed responses if more than one |
|
199 | + $data = array(); |
|
200 | + foreach($shipped['failed'] as $failure) { |
|
201 | + $data = array_merge_recursive($data, $failure['response']->getData()); |
|
202 | + } |
|
203 | + $picked = reset($shipped['failed']); |
|
204 | + $code = $picked['response']->getStatusCode(); |
|
205 | + $meta = $picked['response']->getMeta(); |
|
206 | + $headers = $picked['response']->getHeaders(); |
|
207 | + $response = new OC_OCS_Result($data, $code, $meta['message'], $headers); |
|
208 | + return $response; |
|
209 | + } elseif(!empty($shipped['succeeded'])) { |
|
210 | + $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']); |
|
211 | + } elseif(!empty($thirdparty['failed'])) { |
|
212 | + // Merge failed responses if more than one |
|
213 | + $data = array(); |
|
214 | + foreach($thirdparty['failed'] as $failure) { |
|
215 | + $data = array_merge_recursive($data, $failure['response']->getData()); |
|
216 | + } |
|
217 | + $picked = reset($thirdparty['failed']); |
|
218 | + $code = $picked['response']->getStatusCode(); |
|
219 | + $meta = $picked['response']->getMeta(); |
|
220 | + $headers = $picked['response']->getHeaders(); |
|
221 | + $response = new OC_OCS_Result($data, $code, $meta['message'], $headers); |
|
222 | + return $response; |
|
223 | + } else { |
|
224 | + $responses = $thirdparty['succeeded']; |
|
225 | + } |
|
226 | + // Merge the successful responses |
|
227 | + $data = []; |
|
228 | + $codes = []; |
|
229 | + $header = []; |
|
230 | + |
|
231 | + foreach($responses as $response) { |
|
232 | + if($response['shipped']) { |
|
233 | + $data = array_merge_recursive($response['response']->getData(), $data); |
|
234 | + } else { |
|
235 | + $data = array_merge_recursive($data, $response['response']->getData()); |
|
236 | + } |
|
237 | + $header = array_merge_recursive($header, $response['response']->getHeaders()); |
|
238 | + $codes[] = ['code' => $response['response']->getStatusCode(), |
|
239 | + 'meta' => $response['response']->getMeta()]; |
|
240 | + } |
|
241 | + |
|
242 | + // Use any non 100 status codes |
|
243 | + $statusCode = 100; |
|
244 | + $statusMessage = null; |
|
245 | + foreach($codes as $code) { |
|
246 | + if($code['code'] != 100) { |
|
247 | + $statusCode = $code['code']; |
|
248 | + $statusMessage = $code['meta']['message']; |
|
249 | + break; |
|
250 | + } |
|
251 | + } |
|
252 | + |
|
253 | + return new OC_OCS_Result($data, $statusCode, $statusMessage, $header); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * authenticate the api call |
|
258 | + * @param array $action the action details as supplied to OC_API::register() |
|
259 | + * @return bool |
|
260 | + */ |
|
261 | + private static function isAuthorised($action) { |
|
262 | + $level = $action['authlevel']; |
|
263 | + switch($level) { |
|
264 | + case API::GUEST_AUTH: |
|
265 | + // Anyone can access |
|
266 | + return true; |
|
267 | + case API::USER_AUTH: |
|
268 | + // User required |
|
269 | + return self::loginUser(); |
|
270 | + case API::SUBADMIN_AUTH: |
|
271 | + // Check for subadmin |
|
272 | + $user = self::loginUser(); |
|
273 | + if(!$user) { |
|
274 | + return false; |
|
275 | + } else { |
|
276 | + $userObject = \OC::$server->getUserSession()->getUser(); |
|
277 | + if($userObject === null) { |
|
278 | + return false; |
|
279 | + } |
|
280 | + $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
|
281 | + $admin = OC_User::isAdminUser($user); |
|
282 | + if($isSubAdmin || $admin) { |
|
283 | + return true; |
|
284 | + } else { |
|
285 | + return false; |
|
286 | + } |
|
287 | + } |
|
288 | + case API::ADMIN_AUTH: |
|
289 | + // Check for admin |
|
290 | + $user = self::loginUser(); |
|
291 | + if(!$user) { |
|
292 | + return false; |
|
293 | + } else { |
|
294 | + return OC_User::isAdminUser($user); |
|
295 | + } |
|
296 | + default: |
|
297 | + // oops looks like invalid level supplied |
|
298 | + return false; |
|
299 | + } |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * http basic auth |
|
304 | + * @return string|false (username, or false on failure) |
|
305 | + */ |
|
306 | + private static function loginUser() { |
|
307 | + if(self::$isLoggedIn === true) { |
|
308 | + return \OC_User::getUser(); |
|
309 | + } |
|
310 | + |
|
311 | + // reuse existing login |
|
312 | + $loggedIn = \OC::$server->getUserSession()->isLoggedIn(); |
|
313 | + if ($loggedIn === true) { |
|
314 | + if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) { |
|
315 | + // Do not allow access to OCS until the 2FA challenge was solved successfully |
|
316 | + return false; |
|
317 | + } |
|
318 | + $ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false; |
|
319 | + if ($ocsApiRequest) { |
|
320 | + |
|
321 | + // initialize the user's filesystem |
|
322 | + \OC_Util::setupFS(\OC_User::getUser()); |
|
323 | + self::$isLoggedIn = true; |
|
324 | + |
|
325 | + return OC_User::getUser(); |
|
326 | + } |
|
327 | + return false; |
|
328 | + } |
|
329 | + |
|
330 | + // basic auth - because OC_User::login will create a new session we shall only try to login |
|
331 | + // if user and pass are set |
|
332 | + $userSession = \OC::$server->getUserSession(); |
|
333 | + $request = \OC::$server->getRequest(); |
|
334 | + try { |
|
335 | + $loginSuccess = $userSession->tryTokenLogin($request); |
|
336 | + if (!$loginSuccess) { |
|
337 | + $loginSuccess = $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler()); |
|
338 | + } |
|
339 | + } catch (\OC\User\LoginException $e) { |
|
340 | + return false; |
|
341 | + } |
|
342 | 342 | |
343 | - if ($loginSuccess === true) { |
|
344 | - self::$logoutRequired = true; |
|
345 | - |
|
346 | - // initialize the user's filesystem |
|
347 | - \OC_Util::setupFS(\OC_User::getUser()); |
|
348 | - self::$isLoggedIn = true; |
|
349 | - |
|
350 | - return \OC_User::getUser(); |
|
351 | - } |
|
352 | - |
|
353 | - return false; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * respond to a call |
|
358 | - * @param OC_OCS_Result $result |
|
359 | - * @param string $format the format xml|json |
|
360 | - */ |
|
361 | - public static function respond($result, $format='xml') { |
|
362 | - $request = \OC::$server->getRequest(); |
|
363 | - |
|
364 | - // Send 401 headers if unauthorised |
|
365 | - if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
366 | - // If request comes from JS return dummy auth request |
|
367 | - if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
368 | - header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
|
369 | - } else { |
|
370 | - header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
|
371 | - } |
|
372 | - header('HTTP/1.0 401 Unauthorized'); |
|
373 | - } |
|
374 | - |
|
375 | - foreach($result->getHeaders() as $name => $value) { |
|
376 | - header($name . ': ' . $value); |
|
377 | - } |
|
378 | - |
|
379 | - $meta = $result->getMeta(); |
|
380 | - $data = $result->getData(); |
|
381 | - if (self::isV2($request)) { |
|
382 | - $statusCode = self::mapStatusCodes($result->getStatusCode()); |
|
383 | - if (!is_null($statusCode)) { |
|
384 | - $meta['statuscode'] = $statusCode; |
|
385 | - OC_Response::setStatus($statusCode); |
|
386 | - } |
|
387 | - } |
|
388 | - |
|
389 | - self::setContentType($format); |
|
390 | - $body = self::renderResult($format, $meta, $data); |
|
391 | - echo $body; |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * @param XMLWriter $writer |
|
396 | - */ |
|
397 | - private static function toXML($array, $writer) { |
|
398 | - foreach($array as $k => $v) { |
|
399 | - if ($k[0] === '@') { |
|
400 | - $writer->writeAttribute(substr($k, 1), $v); |
|
401 | - continue; |
|
402 | - } else if (is_numeric($k)) { |
|
403 | - $k = 'element'; |
|
404 | - } |
|
405 | - if(is_array($v)) { |
|
406 | - $writer->startElement($k); |
|
407 | - self::toXML($v, $writer); |
|
408 | - $writer->endElement(); |
|
409 | - } else { |
|
410 | - $writer->writeElement($k, $v); |
|
411 | - } |
|
412 | - } |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * @return string |
|
417 | - */ |
|
418 | - public static function requestedFormat() { |
|
419 | - $formats = array('json', 'xml'); |
|
420 | - |
|
421 | - $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; |
|
422 | - return $format; |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * Based on the requested format the response content type is set |
|
427 | - * @param string $format |
|
428 | - */ |
|
429 | - public static function setContentType($format = null) { |
|
430 | - $format = is_null($format) ? self::requestedFormat() : $format; |
|
431 | - if ($format === 'xml') { |
|
432 | - header('Content-type: text/xml; charset=UTF-8'); |
|
433 | - return; |
|
434 | - } |
|
435 | - |
|
436 | - if ($format === 'json') { |
|
437 | - header('Content-Type: application/json; charset=utf-8'); |
|
438 | - return; |
|
439 | - } |
|
440 | - |
|
441 | - header('Content-Type: application/octet-stream; charset=utf-8'); |
|
442 | - } |
|
443 | - |
|
444 | - /** |
|
445 | - * @param \OCP\IRequest $request |
|
446 | - * @return bool |
|
447 | - */ |
|
448 | - protected static function isV2(\OCP\IRequest $request) { |
|
449 | - $script = $request->getScriptName(); |
|
450 | - |
|
451 | - return substr($script, -11) === '/ocs/v2.php'; |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * @param integer $sc |
|
456 | - * @return int |
|
457 | - */ |
|
458 | - public static function mapStatusCodes($sc) { |
|
459 | - switch ($sc) { |
|
460 | - case API::RESPOND_NOT_FOUND: |
|
461 | - return Http::STATUS_NOT_FOUND; |
|
462 | - case API::RESPOND_SERVER_ERROR: |
|
463 | - return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
464 | - case API::RESPOND_UNKNOWN_ERROR: |
|
465 | - return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
466 | - case API::RESPOND_UNAUTHORISED: |
|
467 | - // already handled for v1 |
|
468 | - return null; |
|
469 | - case 100: |
|
470 | - return Http::STATUS_OK; |
|
471 | - } |
|
472 | - // any 2xx, 4xx and 5xx will be used as is |
|
473 | - if ($sc >= 200 && $sc < 600) { |
|
474 | - return $sc; |
|
475 | - } |
|
476 | - |
|
477 | - return Http::STATUS_BAD_REQUEST; |
|
478 | - } |
|
479 | - |
|
480 | - /** |
|
481 | - * @param string $format |
|
482 | - * @return string |
|
483 | - */ |
|
484 | - public static function renderResult($format, $meta, $data) { |
|
485 | - $response = array( |
|
486 | - 'ocs' => array( |
|
487 | - 'meta' => $meta, |
|
488 | - 'data' => $data, |
|
489 | - ), |
|
490 | - ); |
|
491 | - if ($format == 'json') { |
|
492 | - return OC_JSON::encode($response); |
|
493 | - } |
|
494 | - |
|
495 | - $writer = new XMLWriter(); |
|
496 | - $writer->openMemory(); |
|
497 | - $writer->setIndent(true); |
|
498 | - $writer->startDocument(); |
|
499 | - self::toXML($response, $writer); |
|
500 | - $writer->endDocument(); |
|
501 | - return $writer->outputMemory(true); |
|
502 | - } |
|
343 | + if ($loginSuccess === true) { |
|
344 | + self::$logoutRequired = true; |
|
345 | + |
|
346 | + // initialize the user's filesystem |
|
347 | + \OC_Util::setupFS(\OC_User::getUser()); |
|
348 | + self::$isLoggedIn = true; |
|
349 | + |
|
350 | + return \OC_User::getUser(); |
|
351 | + } |
|
352 | + |
|
353 | + return false; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * respond to a call |
|
358 | + * @param OC_OCS_Result $result |
|
359 | + * @param string $format the format xml|json |
|
360 | + */ |
|
361 | + public static function respond($result, $format='xml') { |
|
362 | + $request = \OC::$server->getRequest(); |
|
363 | + |
|
364 | + // Send 401 headers if unauthorised |
|
365 | + if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
366 | + // If request comes from JS return dummy auth request |
|
367 | + if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
368 | + header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
|
369 | + } else { |
|
370 | + header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
|
371 | + } |
|
372 | + header('HTTP/1.0 401 Unauthorized'); |
|
373 | + } |
|
374 | + |
|
375 | + foreach($result->getHeaders() as $name => $value) { |
|
376 | + header($name . ': ' . $value); |
|
377 | + } |
|
378 | + |
|
379 | + $meta = $result->getMeta(); |
|
380 | + $data = $result->getData(); |
|
381 | + if (self::isV2($request)) { |
|
382 | + $statusCode = self::mapStatusCodes($result->getStatusCode()); |
|
383 | + if (!is_null($statusCode)) { |
|
384 | + $meta['statuscode'] = $statusCode; |
|
385 | + OC_Response::setStatus($statusCode); |
|
386 | + } |
|
387 | + } |
|
388 | + |
|
389 | + self::setContentType($format); |
|
390 | + $body = self::renderResult($format, $meta, $data); |
|
391 | + echo $body; |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * @param XMLWriter $writer |
|
396 | + */ |
|
397 | + private static function toXML($array, $writer) { |
|
398 | + foreach($array as $k => $v) { |
|
399 | + if ($k[0] === '@') { |
|
400 | + $writer->writeAttribute(substr($k, 1), $v); |
|
401 | + continue; |
|
402 | + } else if (is_numeric($k)) { |
|
403 | + $k = 'element'; |
|
404 | + } |
|
405 | + if(is_array($v)) { |
|
406 | + $writer->startElement($k); |
|
407 | + self::toXML($v, $writer); |
|
408 | + $writer->endElement(); |
|
409 | + } else { |
|
410 | + $writer->writeElement($k, $v); |
|
411 | + } |
|
412 | + } |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * @return string |
|
417 | + */ |
|
418 | + public static function requestedFormat() { |
|
419 | + $formats = array('json', 'xml'); |
|
420 | + |
|
421 | + $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; |
|
422 | + return $format; |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * Based on the requested format the response content type is set |
|
427 | + * @param string $format |
|
428 | + */ |
|
429 | + public static function setContentType($format = null) { |
|
430 | + $format = is_null($format) ? self::requestedFormat() : $format; |
|
431 | + if ($format === 'xml') { |
|
432 | + header('Content-type: text/xml; charset=UTF-8'); |
|
433 | + return; |
|
434 | + } |
|
435 | + |
|
436 | + if ($format === 'json') { |
|
437 | + header('Content-Type: application/json; charset=utf-8'); |
|
438 | + return; |
|
439 | + } |
|
440 | + |
|
441 | + header('Content-Type: application/octet-stream; charset=utf-8'); |
|
442 | + } |
|
443 | + |
|
444 | + /** |
|
445 | + * @param \OCP\IRequest $request |
|
446 | + * @return bool |
|
447 | + */ |
|
448 | + protected static function isV2(\OCP\IRequest $request) { |
|
449 | + $script = $request->getScriptName(); |
|
450 | + |
|
451 | + return substr($script, -11) === '/ocs/v2.php'; |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * @param integer $sc |
|
456 | + * @return int |
|
457 | + */ |
|
458 | + public static function mapStatusCodes($sc) { |
|
459 | + switch ($sc) { |
|
460 | + case API::RESPOND_NOT_FOUND: |
|
461 | + return Http::STATUS_NOT_FOUND; |
|
462 | + case API::RESPOND_SERVER_ERROR: |
|
463 | + return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
464 | + case API::RESPOND_UNKNOWN_ERROR: |
|
465 | + return Http::STATUS_INTERNAL_SERVER_ERROR; |
|
466 | + case API::RESPOND_UNAUTHORISED: |
|
467 | + // already handled for v1 |
|
468 | + return null; |
|
469 | + case 100: |
|
470 | + return Http::STATUS_OK; |
|
471 | + } |
|
472 | + // any 2xx, 4xx and 5xx will be used as is |
|
473 | + if ($sc >= 200 && $sc < 600) { |
|
474 | + return $sc; |
|
475 | + } |
|
476 | + |
|
477 | + return Http::STATUS_BAD_REQUEST; |
|
478 | + } |
|
479 | + |
|
480 | + /** |
|
481 | + * @param string $format |
|
482 | + * @return string |
|
483 | + */ |
|
484 | + public static function renderResult($format, $meta, $data) { |
|
485 | + $response = array( |
|
486 | + 'ocs' => array( |
|
487 | + 'meta' => $meta, |
|
488 | + 'data' => $data, |
|
489 | + ), |
|
490 | + ); |
|
491 | + if ($format == 'json') { |
|
492 | + return OC_JSON::encode($response); |
|
493 | + } |
|
494 | + |
|
495 | + $writer = new XMLWriter(); |
|
496 | + $writer->openMemory(); |
|
497 | + $writer->setIndent(true); |
|
498 | + $writer->startDocument(); |
|
499 | + self::toXML($response, $writer); |
|
500 | + $writer->endDocument(); |
|
501 | + return $writer->outputMemory(true); |
|
502 | + } |
|
503 | 503 | } |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | $requirements = array()) { |
93 | 93 | $name = strtolower($method).$url; |
94 | 94 | $name = str_replace(array('/', '{', '}'), '_', $name); |
95 | - if(!isset(self::$actions[$name])) { |
|
95 | + if (!isset(self::$actions[$name])) { |
|
96 | 96 | $oldCollection = OC::$server->getRouter()->getCurrentCollection(); |
97 | 97 | OC::$server->getRouter()->useCollection('ocs'); |
98 | 98 | OC::$server->getRouter()->create($name, $url) |
@@ -115,17 +115,17 @@ discard block |
||
115 | 115 | $method = $request->getMethod(); |
116 | 116 | |
117 | 117 | // Prepare the request variables |
118 | - if($method === 'PUT') { |
|
118 | + if ($method === 'PUT') { |
|
119 | 119 | $parameters['_put'] = $request->getParams(); |
120 | - } else if($method === 'DELETE') { |
|
120 | + } else if ($method === 'DELETE') { |
|
121 | 121 | $parameters['_delete'] = $request->getParams(); |
122 | 122 | } |
123 | 123 | $name = $parameters['_route']; |
124 | 124 | // Foreach registered action |
125 | 125 | $responses = array(); |
126 | - foreach(self::$actions[$name] as $action) { |
|
126 | + foreach (self::$actions[$name] as $action) { |
|
127 | 127 | // Check authentication and availability |
128 | - if(!self::isAuthorised($action)) { |
|
128 | + if (!self::isAuthorised($action)) { |
|
129 | 129 | $responses[] = array( |
130 | 130 | 'app' => $action['app'], |
131 | 131 | 'response' => new OC_OCS_Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'), |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | ); |
134 | 134 | continue; |
135 | 135 | } |
136 | - if(!is_callable($action['action'])) { |
|
136 | + if (!is_callable($action['action'])) { |
|
137 | 137 | $responses[] = array( |
138 | 138 | 'app' => $action['app'], |
139 | 139 | 'response' => new OC_OCS_Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'), |
@@ -173,15 +173,15 @@ discard block |
||
173 | 173 | 'failed' => array(), |
174 | 174 | ); |
175 | 175 | |
176 | - foreach($responses as $response) { |
|
177 | - if($response['shipped'] || ($response['app'] === 'core')) { |
|
178 | - if($response['response']->succeeded()) { |
|
176 | + foreach ($responses as $response) { |
|
177 | + if ($response['shipped'] || ($response['app'] === 'core')) { |
|
178 | + if ($response['response']->succeeded()) { |
|
179 | 179 | $shipped['succeeded'][$response['app']] = $response; |
180 | 180 | } else { |
181 | 181 | $shipped['failed'][$response['app']] = $response; |
182 | 182 | } |
183 | 183 | } else { |
184 | - if($response['response']->succeeded()) { |
|
184 | + if ($response['response']->succeeded()) { |
|
185 | 185 | $thirdparty['succeeded'][$response['app']] = $response; |
186 | 186 | } else { |
187 | 187 | $thirdparty['failed'][$response['app']] = $response; |
@@ -190,14 +190,14 @@ discard block |
||
190 | 190 | } |
191 | 191 | |
192 | 192 | // Remove any error responses if there is one shipped response that succeeded |
193 | - if(!empty($shipped['failed'])) { |
|
193 | + if (!empty($shipped['failed'])) { |
|
194 | 194 | // Which shipped response do we use if they all failed? |
195 | 195 | // They may have failed for different reasons (different status codes) |
196 | 196 | // Which response code should we return? |
197 | 197 | // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR |
198 | 198 | // Merge failed responses if more than one |
199 | 199 | $data = array(); |
200 | - foreach($shipped['failed'] as $failure) { |
|
200 | + foreach ($shipped['failed'] as $failure) { |
|
201 | 201 | $data = array_merge_recursive($data, $failure['response']->getData()); |
202 | 202 | } |
203 | 203 | $picked = reset($shipped['failed']); |
@@ -206,12 +206,12 @@ discard block |
||
206 | 206 | $headers = $picked['response']->getHeaders(); |
207 | 207 | $response = new OC_OCS_Result($data, $code, $meta['message'], $headers); |
208 | 208 | return $response; |
209 | - } elseif(!empty($shipped['succeeded'])) { |
|
209 | + } elseif (!empty($shipped['succeeded'])) { |
|
210 | 210 | $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']); |
211 | - } elseif(!empty($thirdparty['failed'])) { |
|
211 | + } elseif (!empty($thirdparty['failed'])) { |
|
212 | 212 | // Merge failed responses if more than one |
213 | 213 | $data = array(); |
214 | - foreach($thirdparty['failed'] as $failure) { |
|
214 | + foreach ($thirdparty['failed'] as $failure) { |
|
215 | 215 | $data = array_merge_recursive($data, $failure['response']->getData()); |
216 | 216 | } |
217 | 217 | $picked = reset($thirdparty['failed']); |
@@ -228,8 +228,8 @@ discard block |
||
228 | 228 | $codes = []; |
229 | 229 | $header = []; |
230 | 230 | |
231 | - foreach($responses as $response) { |
|
232 | - if($response['shipped']) { |
|
231 | + foreach ($responses as $response) { |
|
232 | + if ($response['shipped']) { |
|
233 | 233 | $data = array_merge_recursive($response['response']->getData(), $data); |
234 | 234 | } else { |
235 | 235 | $data = array_merge_recursive($data, $response['response']->getData()); |
@@ -242,8 +242,8 @@ discard block |
||
242 | 242 | // Use any non 100 status codes |
243 | 243 | $statusCode = 100; |
244 | 244 | $statusMessage = null; |
245 | - foreach($codes as $code) { |
|
246 | - if($code['code'] != 100) { |
|
245 | + foreach ($codes as $code) { |
|
246 | + if ($code['code'] != 100) { |
|
247 | 247 | $statusCode = $code['code']; |
248 | 248 | $statusMessage = $code['meta']['message']; |
249 | 249 | break; |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | */ |
261 | 261 | private static function isAuthorised($action) { |
262 | 262 | $level = $action['authlevel']; |
263 | - switch($level) { |
|
263 | + switch ($level) { |
|
264 | 264 | case API::GUEST_AUTH: |
265 | 265 | // Anyone can access |
266 | 266 | return true; |
@@ -270,16 +270,16 @@ discard block |
||
270 | 270 | case API::SUBADMIN_AUTH: |
271 | 271 | // Check for subadmin |
272 | 272 | $user = self::loginUser(); |
273 | - if(!$user) { |
|
273 | + if (!$user) { |
|
274 | 274 | return false; |
275 | 275 | } else { |
276 | 276 | $userObject = \OC::$server->getUserSession()->getUser(); |
277 | - if($userObject === null) { |
|
277 | + if ($userObject === null) { |
|
278 | 278 | return false; |
279 | 279 | } |
280 | 280 | $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject); |
281 | 281 | $admin = OC_User::isAdminUser($user); |
282 | - if($isSubAdmin || $admin) { |
|
282 | + if ($isSubAdmin || $admin) { |
|
283 | 283 | return true; |
284 | 284 | } else { |
285 | 285 | return false; |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | case API::ADMIN_AUTH: |
289 | 289 | // Check for admin |
290 | 290 | $user = self::loginUser(); |
291 | - if(!$user) { |
|
291 | + if (!$user) { |
|
292 | 292 | return false; |
293 | 293 | } else { |
294 | 294 | return OC_User::isAdminUser($user); |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | * @return string|false (username, or false on failure) |
305 | 305 | */ |
306 | 306 | private static function loginUser() { |
307 | - if(self::$isLoggedIn === true) { |
|
307 | + if (self::$isLoggedIn === true) { |
|
308 | 308 | return \OC_User::getUser(); |
309 | 309 | } |
310 | 310 | |
@@ -358,13 +358,13 @@ discard block |
||
358 | 358 | * @param OC_OCS_Result $result |
359 | 359 | * @param string $format the format xml|json |
360 | 360 | */ |
361 | - public static function respond($result, $format='xml') { |
|
361 | + public static function respond($result, $format = 'xml') { |
|
362 | 362 | $request = \OC::$server->getRequest(); |
363 | 363 | |
364 | 364 | // Send 401 headers if unauthorised |
365 | - if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
365 | + if ($result->getStatusCode() === API::RESPOND_UNAUTHORISED) { |
|
366 | 366 | // If request comes from JS return dummy auth request |
367 | - if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
367 | + if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { |
|
368 | 368 | header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); |
369 | 369 | } else { |
370 | 370 | header('WWW-Authenticate: Basic realm="Authorisation Required"'); |
@@ -372,8 +372,8 @@ discard block |
||
372 | 372 | header('HTTP/1.0 401 Unauthorized'); |
373 | 373 | } |
374 | 374 | |
375 | - foreach($result->getHeaders() as $name => $value) { |
|
376 | - header($name . ': ' . $value); |
|
375 | + foreach ($result->getHeaders() as $name => $value) { |
|
376 | + header($name.': '.$value); |
|
377 | 377 | } |
378 | 378 | |
379 | 379 | $meta = $result->getMeta(); |
@@ -395,14 +395,14 @@ discard block |
||
395 | 395 | * @param XMLWriter $writer |
396 | 396 | */ |
397 | 397 | private static function toXML($array, $writer) { |
398 | - foreach($array as $k => $v) { |
|
398 | + foreach ($array as $k => $v) { |
|
399 | 399 | if ($k[0] === '@') { |
400 | 400 | $writer->writeAttribute(substr($k, 1), $v); |
401 | 401 | continue; |
402 | 402 | } else if (is_numeric($k)) { |
403 | 403 | $k = 'element'; |
404 | 404 | } |
405 | - if(is_array($v)) { |
|
405 | + if (is_array($v)) { |
|
406 | 406 | $writer->startElement($k); |
407 | 407 | self::toXML($v, $writer); |
408 | 408 | $writer->endElement(); |