folder.php 11.3 KB
Newer Older
1
2
<?php
/**
3
4
5
6
 * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
7
 */
8

9
10
11
namespace OC\Files\Node;

use OC\Files\Cache\Cache;
12
13
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
14

15
class Folder extends Node implements \OCP\Files\Folder {
16
17
18
	/**
	 * @param string $path path relative to the folder
	 * @return string
19
	 * @throws \OCP\Files\NotPermittedException
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
	 */
	public function getFullPath($path) {
		if (!$this->isValidPath($path)) {
			throw new NotPermittedException();
		}
		return $this->path . $this->normalizePath($path);
	}

	/**
	 * @param string $path
	 * @return string
	 */
	public function getRelativePath($path) {
		if ($this->path === '' or $this->path === '/') {
			return $this->normalizePath($path);
		}
Robin Appelman's avatar
Robin Appelman committed
36
37
38
		if ($path === $this->path) {
			return '/';
		} else if (strpos($path, $this->path . '/') !== 0) {
39
			return null;
40
41
		} else {
			$path = substr($path, strlen($this->path));
Robin Appelman's avatar
Robin Appelman committed
42
			return $this->normalizePath($path);
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
		}
	}

	/**
	 * check if a node is a (grand-)child of the folder
	 *
	 * @param \OC\Files\Node\Node $node
	 * @return bool
	 */
	public function isSubNode($node) {
		return strpos($node->getPath(), $this->path . '/') === 0;
	}

	/**
	 * get the content of this directory
	 *
59
	 * @throws \OCP\Files\NotFoundException
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	 * @return Node[]
	 */
	public function getDirectoryListing() {
		$result = array();

		/**
		 * @var \OC\Files\Storage\Storage $storage
		 */
		list($storage, $internalPath) = $this->view->resolvePath($this->path);
		if ($storage) {
			$cache = $storage->getCache($internalPath);

			//trigger cache update check
			$this->view->getFileInfo($this->path);

			$files = $cache->getFolderContents($internalPath);
		} else {
			$files = array();
		}

		//add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
		$mounts = $this->root->getMountsIn($this->path);
		$dirLength = strlen($this->path);
		foreach ($mounts as $mount) {
			$subStorage = $mount->getStorage();
			if ($subStorage) {
				$subCache = $subStorage->getCache('');

				if ($subCache->getStatus('') === Cache::NOT_FOUND) {
					$subScanner = $subStorage->getScanner('');
					$subScanner->scanFile('');
				}

				$rootEntry = $subCache->get('');
				if ($rootEntry) {
					$relativePath = trim(substr($mount->getMountPoint(), $dirLength), '/');
					if ($pos = strpos($relativePath, '/')) {
						//mountpoint inside subfolder add size to the correct folder
						$entryName = substr($relativePath, 0, $pos);
						foreach ($files as &$entry) {
							if ($entry['name'] === $entryName) {
								if ($rootEntry['size'] >= 0) {
									$entry['size'] += $rootEntry['size'];
								} else {
									$entry['size'] = -1;
								}
							}
						}
					} else { //mountpoint in this folder, add an entry for it
						$rootEntry['name'] = $relativePath;
						$rootEntry['storageObject'] = $subStorage;

						//remove any existing entry with the same name
						foreach ($files as $i => $file) {
							if ($file['name'] === $rootEntry['name']) {
								$files[$i] = null;
								break;
							}
						}
						$files[] = $rootEntry;
					}
				}
			}
		}

		foreach ($files as $file) {
			if ($file) {
				$node = $this->createNode($this->path . '/' . $file['name'], $file);
				$result[] = $node;
			}
		}

		return $result;
	}

	/**
	 * @param string $path
	 * @param array $info
	 * @return File|Folder
	 */
	protected function createNode($path, $info = array()) {
		if (!isset($info['mimetype'])) {
			$isDir = $this->view->is_dir($path);
		} else {
			$isDir = $info['mimetype'] === 'httpd/unix-directory';
		}
		if ($isDir) {
			return new Folder($this->root, $this->view, $path);
		} else {
			return new File($this->root, $this->view, $path);
		}
	}

