Passed
Push — master ( 3ea7fa...3da736 )
by Morris
34:52 queued 19:21
created
lib/private/Files/Node/Folder.php 2 patches
Indentation   +394 added lines, -394 removed lines patch added patch discarded remove patch
@@ -36,398 +36,398 @@
 block discarded – undo
36 36
 use OCP\Files\Search\ISearchOperator;
37 37
 
38 38
 class Folder extends Node implements \OCP\Files\Folder {
39
-	/**
40
-	 * Creates a Folder that represents a non-existing path
41
-	 *
42
-	 * @param string $path path
43
-	 * @return string non-existing node class
44
-	 */
45
-	protected function createNonExistingNode($path) {
46
-		return new NonExistingFolder($this->root, $this->view, $path);
47
-	}
48
-
49
-	/**
50
-	 * @param string $path path relative to the folder
51
-	 * @return string
52
-	 * @throws \OCP\Files\NotPermittedException
53
-	 */
54
-	public function getFullPath($path) {
55
-		if (!$this->isValidPath($path)) {
56
-			throw new NotPermittedException('Invalid path');
57
-		}
58
-		return $this->path . $this->normalizePath($path);
59
-	}
60
-
61
-	/**
62
-	 * @param string $path
63
-	 * @return string
64
-	 */
65
-	public function getRelativePath($path) {
66
-		if ($this->path === '' or $this->path === '/') {
67
-			return $this->normalizePath($path);
68
-		}
69
-		if ($path === $this->path) {
70
-			return '/';
71
-		} else if (strpos($path, $this->path . '/') !== 0) {
72
-			return null;
73
-		} else {
74
-			$path = substr($path, strlen($this->path));
75
-			return $this->normalizePath($path);
76
-		}
77
-	}
78
-
79
-	/**
80
-	 * check if a node is a (grand-)child of the folder
81
-	 *
82
-	 * @param \OC\Files\Node\Node $node
83
-	 * @return bool
84
-	 */
85
-	public function isSubNode($node) {
86
-		return strpos($node->getPath(), $this->path . '/') === 0;
87
-	}
88
-
89
-	/**
90
-	 * get the content of this directory
91
-	 *
92
-	 * @throws \OCP\Files\NotFoundException
93
-	 * @return Node[]
94
-	 */
95
-	public function getDirectoryListing() {
96
-		$folderContent = $this->view->getDirectoryContent($this->path);
97
-
98
-		return array_map(function (FileInfo $info) {
99
-			if ($info->getMimetype() === 'httpd/unix-directory') {
100
-				return new Folder($this->root, $this->view, $info->getPath(), $info);
101
-			} else {
102
-				return new File($this->root, $this->view, $info->getPath(), $info);
103
-			}
104
-		}, $folderContent);
105
-	}
106
-
107
-	/**
108
-	 * @param string $path
109
-	 * @param FileInfo $info
110
-	 * @return File|Folder
111
-	 */
112
-	protected function createNode($path, FileInfo $info = null) {
113
-		if (is_null($info)) {
114
-			$isDir = $this->view->is_dir($path);
115
-		} else {
116
-			$isDir = $info->getType() === FileInfo::TYPE_FOLDER;
117
-		}
118
-		if ($isDir) {
119
-			return new Folder($this->root, $this->view, $path, $info);
120
-		} else {
121
-			return new File($this->root, $this->view, $path, $info);
122
-		}
123
-	}
124
-
125
-	/**
126
-	 * Get the node at $path
127
-	 *
128
-	 * @param string $path
129
-	 * @return \OC\Files\Node\Node
130
-	 * @throws \OCP\Files\NotFoundException
131
-	 */
132
-	public function get($path) {
133
-		return $this->root->get($this->getFullPath($path));
134
-	}
135
-
136
-	/**
137
-	 * @param string $path
138
-	 * @return bool
139
-	 */
140
-	public function nodeExists($path) {
141
-		try {
142
-			$this->get($path);
143
-			return true;
144
-		} catch (NotFoundException $e) {
145
-			return false;
146
-		}
147
-	}
148
-
149
-	/**
150
-	 * @param string $path
151
-	 * @return \OC\Files\Node\Folder
152
-	 * @throws \OCP\Files\NotPermittedException
153
-	 */
154
-	public function newFolder($path) {
155
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
156
-			$fullPath = $this->getFullPath($path);
157
-			$nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
158
-			$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
159
-			$this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
160
-			$this->view->mkdir($fullPath);
161
-			$node = new Folder($this->root, $this->view, $fullPath);
162
-			$this->root->emit('\OC\Files', 'postWrite', array($node));
163
-			$this->root->emit('\OC\Files', 'postCreate', array($node));
164
-			return $node;
165
-		} else {
166
-			throw new NotPermittedException('No create permission for folder');
167
-		}
168
-	}
169
-
170
-	/**
171
-	 * @param string $path
172
-	 * @return \OC\Files\Node\File
173
-	 * @throws \OCP\Files\NotPermittedException
174
-	 */
175
-	public function newFile($path) {
176
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
177
-			$fullPath = $this->getFullPath($path);
178
-			$nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
179
-			$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
180
-			$this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
181
-			$this->view->touch($fullPath);
182
-			$node = new File($this->root, $this->view, $fullPath);
183
-			$this->root->emit('\OC\Files', 'postWrite', array($node));
184
-			$this->root->emit('\OC\Files', 'postCreate', array($node));
185
-			return $node;
186
-		} else {
187
-			throw new NotPermittedException('No create permission for path');
188
-		}
189
-	}
190
-
191
-	/**
192
-	 * search for files with the name matching $query
193
-	 *
194
-	 * @param string|ISearchOperator $query
195
-	 * @return \OC\Files\Node\Node[]
196
-	 */
197
-	public function search($query) {
198
-		if (is_string($query)) {
199
-			return $this->searchCommon('search', array('%' . $query . '%'));
200
-		} else {
201
-			return $this->searchCommon('searchQuery', array($query));
202
-		}
203
-	}
204
-
205
-	/**
206
-	 * search for files by mimetype
207
-	 *
208
-	 * @param string $mimetype
209
-	 * @return Node[]
210
-	 */
211
-	public function searchByMime($mimetype) {
212
-		return $this->searchCommon('searchByMime', array($mimetype));
213
-	}
214
-
215
-	/**
216
-	 * search for files by tag
217
-	 *
218
-	 * @param string|int $tag name or tag id
219
-	 * @param string $userId owner of the tags
220
-	 * @return Node[]
221
-	 */
222
-	public function searchByTag($tag, $userId) {
223
-		return $this->searchCommon('searchByTag', array($tag, $userId));
224
-	}
225
-
226
-	/**
227
-	 * @param string $method cache method
228
-	 * @param array $args call args
229
-	 * @return \OC\Files\Node\Node[]
230
-	 */
231
-	private function searchCommon($method, $args) {
232
-		$files = array();
233
-		$rootLength = strlen($this->path);
234
-		$mount = $this->root->getMount($this->path);
235
-		$storage = $mount->getStorage();
236
-		$internalPath = $mount->getInternalPath($this->path);
237
-		$internalPath = rtrim($internalPath, '/');
238
-		if ($internalPath !== '') {
239
-			$internalPath = $internalPath . '/';
240
-		}
241
-		$internalRootLength = strlen($internalPath);
242
-
243
-		$cache = $storage->getCache('');
244
-
245
-		$results = call_user_func_array(array($cache, $method), $args);
246
-		foreach ($results as $result) {
247
-			if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
248
-				$result['internalPath'] = $result['path'];
249
-				$result['path'] = substr($result['path'], $internalRootLength);
250
-				$result['storage'] = $storage;
251
-				$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
252
-			}
253
-		}
254
-
255
-		$mounts = $this->root->getMountsIn($this->path);
256
-		foreach ($mounts as $mount) {
257
-			$storage = $mount->getStorage();
258
-			if ($storage) {
259
-				$cache = $storage->getCache('');
260
-
261
-				$relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
262
-				$results = call_user_func_array(array($cache, $method), $args);
263
-				foreach ($results as $result) {
264
-					$result['internalPath'] = $result['path'];
265
-					$result['path'] = $relativeMountPoint . $result['path'];
266
-					$result['storage'] = $storage;
267
-					$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
268
-				}
269
-			}
270
-		}
271
-
272
-		return array_map(function (FileInfo $file) {
273
-			return $this->createNode($file->getPath(), $file);
274
-		}, $files);
275
-	}
276
-
277
-	/**
278
-	 * @param int $id
279
-	 * @return \OC\Files\Node\Node[]
280
-	 */
281
-	public function getById($id) {
282
-		$mountCache = $this->root->getUserMountCache();
283
-		if (strpos($this->getPath(), '/', 1) > 0) {
284
-			list(, $user) = explode('/', $this->getPath());
285
-		} else {
286
-			$user = null;
287
-		}
288
-		$mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user);
289
-		$mounts = $this->root->getMountsIn($this->path);
290
-		$mounts[] = $this->root->getMount($this->path);
291
-		/** @var IMountPoint[] $folderMounts */
292
-		$folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) {
293
-			return $mountPoint->getMountPoint();
294
-		}, $mounts), $mounts);
295
-
296
-		/** @var ICachedMountInfo[] $mountsContainingFile */
297
-		$mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
298
-			return isset($folderMounts[$cachedMountInfo->getMountPoint()]);
299
-		}));
300
-
301
-		if (count($mountsContainingFile) === 0) {
302
-			return [];
303
-		}
304
-
305
-		$nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) {
306
-			$mount = $folderMounts[$cachedMountInfo->getMountPoint()];
307
-			$cacheEntry = $mount->getStorage()->getCache()->get((int)$id);
308
-			if (!$cacheEntry) {
309
-				return null;
310
-			}
311
-
312
-			// cache jails will hide the "true" internal path
313
-			$internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/');
314
-			$pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath()));
315
-			$pathRelativeToMount = ltrim($pathRelativeToMount, '/');
316
-			$absolutePath = rtrim($cachedMountInfo->getMountPoint() . $pathRelativeToMount, '/');
317
-			return $this->root->createNode($absolutePath, new \OC\Files\FileInfo(
318
-				$absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
319
-				\OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
320
-			));
321
-		}, $mountsContainingFile);
322
-
323
-		$nodes = array_filter($nodes);
324
-
325
-		return array_filter($nodes, function (Node $node) {
326
-			return $this->getRelativePath($node->getPath());
327
-		});
328
-	}
329
-
330
-	public function getFreeSpace() {
331
-		return $this->view->free_space($this->path);
332
-	}
333
-
334
-	public function delete() {
335
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
336
-			$this->sendHooks(array('preDelete'));
337
-			$fileInfo = $this->getFileInfo();
338
-			$this->view->rmdir($this->path);
339
-			$nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
340
-			$this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
341
-			$this->exists = false;
342
-		} else {
343
-			throw new NotPermittedException('No delete permission for path');
344
-		}
345
-	}
346
-
347
-	/**
348
-	 * Add a suffix to the name in case the file exists
349
-	 *
350
-	 * @param string $name
351
-	 * @return string
352
-	 * @throws NotPermittedException
353
-	 */
354
-	public function getNonExistingName($name) {
355
-		$uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
356
-		return trim($this->getRelativePath($uniqueName), '/');
357
-	}
358
-
359
-	/**
360
-	 * @param int $limit
361
-	 * @param int $offset
362
-	 * @return \OCP\Files\Node[]
363
-	 */
364
-	public function getRecent($limit, $offset = 0) {
365
-		$mimetypeLoader = \OC::$server->getMimeTypeLoader();
366
-		$mounts = $this->root->getMountsIn($this->path);
367
-		$mounts[] = $this->getMountPoint();
368
-
369
-		$mounts = array_filter($mounts, function (IMountPoint $mount) {
370
-			return $mount->getStorage();
371
-		});
372
-		$storageIds = array_map(function (IMountPoint $mount) {
373
-			return $mount->getStorage()->getCache()->getNumericStorageId();
374
-		}, $mounts);
375
-		/** @var IMountPoint[] $mountMap */
376
-		$mountMap = array_combine($storageIds, $mounts);
377
-		$folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER);
378
-
379
-		//todo look into options of filtering path based on storage id (only search in files/ for home storage, filter by share root for shared, etc)
380
-
381
-		$builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
382
-		$query = $builder
383
-			->select('f.*')
384
-			->from('filecache', 'f')
385
-			->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)))
386
-			->andWhere($builder->expr()->orX(
387
-			// handle non empty folders separate
388
-				$builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
389
-				$builder->expr()->eq('f.size', new Literal(0))
390
-			))
391
-			->orderBy('f.mtime', 'DESC')
392
-			->setMaxResults($limit)
393
-			->setFirstResult($offset);
394
-
395
-		$result = $query->execute()->fetchAll();
396
-
397
-		$files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
398
-			$mount = $mountMap[$entry['storage']];
399
-			$entry['internalPath'] = $entry['path'];
400
-			$entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
401
-			$entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']);
402
-			$path = $this->getAbsolutePath($mount, $entry['path']);
403
-			if (is_null($path)) {
404
-				return null;
405
-			}
406
-			$fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount);
407
-			return $this->root->createNode($fileInfo->getPath(), $fileInfo);
408
-		}, $result));
409
-
410
-		return array_values(array_filter($files, function (Node $node) {
411
-			$relative = $this->getRelativePath($node->getPath());
412
-			return $relative !== null && $relative !== '/';
413
-		}));
414
-	}
415
-
416
-	private function getAbsolutePath(IMountPoint $mount, $path) {
417
-		$storage = $mount->getStorage();
418
-		if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) {
419
-			/** @var \OC\Files\Storage\Wrapper\Jail $storage */
420
-			$jailRoot = $storage->getUnjailedPath('');
421
-			$rootLength = strlen($jailRoot) + 1;
422
-			if ($path === $jailRoot) {
423
-				return $mount->getMountPoint();
424
-			} else if (substr($path, 0, $rootLength) === $jailRoot . '/') {
425
-				return $mount->getMountPoint() . substr($path, $rootLength);
426
-			} else {
427
-				return null;
428
-			}
429
-		} else {
430
-			return $mount->getMountPoint() . $path;
431
-		}
432
-	}
39
+    /**
40
+     * Creates a Folder that represents a non-existing path
41
+     *
42
+     * @param string $path path
43
+     * @return string non-existing node class
44
+     */
45
+    protected function createNonExistingNode($path) {
46
+        return new NonExistingFolder($this->root, $this->view, $path);
47
+    }
48
+
49
+    /**
50
+     * @param string $path path relative to the folder
51
+     * @return string
52
+     * @throws \OCP\Files\NotPermittedException
53
+     */
54
+    public function getFullPath($path) {
55
+        if (!$this->isValidPath($path)) {
56
+            throw new NotPermittedException('Invalid path');
57
+        }
58
+        return $this->path . $this->normalizePath($path);
59
+    }
60
+
61
+    /**
62
+     * @param string $path
63
+     * @return string
64
+     */
65
+    public function getRelativePath($path) {
66
+        if ($this->path === '' or $this->path === '/') {
67
+            return $this->normalizePath($path);
68
+        }
69
+        if ($path === $this->path) {
70
+            return '/';
71
+        } else if (strpos($path, $this->path . '/') !== 0) {
72
+            return null;
73
+        } else {
74
+            $path = substr($path, strlen($this->path));
75
+            return $this->normalizePath($path);
76
+        }
77
+    }
78
+
79
+    /**
80
+     * check if a node is a (grand-)child of the folder
81
+     *
82
+     * @param \OC\Files\Node\Node $node
83
+     * @return bool
84
+     */
85
+    public function isSubNode($node) {
86
+        return strpos($node->getPath(), $this->path . '/') === 0;
87
+    }
88
+
89
+    /**
90
+     * get the content of this directory
91
+     *
92
+     * @throws \OCP\Files\NotFoundException
93
+     * @return Node[]
94
+     */
95
+    public function getDirectoryListing() {
96
+        $folderContent = $this->view->getDirectoryContent($this->path);
97
+
98
+        return array_map(function (FileInfo $info) {
99
+            if ($info->getMimetype() === 'httpd/unix-directory') {
100
+                return new Folder($this->root, $this->view, $info->getPath(), $info);
101
+            } else {
102
+                return new File($this->root, $this->view, $info->getPath(), $info);
103
+            }
104
+        }, $folderContent);
105
+    }
106
+
107
+    /**
108
+     * @param string $path
109
+     * @param FileInfo $info
110
+     * @return File|Folder
111
+     */
112
+    protected function createNode($path, FileInfo $info = null) {
113
+        if (is_null($info)) {
114
+            $isDir = $this->view->is_dir($path);
115
+        } else {
116
+            $isDir = $info->getType() === FileInfo::TYPE_FOLDER;
117
+        }
118
+        if ($isDir) {
119
+            return new Folder($this->root, $this->view, $path, $info);
120
+        } else {
121
+            return new File($this->root, $this->view, $path, $info);
122
+        }
123
+    }
124
+
125
+    /**
126
+     * Get the node at $path
127
+     *
128
+     * @param string $path
129
+     * @return \OC\Files\Node\Node
130
+     * @throws \OCP\Files\NotFoundException
131
+     */
132
+    public function get($path) {
133
+        return $this->root->get($this->getFullPath($path));
134
+    }
135
+
136
+    /**
137
+     * @param string $path
138
+     * @return bool
139
+     */
140
+    public function nodeExists($path) {
141
+        try {
142
+            $this->get($path);
143
+            return true;
144
+        } catch (NotFoundException $e) {
145
+            return false;
146
+        }
147
+    }
148
+
149
+    /**
150
+     * @param string $path
151
+     * @return \OC\Files\Node\Folder
152
+     * @throws \OCP\Files\NotPermittedException
153
+     */
154
+    public function newFolder($path) {
155
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
156
+            $fullPath = $this->getFullPath($path);
157
+            $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
158
+            $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
159
+            $this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
160
+            $this->view->mkdir($fullPath);
161
+            $node = new Folder($this->root, $this->view, $fullPath);
162
+            $this->root->emit('\OC\Files', 'postWrite', array($node));
163
+            $this->root->emit('\OC\Files', 'postCreate', array($node));
164
+            return $node;
165
+        } else {
166
+            throw new NotPermittedException('No create permission for folder');
167
+        }
168
+    }
169
+
170
+    /**
171
+     * @param string $path
172
+     * @return \OC\Files\Node\File
173
+     * @throws \OCP\Files\NotPermittedException
174
+     */
175
+    public function newFile($path) {
176
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
177
+            $fullPath = $this->getFullPath($path);
178
+            $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
179
+            $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
180
+            $this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
181
+            $this->view->touch($fullPath);
182
+            $node = new File($this->root, $this->view, $fullPath);
183
+            $this->root->emit('\OC\Files', 'postWrite', array($node));
184
+            $this->root->emit('\OC\Files', 'postCreate', array($node));
185
+            return $node;
186
+        } else {
187
+            throw new NotPermittedException('No create permission for path');
188
+        }
189
+    }
190
+
191
+    /**
192
+     * search for files with the name matching $query
193
+     *
194
+     * @param string|ISearchOperator $query
195
+     * @return \OC\Files\Node\Node[]
196
+     */
197
+    public function search($query) {
198
+        if (is_string($query)) {
199
+            return $this->searchCommon('search', array('%' . $query . '%'));
200
+        } else {
201
+            return $this->searchCommon('searchQuery', array($query));
202
+        }
203
+    }
204
+
205
+    /**
206
+     * search for files by mimetype
207
+     *
208
+     * @param string $mimetype
209
+     * @return Node[]
210
+     */
211
+    public function searchByMime($mimetype) {
212
+        return $this->searchCommon('searchByMime', array($mimetype));
213
+    }
214
+
215
+    /**
216
+     * search for files by tag
217
+     *
218
+     * @param string|int $tag name or tag id
219
+     * @param string $userId owner of the tags
220
+     * @return Node[]
221
+     */
222
+    public function searchByTag($tag, $userId) {
223
+        return $this->searchCommon('searchByTag', array($tag, $userId));
224
+    }
225
+
226
+    /**
227
+     * @param string $method cache method
228
+     * @param array $args call args
229
+     * @return \OC\Files\Node\Node[]
230
+     */
231
+    private function searchCommon($method, $args) {
232
+        $files = array();
233
+        $rootLength = strlen($this->path);
234
+        $mount = $this->root->getMount($this->path);
235
+        $storage = $mount->getStorage();
236
+        $internalPath = $mount->getInternalPath($this->path);
237
+        $internalPath = rtrim($internalPath, '/');
238
+        if ($internalPath !== '') {
239
+            $internalPath = $internalPath . '/';
240
+        }
241
+        $internalRootLength = strlen($internalPath);
242
+
243
+        $cache = $storage->getCache('');
244
+
245
+        $results = call_user_func_array(array($cache, $method), $args);
246
+        foreach ($results as $result) {
247
+            if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
248
+                $result['internalPath'] = $result['path'];
249
+                $result['path'] = substr($result['path'], $internalRootLength);
250
+                $result['storage'] = $storage;
251
+                $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
252
+            }
253
+        }
254
+
255
+        $mounts = $this->root->getMountsIn($this->path);
256
+        foreach ($mounts as $mount) {
257
+            $storage = $mount->getStorage();
258
+            if ($storage) {
259
+                $cache = $storage->getCache('');
260
+
261
+                $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
262
+                $results = call_user_func_array(array($cache, $method), $args);
263
+                foreach ($results as $result) {
264
+                    $result['internalPath'] = $result['path'];
265
+                    $result['path'] = $relativeMountPoint . $result['path'];
266
+                    $result['storage'] = $storage;
267
+                    $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
268
+                }
269
+            }
270
+        }
271
+
272
+        return array_map(function (FileInfo $file) {
273
+            return $this->createNode($file->getPath(), $file);
274
+        }, $files);
275
+    }
276
+
277
+    /**
278
+     * @param int $id
279
+     * @return \OC\Files\Node\Node[]
280
+     */
281
+    public function getById($id) {
282
+        $mountCache = $this->root->getUserMountCache();
283
+        if (strpos($this->getPath(), '/', 1) > 0) {
284
+            list(, $user) = explode('/', $this->getPath());
285
+        } else {
286
+            $user = null;
287
+        }
288
+        $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user);
289
+        $mounts = $this->root->getMountsIn($this->path);
290
+        $mounts[] = $this->root->getMount($this->path);
291
+        /** @var IMountPoint[] $folderMounts */
292
+        $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) {
293
+            return $mountPoint->getMountPoint();
294
+        }, $mounts), $mounts);
295
+
296
+        /** @var ICachedMountInfo[] $mountsContainingFile */
297
+        $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
298
+            return isset($folderMounts[$cachedMountInfo->getMountPoint()]);
299
+        }));
300
+
301
+        if (count($mountsContainingFile) === 0) {
302
+            return [];
303
+        }
304
+
305
+        $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) {
306
+            $mount = $folderMounts[$cachedMountInfo->getMountPoint()];
307
+            $cacheEntry = $mount->getStorage()->getCache()->get((int)$id);
308
+            if (!$cacheEntry) {
309
+                return null;
310
+            }
311
+
312
+            // cache jails will hide the "true" internal path
313
+            $internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/');
314
+            $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath()));
315
+            $pathRelativeToMount = ltrim($pathRelativeToMount, '/');
316
+            $absolutePath = rtrim($cachedMountInfo->getMountPoint() . $pathRelativeToMount, '/');
317
+            return $this->root->createNode($absolutePath, new \OC\Files\FileInfo(
318
+                $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
319
+                \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
320
+            ));
321
+        }, $mountsContainingFile);
322
+
323
+        $nodes = array_filter($nodes);
324
+
325
+        return array_filter($nodes, function (Node $node) {
326
+            return $this->getRelativePath($node->getPath());
327
+        });
328
+    }
329
+
330
+    public function getFreeSpace() {
331
+        return $this->view->free_space($this->path);
332
+    }
333
+
334
+    public function delete() {
335
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
336
+            $this->sendHooks(array('preDelete'));
337
+            $fileInfo = $this->getFileInfo();
338
+            $this->view->rmdir($this->path);
339
+            $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
340
+            $this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
341
+            $this->exists = false;
342
+        } else {
343
+            throw new NotPermittedException('No delete permission for path');
344
+        }
345
+    }
346
+
347
+    /**
348
+     * Add a suffix to the name in case the file exists
349
+     *
350
+     * @param string $name
351
+     * @return string
352
+     * @throws NotPermittedException
353
+     */
354
+    public function getNonExistingName($name) {
355
+        $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
356
+        return trim($this->getRelativePath($uniqueName), '/');
357
+    }
358
+
359
+    /**
360
+     * @param int $limit
361
+     * @param int $offset
362
+     * @return \OCP\Files\Node[]
363
+     */
364
+    public function getRecent($limit, $offset = 0) {
365
+        $mimetypeLoader = \OC::$server->getMimeTypeLoader();
366
+        $mounts = $this->root->getMountsIn($this->path);
367
+        $mounts[] = $this->getMountPoint();
368
+
369
+        $mounts = array_filter($mounts, function (IMountPoint $mount) {
370
+            return $mount->getStorage();
371
+        });
372
+        $storageIds = array_map(function (IMountPoint $mount) {
373
+            return $mount->getStorage()->getCache()->getNumericStorageId();
374
+        }, $mounts);
375
+        /** @var IMountPoint[] $mountMap */
376
+        $mountMap = array_combine($storageIds, $mounts);
377
+        $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER);
378
+
379
+        //todo look into options of filtering path based on storage id (only search in files/ for home storage, filter by share root for shared, etc)
380
+
381
+        $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
382
+        $query = $builder
383
+            ->select('f.*')
384
+            ->from('filecache', 'f')
385
+            ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)))
386
+            ->andWhere($builder->expr()->orX(
387
+            // handle non empty folders separate
388
+                $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
389
+                $builder->expr()->eq('f.size', new Literal(0))
390
+            ))
391
+            ->orderBy('f.mtime', 'DESC')
392
+            ->setMaxResults($limit)
393
+            ->setFirstResult($offset);
394
+
395
+        $result = $query->execute()->fetchAll();
396
+
397
+        $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
398
+            $mount = $mountMap[$entry['storage']];
399
+            $entry['internalPath'] = $entry['path'];
400
+            $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
401
+            $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']);
402
+            $path = $this->getAbsolutePath($mount, $entry['path']);
403
+            if (is_null($path)) {
404
+                return null;
405
+            }
406
+            $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount);
407
+            return $this->root->createNode($fileInfo->getPath(), $fileInfo);
408
+        }, $result));
409
+
410
+        return array_values(array_filter($files, function (Node $node) {
411
+            $relative = $this->getRelativePath($node->getPath());
412
+            return $relative !== null && $relative !== '/';
413
+        }));
414
+    }
415
+
416
+    private function getAbsolutePath(IMountPoint $mount, $path) {
417
+        $storage = $mount->getStorage();
418
+        if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) {
419
+            /** @var \OC\Files\Storage\Wrapper\Jail $storage */
420
+            $jailRoot = $storage->getUnjailedPath('');
421
+            $rootLength = strlen($jailRoot) + 1;
422
+            if ($path === $jailRoot) {
423
+                return $mount->getMountPoint();
424
+            } else if (substr($path, 0, $rootLength) === $jailRoot . '/') {
425
+                return $mount->getMountPoint() . substr($path, $rootLength);
426
+            } else {
427
+                return null;
428
+            }
429
+        } else {
430
+            return $mount->getMountPoint() . $path;
431
+        }
432
+    }
433 433
 }
