2013-09-01 21:47:48 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
namespace OC\Files\Node;
|
|
|
|
|
|
|
|
use OC\Files\Cache\Cache;
|
2013-09-10 21:44:23 +04:00
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\NotPermittedException;
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2013-09-10 21:44:23 +04:00
|
|
|
class Folder extends Node implements \OCP\Files\Folder {
|
2013-09-01 21:47:48 +04:00
|
|
|
/**
|
|
|
|
* @param string $path path relative to the folder
|
|
|
|
* @return string
|
2013-09-10 21:44:23 +04:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
2014-08-05 18:58:10 +04:00
|
|
|
if ($path === $this->path) {
|
|
|
|
return '/';
|
|
|
|
} else if (strpos($path, $this->path . '/') !== 0) {
|
2014-08-06 14:06:41 +04:00
|
|
|
return null;
|
2013-09-01 21:47:48 +04:00
|
|
|
} else {
|
|
|
|
$path = substr($path, strlen($this->path));
|
2014-08-05 18:58:10 +04:00
|
|
|
return $this->normalizePath($path);
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2013-09-10 21:44:23 +04:00
|
|
|
* @throws \OCP\Files\NotFoundException
|
2013-09-01 21:47:48 +04:00
|
|
|
* @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
|
2013-09-10 21:44:23 +04:00
|
|
|
* @throws \OCP\Files\NotFoundException
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
|
|
|
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
|
2013-09-10 21:44:23 +04:00
|
|
|
* @return \OC\Files\Node\Folder
|
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
|
|
|
public function newFolder($path) {
|
2014-11-25 18:28:41 +03:00
|
|
|
if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
|
2013-09-01 21:47:48 +04:00
|
|
|
$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
|
2013-09-10 21:44:23 +04:00
|
|
|
* @return \OC\Files\Node\File
|
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
|
|
|
public function newFile($path) {
|
2014-11-25 18:28:41 +03:00
|
|
|
if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
|
2013-09-01 21:47:48 +04:00
|
|
|
$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
|
2013-09-10 21:44:23 +04:00
|
|
|
* @return \OC\Files\Node\Node[]
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
|
|
|
public function search($query) {
|
2014-12-12 13:18:35 +03:00
|
|
|
return $this->searchCommon('search', array('%' . $query . '%'));
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search for files by mimetype
|
|
|
|
*
|
|
|
|
* @param string $mimetype
|
|
|
|
* @return Node[]
|
|
|
|
*/
|
|
|
|
public function searchByMime($mimetype) {
|
2014-12-12 13:18:35 +03:00
|
|
|
return $this->searchCommon('searchByMime', array($mimetype));
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|
|
|
|
|
2014-12-04 16:01:15 +03:00
|
|
|
/**
|
|
|
|
* search for files by tag
|
|
|
|
*
|
2014-12-12 13:18:35 +03:00
|
|
|
* @param string|int $tag name or tag id
|
|
|
|
* @param string $userId owner of the tags
|
2014-12-04 16:01:15 +03:00
|
|
|
* @return Node[]
|
|
|
|
*/
|
2014-12-12 13:18:35 +03:00
|
|
|
public function searchByTag($tag, $userId) {
|
|
|
|
return $this->searchCommon('searchByTag', array($tag, $userId));
|
2014-12-04 16:01:15 +03:00
|
|
|
}
|
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
/**
|
2014-12-12 13:18:35 +03:00
|
|
|
* @param string $method cache method
|
|
|
|
* @param array $args call args
|
2013-09-10 21:44:23 +04:00
|
|
|
* @return \OC\Files\Node\Node[]
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
2014-12-12 13:18:35 +03:00
|
|
|
private function searchCommon($method, $args) {
|
2013-09-01 21:47:48 +04:00
|
|
|
$files = array();
|
|
|
|
$rootLength = strlen($this->path);
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
*/
|
|
|
|
list($storage, $internalPath) = $this->view->resolvePath($this->path);
|
2015-01-12 21:33:00 +03:00
|
|
|
$internalPath = rtrim($internalPath, '/') . '/';
|
2013-09-01 21:47:48 +04:00
|
|
|
$internalRootLength = strlen($internalPath);
|
|
|
|
|
|
|
|
$cache = $storage->getCache('');
|
|
|
|
|
2014-12-12 13:18:35 +03:00
|
|
|
$results = call_user_func_array(array($cache, $method), $args);
|
2013-09-01 21:47:48 +04:00
|
|
|
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);
|
2014-12-12 13:18:35 +03:00
|
|
|
$results = call_user_func_array(array($cache, $method), $args);
|
2013-09-01 21:47:48 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param int $id
|
2013-09-10 21:44:23 +04:00
|
|
|
* @return \OC\Files\Node\Node[]
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
|
|
|
public function getById($id) {
|
2014-08-05 18:58:10 +04:00
|
|
|
$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) {
|
|
|
|
/**
|
2014-11-24 17:54:42 +03:00
|
|
|
* @var \OC\Files\Mount\MountPoint $mount
|
2014-08-05 18:58:10 +04:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|
|
|
|
}
|
2014-08-05 18:58:10 +04:00
|
|
|
return $nodes;
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getFreeSpace() {
|
|
|
|
return $this->view->free_space($this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete() {
|
2014-11-25 18:28:41 +03:00
|
|
|
if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
|
2013-09-01 21:47:48 +04:00
|
|
|
$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
|
2013-09-10 21:44:23 +04:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2013-09-01 21:47:48 +04:00
|
|
|
* @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
|
2013-09-10 21:44:23 +04:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2013-09-01 21:47:48 +04:00
|
|
|
* @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();
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 17:00:36 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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), '/');
|
|
|
|
}
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|