	/**
	 * Get the node at $path
	 *
	 * @param string $path
	 * @return \OC\Files\Node\Node
158
	 * @throws \OCP\Files\NotFoundException
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
	 */
	public function get($path) {
		return $this->root->get($this->getFullPath($path));
	}

	/**
	 * @param string $path
	 * @return bool
	 */
	public function nodeExists($path) {
		try {
			$this->get($path);
			return true;
		} catch (NotFoundException $e) {
			return false;
		}
	}

	/**
	 * @param string $path
179
180
	 * @return \OC\Files\Node\Folder
	 * @throws \OCP\Files\NotPermittedException
181
182
	 */
	public function newFolder($path) {
183
		if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
			$fullPath = $this->getFullPath($path);
			$nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
			$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
			$this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
			$this->view->mkdir($fullPath);
			$node = new Folder($this->root, $this->view, $fullPath);
			$this->root->emit('\OC\Files', 'postWrite', array($node));
			$this->root->emit('\OC\Files', 'postCreate', array($node));
			return $node;
		} else {
			throw new NotPermittedException();
		}
	}

	/**
	 * @param string $path
200
201
	 * @return \OC\Files\Node\File
	 * @throws \OCP\Files\NotPermittedException
202
203
	 */
	public function newFile($path) {
204
		if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
			$fullPath = $this->getFullPath($path);
			$nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
			$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
			$this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
			$this->view->touch($fullPath);
			$node = new File($this->root, $this->view, $fullPath);
			$this->root->emit('\OC\Files', 'postWrite', array($node));
			$this->root->emit('\OC\Files', 'postCreate', array($node));
			return $node;
		} else {
			throw new NotPermittedException();
		}
	}

	/**
	 * search for files with the name matching $query
	 *
	 * @param string $query
223
	 * @return \OC\Files\Node\Node[]
224
225
	 */
	public function search($query) {
226
		return $this->searchCommon('search', array('%' . $query . '%'));
227
228
229
230
231
232
233
234
235
	}

	/**
	 * search for files by mimetype
	 *
	 * @param string $mimetype
	 * @return Node[]
	 */
	public function searchByMime($mimetype) {
236
		return $this->searchCommon('searchByMime', array($mimetype));
237
238
	}

239
240
241
	/**
	 * search for files by tag
	 *
242
243
	 * @param string|int $tag name or tag id
	 * @param string $userId owner of the tags
244
245
	 * @return Node[]
	 */
246
247
	public function searchByTag($tag, $userId) {
		return $this->searchCommon('searchByTag', array($tag, $userId));
248
249
	}

250
	/**
251
252
	 * @param string $method cache method
	 * @param array $args call args
253
	 * @return \OC\Files\Node\Node[]
254
	 */