Please login to merge, or discard this patch.
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -55,7 +55,7 @@  discard block
 block discarded – undo
55 55
 		if (!$this->isValidPath($path)) {
56 56
 			throw new NotPermittedException('Invalid path');
57 57
 		}
58
-		return $this->path . $this->normalizePath($path);
58
+		return $this->path.$this->normalizePath($path);
59 59
 	}
60 60
 
61 61
 	/**
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
 		}
69 69
 		if ($path === $this->path) {
70 70
 			return '/';
71
-		} else if (strpos($path, $this->path . '/') !== 0) {
71
+		} else if (strpos($path, $this->path.'/') !== 0) {
72 72
 			return null;
73 73
 		} else {
74 74
 			$path = substr($path, strlen($this->path));
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 	 * @return bool
84 84
 	 */
85 85
 	public function isSubNode($node) {
86
-		return strpos($node->getPath(), $this->path . '/') === 0;
86
+		return strpos($node->getPath(), $this->path.'/') === 0;
87 87
 	}
88 88
 
89 89
 	/**
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 	public function getDirectoryListing() {
96 96
 		$folderContent = $this->view->getDirectoryContent($this->path);
97 97
 
98
-		return array_map(function (FileInfo $info) {
98
+		return array_map(function(FileInfo $info) {
99 99
 			if ($info->getMimetype() === 'httpd/unix-directory') {
100 100
 				return new Folder($this->root, $this->view, $info->getPath(), $info);
101 101
 			} else {
@@ -196,7 +196,7 @@  discard block
 block discarded – undo
196 196
 	 */