255
	private function searchCommon($method, $args) {
256
257
258
259
260
261
		$files = array();
		$rootLength = strlen($this->path);
		/**
		 * @var \OC\Files\Storage\Storage $storage
		 */
		list($storage, $internalPath) = $this->view->resolvePath($this->path);
262
		$internalPath = rtrim($internalPath, '/') . '/';
263
264
265
266
		$internalRootLength = strlen($internalPath);

		$cache = $storage->getCache('');

267
		$results = call_user_func_array(array($cache, $method), $args);
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
		foreach ($results as $result) {
			if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
				$result['internalPath'] = $result['path'];
				$result['path'] = substr($result['path'], $internalRootLength);
				$result['storage'] = $storage;
				$files[] = $result;
			}
		}

		$mounts = $this->root->getMountsIn($this->path);
		foreach ($mounts as $mount) {
			$storage = $mount->getStorage();
			if ($storage) {
				$cache = $storage->getCache('');

				$relativeMountPoint = substr($mount->getMountPoint(), $rootLength);
284
				$results = call_user_func_array(array($cache, $method), $args);
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
				foreach ($results as $result) {
					$result['internalPath'] = $result['path'];
					$result['path'] = $relativeMountPoint . $result['path'];
					$result['storage'] = $storage;
					$files[] = $result;
				}
			}
		}

		$result = array();
		foreach ($files as $file) {
			$result[] = $this->createNode($this->normalizePath($this->path . '/' . $file['path']), $file);
		}

		return $result;
	}

	/**
303
	 * @param int $id
304
	 * @return \OC\Files\Node\Node[]
305
306
	 */
	public function getById($id) {
Robin Appelman's avatar
Robin Appelman committed
307
308
309
310
311
312
313
314
315
		$mounts = $this->root->getMountsIn($this->path);
		$mounts[] = $this->root->getMount($this->path);
		// reverse the array so we start with the storage this view is in
		// which is the most likely to contain the file we're looking for
		$mounts = array_reverse($mounts);

		$nodes = array();
		foreach ($mounts as $mount) {
			/**
316
			 * @var \OC\Files\Mount\MountPoint $mount
Robin Appelman's avatar
Robin Appelman committed
317
318
319
320
321
322
323
324
325
326
			 */
			if ($mount->getStorage()) {
				$cache = $mount->getStorage()->getCache();
				$internalPath = $cache->getPathById($id);
				if (is_string($internalPath)) {
					$fullPath = $mount->getMountPoint() . $internalPath;
					if (!is_null($path = $this->getRelativePath($fullPath))) {
						$nodes[] = $this->get($path);
					}
				}
327
328
			}
		}
Robin Appelman's avatar
Robin Appelman committed
329
		return $nodes;
330
331
332
333
334
335
336
	}

	public function getFreeSpace() {
		return $this->view->free_space($this->path);
	}

	public function delete() {
337
		if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
338
339
340
341
342
343
344
345
346
347
348
349
			$this->sendHooks(array('preDelete'));
			$this->view->rmdir($this->path);
			$nonExisting = new NonExistingFolder($this->root, $this->view, $this->path);
			$this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
			$this->exists = false;
		} else {
			throw new NotPermittedException();
		}
	}

	/**
	 * @param string $targetPath
350
	 * @throws \OCP\Files\NotPermittedException
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
	 * @return \OC\Files\Node\Node
	 */
	public function copy($targetPath) {
		$targetPath = $this->normalizePath($targetPath);
		$parent = $this->root->get(dirname($targetPath));
		if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
			$nonExisting = new NonExistingFolder($this->root, $this->view, $targetPath);
			$this->root->emit('\OC\Files', 'preCopy', array($this, $nonExisting));
			$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
			$this->view->copy($this->path, $targetPath);
			$targetNode = $this->root->get($targetPath);
			$this->root->emit('\OC\Files', 'postCopy', array($this, $targetNode));
			$this->root->emit('\OC\Files', 'postWrite', array($targetNode));
			return $targetNode;
		} else {
			throw new NotPermittedException();
		}
	}

	/**
	 * @param string $targetPath
372
	 * @throws \OCP\Files\NotPermittedException
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
	 * @return \OC\Files\Node\Node
	 */
	public function move($targetPath) {
		$targetPath = $this->normalizePath($targetPath);
		$parent = $this->root->get(dirname($targetPath));
		if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
			$nonExisting = new NonExistingFolder($this->root, $this->view, $targetPath);
			$this->root->emit('\OC\Files', 'preRename', array($this, $nonExisting));
			$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
			$this->view->rename($this->path, $targetPath);
			$targetNode = $this->root->get($targetPath);
			$this->root->emit('\OC\Files', 'postRename', array($this, $targetNode));
			$this->root->emit('\OC\Files', 'postWrite', array($targetNode));
			$this->path = $targetPath;
			return $targetNode;
		} else {
			throw new NotPermittedException();
		}
	}
392
393
394
395
396
397
398
399
400
401
402
403

	/**
	 * Add a suffix to the name in case the file exists
	 *
	 * @param string $name
	 * @return string
	 * @throws NotPermittedException
	 */
	public function getNonExistingName($name) {
		$uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
		return trim($this->getRelativePath($uniqueName), '/');
	}
404
}