197 197
 	public function search($query) {
198 198
 		if (is_string($query)) {
199
-			return $this->searchCommon('search', array('%' . $query . '%'));
199
+			return $this->searchCommon('search', array('%'.$query.'%'));
200 200
 		} else {
201 201
 			return $this->searchCommon('searchQuery', array($query));
202 202
 		}
@@ -236,7 +236,7 @@  discard block
 block discarded – undo
236 236
 		$internalPath = $mount->getInternalPath($this->path);
237 237
 		$internalPath = rtrim($internalPath, '/');
238 238
 		if ($internalPath !== '') {
239
-			$internalPath = $internalPath . '/';
239
+			$internalPath = $internalPath.'/';
240 240
 		}
241 241
 		$internalRootLength = strlen($internalPath);
242 242
 
@@ -248,7 +248,7 @@  discard block
 block discarded – undo
248 248
 				$result['internalPath'] = $result['path'];
249 249
 				$result['path'] = substr($result['path'], $internalRootLength);
250 250
 				$result['storage'] = $storage;
251
-				$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
251
+				$files[] = new \OC\Files\FileInfo($this->path.'/'.$result['path'], $storage, $result['internalPath'], $result, $mount);
252 252
 			}
253 253
 		}
254 254
 
@@ -262,14 +262,14 @@  discard block
 block discarded – undo
262 262
 				$results = call_user_func_array(array($cache, $method), $args);
263 263
 				foreach ($results as $result) {
264 264
 					$result['internalPath'] = $result['path'];
265
-					$result['path'] = $relativeMountPoint . $result['path'];
265
+					$result['path'] = $relativeMountPoint.$result['path'];
266 266
 					$result['storage'] = $storage;
267
-					$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
267
+					$files[] = new \OC\Files\FileInfo($this->path.'/'.$result['path'], $storage, $result['internalPath'], $result, $mount);
268 268
 				}
269 269
 			}
270 270
 		}
271 271
 
272
-		return array_map(function (FileInfo $file) {
272
+		return array_map(function(FileInfo $file) {
273 273
 			return $this->createNode($file->getPath(), $file);
274 274
 		}, $files);
275 275
 	}
@@ -285,16 +285,16 @@  discard block
 block discarded – undo
285 285
 		} else {
286 286
 			$user = null;
287 287
 		}
288
-		$mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user);
288
+		$mountsContainingFile = $mountCache->getMountsForFileId((int) $id, $user);
289 289
 		$mounts = $this->root->getMountsIn($this->path);
290 290
 		$mounts[] = $this->root->getMount($this->path);
291 291
 		/** @var IMountPoint[] $folderMounts */
292
-		$folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) {
292
+		$folderMounts = array_combine(array_map(function(IMountPoint $mountPoint) {
293 293
 			return $mountPoint->getMountPoint();
294 294
 		}, $mounts), $mounts);
295 295
 
296 296
 		/** @var ICachedMountInfo[] $mountsContainingFile */
297
-		$mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
297
+		$mountsContainingFile = array_values(array_filter($mountsContainingFile, function(ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
298 298
 			return isset($folderMounts[$cachedMountInfo->getMountPoint()]);
299 299
 		}));
300 300
 
@@ -302,18 +302,18 @@  discard block
 block discarded – undo
302 302
 			return [];
303 303
 		}
304 304
 
305
-		$nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) {
305
+		$nodes = array_map(function(ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) {
306 306
 			$mount = $folderMounts[$cachedMountInfo->getMountPoint()];
307
-			$cacheEntry = $mount->getStorage()->getCache()->get((int)$id);
307
+			$cacheEntry = $mount->getStorage()->getCache()->get((int) $id);
308 308
 			if (!$cacheEntry) {
309 309
 				return null;
310 310
 			}
311 311
 
312 312
 			// cache jails will hide the "true" internal path
313
-			$internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/');
313
+			$internalPath = ltrim($cachedMountInfo->getRootInternalPath().'/'.$cacheEntry->getPath(), '/');
314 314
 			$pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath()));
315 315
 			$pathRelativeToMount = ltrim($pathRelativeToMount, '/');
316
-			$absolutePath = rtrim($cachedMountInfo->getMountPoint() . $pathRelativeToMount, '/');
316
+			$absolutePath = rtrim($cachedMountInfo->getMountPoint().$pathRelativeToMount, '/');
317 317
 			return $this->root->createNode($absolutePath, new \OC\Files\FileInfo(
318 318
 				$absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
319 319
 				\OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
@@ -322,7 +322,7 @@  discard block
 block discarded – undo
322 322
 
323 323
 		$nodes = array_filter($nodes);
324 324
 
325
-		return array_filter($nodes, function (Node $node) {
325
+		return array_filter($nodes, function(Node $node) {
326 326
 			return $this->getRelativePath($node->getPath());
327 327
 		});
328 328
 	}
@@ -366,10 +366,10 @@  discard block
 block discarded – undo
366 366
 		$mounts = $this->root->getMountsIn($this->path);
367 367
 		$mounts[] = $this->getMountPoint();
368 368
 
369
-		$mounts = array_filter($mounts, function (IMountPoint $mount) {
369
+		$mounts = array_filter($mounts, function(IMountPoint $mount) {
370 370
 			return $mount->getStorage();
371 371
 		});
372
-		$storageIds = array_map(function (IMountPoint $mount) {
372
+		$storageIds = array_map(function(IMountPoint $mount) {
373 373
 			return $mount->getStorage()->getCache()->getNumericStorageId();
374 374
 		}, $mounts);
375 375
 		/** @var IMountPoint[] $mountMap */
@@ -394,7 +394,7 @@  discard block
 block discarded – undo
394 394
 
395 395
 		$result = $query->execute()->fetchAll();
396 396
 
397
-		$files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
397
+		$files = array_filter(array_map(function(array $entry) use ($mountMap, $mimetypeLoader) {
398 398
 			$mount = $mountMap[$entry['storage']];
399 399
 			$entry['internalPath'] = $entry['path'];
400 400
 			$entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
@@ -407,7 +407,7 @@  discard block
 block discarded – undo
407 407
 			return $this->root->createNode($fileInfo->getPath(), $fileInfo);
408 408
 		}, $result));
409 409
 
410
-		return array_values(array_filter($files, function (Node $node) {
410
+		return array_values(array_filter($files, function(Node $node) {
411 411
 			$relative = $this->getRelativePath($node->getPath());
412 412
 			return $relative !== null && $relative !== '/';
413 413
 		}));
@@ -421,13 +421,13 @@  discard block
 block discarded – undo
421 421
 			$rootLength = strlen($jailRoot) + 1;
422 422
 			if ($path === $jailRoot) {
423 423
 				return $mount->getMountPoint();
424
-			} else if (substr($path, 0, $rootLength) === $jailRoot . '/') {
425
-				return $mount->getMountPoint() . substr($path, $rootLength);
424
+			} else if (substr($path, 0, $rootLength) === $jailRoot.'/') {
425
+				return $mount->getMountPoint().substr($path, $rootLength);
426 426
 			} else {
427 427
 				return null;
428 428
 			}
429 429
 		} else {
430
-			return $mount->getMountPoint() . $path;
430
+			return $mount->getMountPoint().$path;
431 431
 		}
432 432
 	}
433 433
 }
Please login to merge, or discard this patch.