@@ -32,191 +32,191 @@ |
||
| 32 | 32 | |
| 33 | 33 | class Quota extends Wrapper { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @var int $quota |
|
| 37 | - */ |
|
| 38 | - protected $quota; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var string $sizeRoot |
|
| 42 | - */ |
|
| 43 | - protected $sizeRoot; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @param array $parameters |
|
| 47 | - */ |
|
| 48 | - public function __construct($parameters) { |
|
| 49 | - parent::__construct($parameters); |
|
| 50 | - $this->quota = $parameters['quota']; |
|
| 51 | - $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @return int quota value |
|
| 56 | - */ |
|
| 57 | - public function getQuota() { |
|
| 58 | - return $this->quota; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param string $path |
|
| 63 | - * @param \OC\Files\Storage\Storage $storage |
|
| 64 | - */ |
|
| 65 | - protected function getSize($path, $storage = null) { |
|
| 66 | - if (is_null($storage)) { |
|
| 67 | - $cache = $this->getCache(); |
|
| 68 | - } else { |
|
| 69 | - $cache = $storage->getCache(); |
|
| 70 | - } |
|
| 71 | - $data = $cache->get($path); |
|
| 72 | - if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
| 73 | - return $data['size']; |
|
| 74 | - } else { |
|
| 75 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Get free space as limited by the quota |
|
| 81 | - * |
|
| 82 | - * @param string $path |
|
| 83 | - * @return int |
|
| 84 | - */ |
|
| 85 | - public function free_space($path) { |
|
| 86 | - if ($this->quota < 0 || strpos($path, 'cache') === 0 || strpos($path, 'uploads') === 0) { |
|
| 87 | - return $this->storage->free_space($path); |
|
| 88 | - } else { |
|
| 89 | - $used = $this->getSize($this->sizeRoot); |
|
| 90 | - if ($used < 0) { |
|
| 91 | - return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 92 | - } else { |
|
| 93 | - $free = $this->storage->free_space($path); |
|
| 94 | - $quotaFree = max($this->quota - $used, 0); |
|
| 95 | - // if free space is known |
|
| 96 | - if ($free >= 0) { |
|
| 97 | - $free = min($free, $quotaFree); |
|
| 98 | - } else { |
|
| 99 | - $free = $quotaFree; |
|
| 100 | - } |
|
| 101 | - return $free; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * see http://php.net/manual/en/function.file_put_contents.php |
|
| 108 | - * |
|
| 109 | - * @param string $path |
|
| 110 | - * @param string $data |
|
| 111 | - * @return bool |
|
| 112 | - */ |
|
| 113 | - public function file_put_contents($path, $data) { |
|
| 114 | - $free = $this->free_space($path); |
|
| 115 | - if ($free < 0 or strlen($data) < $free) { |
|
| 116 | - return $this->storage->file_put_contents($path, $data); |
|
| 117 | - } else { |
|
| 118 | - return false; |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * see http://php.net/manual/en/function.copy.php |
|
| 124 | - * |
|
| 125 | - * @param string $source |
|
| 126 | - * @param string $target |
|
| 127 | - * @return bool |
|
| 128 | - */ |
|
| 129 | - public function copy($source, $target) { |
|
| 130 | - $free = $this->free_space($target); |
|
| 131 | - if ($free < 0 or $this->getSize($source) < $free) { |
|
| 132 | - return $this->storage->copy($source, $target); |
|
| 133 | - } else { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * see http://php.net/manual/en/function.fopen.php |
|
| 140 | - * |
|
| 141 | - * @param string $path |
|
| 142 | - * @param string $mode |
|
| 143 | - * @return resource |
|
| 144 | - */ |
|
| 145 | - public function fopen($path, $mode) { |
|
| 146 | - $source = $this->storage->fopen($path, $mode); |
|
| 147 | - |
|
| 148 | - // don't apply quota for part files |
|
| 149 | - if (!$this->isPartFile($path)) { |
|
| 150 | - $free = $this->free_space($path); |
|
| 151 | - if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
| 152 | - // only apply quota for files, not metadata, trash or others |
|
| 153 | - if (strpos(ltrim($path, '/'), 'files/') === 0) { |
|
| 154 | - return \OC\Files\Stream\Quota::wrap($source, $free); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - return $source; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Checks whether the given path is a part file |
|
| 163 | - * |
|
| 164 | - * @param string $path Path that may identify a .part file |
|
| 165 | - * @return string File path without .part extension |
|
| 166 | - * @note this is needed for reusing keys |
|
| 167 | - */ |
|
| 168 | - private function isPartFile($path) { |
|
| 169 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 170 | - |
|
| 171 | - return ($extension === 'part'); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @param IStorage $sourceStorage |
|
| 176 | - * @param string $sourceInternalPath |
|
| 177 | - * @param string $targetInternalPath |
|
| 178 | - * @return bool |
|
| 179 | - */ |
|
| 180 | - public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 181 | - $free = $this->free_space($targetInternalPath); |
|
| 182 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 183 | - return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 184 | - } else { |
|
| 185 | - return false; |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @param IStorage $sourceStorage |
|
| 191 | - * @param string $sourceInternalPath |
|
| 192 | - * @param string $targetInternalPath |
|
| 193 | - * @return bool |
|
| 194 | - */ |
|
| 195 | - public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 196 | - $free = $this->free_space($targetInternalPath); |
|
| 197 | - if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 198 | - return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 199 | - } else { |
|
| 200 | - return false; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - public function mkdir($path) { |
|
| 205 | - $free = $this->free_space($path); |
|
| 206 | - if ($free === 0.0) { |
|
| 207 | - return false; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - return parent::mkdir($path); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - public function touch($path, $mtime = null) { |
|
| 214 | - $free = $this->free_space($path); |
|
| 215 | - if ($free === 0.0) { |
|
| 216 | - return false; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - return parent::touch($path, $mtime); |
|
| 220 | - } |
|
| 35 | + /** |
|
| 36 | + * @var int $quota |
|
| 37 | + */ |
|
| 38 | + protected $quota; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var string $sizeRoot |
|
| 42 | + */ |
|
| 43 | + protected $sizeRoot; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @param array $parameters |
|
| 47 | + */ |
|
| 48 | + public function __construct($parameters) { |
|
| 49 | + parent::__construct($parameters); |
|
| 50 | + $this->quota = $parameters['quota']; |
|
| 51 | + $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : ''; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @return int quota value |
|
| 56 | + */ |
|
| 57 | + public function getQuota() { |
|
| 58 | + return $this->quota; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param string $path |
|
| 63 | + * @param \OC\Files\Storage\Storage $storage |
|
| 64 | + */ |
|
| 65 | + protected function getSize($path, $storage = null) { |
|
| 66 | + if (is_null($storage)) { |
|
| 67 | + $cache = $this->getCache(); |
|
| 68 | + } else { |
|
| 69 | + $cache = $storage->getCache(); |
|
| 70 | + } |
|
| 71 | + $data = $cache->get($path); |
|
| 72 | + if ($data instanceof ICacheEntry and isset($data['size'])) { |
|
| 73 | + return $data['size']; |
|
| 74 | + } else { |
|
| 75 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Get free space as limited by the quota |
|
| 81 | + * |
|
| 82 | + * @param string $path |
|
| 83 | + * @return int |
|
| 84 | + */ |
|
| 85 | + public function free_space($path) { |
|
| 86 | + if ($this->quota < 0 || strpos($path, 'cache') === 0 || strpos($path, 'uploads') === 0) { |
|
| 87 | + return $this->storage->free_space($path); |
|
| 88 | + } else { |
|
| 89 | + $used = $this->getSize($this->sizeRoot); |
|
| 90 | + if ($used < 0) { |
|
| 91 | + return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
|
| 92 | + } else { |
|
| 93 | + $free = $this->storage->free_space($path); |
|
| 94 | + $quotaFree = max($this->quota - $used, 0); |
|
| 95 | + // if free space is known |
|
| 96 | + if ($free >= 0) { |
|
| 97 | + $free = min($free, $quotaFree); |
|
| 98 | + } else { |
|
| 99 | + $free = $quotaFree; |
|
| 100 | + } |
|
| 101 | + return $free; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * see http://php.net/manual/en/function.file_put_contents.php |
|
| 108 | + * |
|
| 109 | + * @param string $path |
|
| 110 | + * @param string $data |
|
| 111 | + * @return bool |
|
| 112 | + */ |
|
| 113 | + public function file_put_contents($path, $data) { |
|
| 114 | + $free = $this->free_space($path); |
|
| 115 | + if ($free < 0 or strlen($data) < $free) { |
|
| 116 | + return $this->storage->file_put_contents($path, $data); |
|
| 117 | + } else { |
|
| 118 | + return false; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * see http://php.net/manual/en/function.copy.php |
|
| 124 | + * |
|
| 125 | + * @param string $source |
|
| 126 | + * @param string $target |
|
| 127 | + * @return bool |
|
| 128 | + */ |
|
| 129 | + public function copy($source, $target) { |
|
| 130 | + $free = $this->free_space($target); |
|
| 131 | + if ($free < 0 or $this->getSize($source) < $free) { |
|
| 132 | + return $this->storage->copy($source, $target); |
|
| 133 | + } else { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * see http://php.net/manual/en/function.fopen.php |
|
| 140 | + * |
|
| 141 | + * @param string $path |
|
| 142 | + * @param string $mode |
|
| 143 | + * @return resource |
|
| 144 | + */ |
|
| 145 | + public function fopen($path, $mode) { |
|
| 146 | + $source = $this->storage->fopen($path, $mode); |
|
| 147 | + |
|
| 148 | + // don't apply quota for part files |
|
| 149 | + if (!$this->isPartFile($path)) { |
|
| 150 | + $free = $this->free_space($path); |
|
| 151 | + if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') { |
|
| 152 | + // only apply quota for files, not metadata, trash or others |
|
| 153 | + if (strpos(ltrim($path, '/'), 'files/') === 0) { |
|
| 154 | + return \OC\Files\Stream\Quota::wrap($source, $free); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + return $source; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Checks whether the given path is a part file |
|
| 163 | + * |
|
| 164 | + * @param string $path Path that may identify a .part file |
|
| 165 | + * @return string File path without .part extension |
|
| 166 | + * @note this is needed for reusing keys |
|
| 167 | + */ |
|
| 168 | + private function isPartFile($path) { |
|
| 169 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 170 | + |
|
| 171 | + return ($extension === 'part'); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @param IStorage $sourceStorage |
|
| 176 | + * @param string $sourceInternalPath |
|
| 177 | + * @param string $targetInternalPath |
|
| 178 | + * @return bool |
|
| 179 | + */ |
|
| 180 | + public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 181 | + $free = $this->free_space($targetInternalPath); |
|
| 182 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 183 | + return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 184 | + } else { |
|
| 185 | + return false; |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @param IStorage $sourceStorage |
|
| 191 | + * @param string $sourceInternalPath |
|
| 192 | + * @param string $targetInternalPath |
|
| 193 | + * @return bool |
|
| 194 | + */ |
|
| 195 | + public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 196 | + $free = $this->free_space($targetInternalPath); |
|
| 197 | + if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) { |
|
| 198 | + return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 199 | + } else { |
|
| 200 | + return false; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + public function mkdir($path) { |
|
| 205 | + $free = $this->free_space($path); |
|
| 206 | + if ($free === 0.0) { |
|
| 207 | + return false; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + return parent::mkdir($path); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + public function touch($path, $mtime = null) { |
|
| 214 | + $free = $this->free_space($path); |
|
| 215 | + if ($free === 0.0) { |
|
| 216 | + return false; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + return parent::touch($path, $mtime); |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | 222 | } |
@@ -37,429 +37,429 @@ |
||
| 37 | 37 | use OCP\Files\Search\ISearchOperator; |
| 38 | 38 | |
| 39 | 39 | class Folder extends Node implements \OCP\Files\Folder { |
| 40 | - /** |
|
| 41 | - * Creates a Folder that represents a non-existing path |
|
| 42 | - * |
|
| 43 | - * @param string $path path |
|
| 44 | - * @return string non-existing node class |
|
| 45 | - */ |
|
| 46 | - protected function createNonExistingNode($path) { |
|
| 47 | - return new NonExistingFolder($this->root, $this->view, $path); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param string $path path relative to the folder |
|
| 52 | - * @return string |
|
| 53 | - * @throws \OCP\Files\NotPermittedException |
|
| 54 | - */ |
|
| 55 | - public function getFullPath($path) { |
|
| 56 | - if (!$this->isValidPath($path)) { |
|
| 57 | - throw new NotPermittedException('Invalid path'); |
|
| 58 | - } |
|
| 59 | - return $this->path . $this->normalizePath($path); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param string $path |
|
| 64 | - * @return string |
|
| 65 | - */ |
|
| 66 | - public function getRelativePath($path) { |
|
| 67 | - if ($this->path === '' or $this->path === '/') { |
|
| 68 | - return $this->normalizePath($path); |
|
| 69 | - } |
|
| 70 | - if ($path === $this->path) { |
|
| 71 | - return '/'; |
|
| 72 | - } else if (strpos($path, $this->path . '/') !== 0) { |
|
| 73 | - return null; |
|
| 74 | - } else { |
|
| 75 | - $path = substr($path, strlen($this->path)); |
|
| 76 | - return $this->normalizePath($path); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * check if a node is a (grand-)child of the folder |
|
| 82 | - * |
|
| 83 | - * @param \OC\Files\Node\Node $node |
|
| 84 | - * @return bool |
|
| 85 | - */ |
|
| 86 | - public function isSubNode($node) { |
|
| 87 | - return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * get the content of this directory |
|
| 92 | - * |
|
| 93 | - * @throws \OCP\Files\NotFoundException |
|
| 94 | - * @return Node[] |
|
| 95 | - */ |
|
| 96 | - public function getDirectoryListing() { |
|
| 97 | - $folderContent = $this->view->getDirectoryContent($this->path); |
|
| 98 | - |
|
| 99 | - return array_map(function (FileInfo $info) { |
|
| 100 | - if ($info->getMimetype() === 'httpd/unix-directory') { |
|
| 101 | - return new Folder($this->root, $this->view, $info->getPath(), $info); |
|
| 102 | - } else { |
|
| 103 | - return new File($this->root, $this->view, $info->getPath(), $info); |
|
| 104 | - } |
|
| 105 | - }, $folderContent); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param string $path |
|
| 110 | - * @param FileInfo $info |
|
| 111 | - * @return File|Folder |
|
| 112 | - */ |
|
| 113 | - protected function createNode($path, FileInfo $info = null) { |
|
| 114 | - if (is_null($info)) { |
|
| 115 | - $isDir = $this->view->is_dir($path); |
|
| 116 | - } else { |
|
| 117 | - $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
| 118 | - } |
|
| 119 | - if ($isDir) { |
|
| 120 | - return new Folder($this->root, $this->view, $path, $info); |
|
| 121 | - } else { |
|
| 122 | - return new File($this->root, $this->view, $path, $info); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Get the node at $path |
|
| 128 | - * |
|
| 129 | - * @param string $path |
|
| 130 | - * @return \OC\Files\Node\Node |
|
| 131 | - * @throws \OCP\Files\NotFoundException |
|
| 132 | - */ |
|
| 133 | - public function get($path) { |
|
| 134 | - return $this->root->get($this->getFullPath($path)); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @param string $path |
|
| 139 | - * @return bool |
|
| 140 | - */ |
|
| 141 | - public function nodeExists($path) { |
|
| 142 | - try { |
|
| 143 | - $this->get($path); |
|
| 144 | - return true; |
|
| 145 | - } catch (NotFoundException $e) { |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @param string $path |
|
| 152 | - * @return \OC\Files\Node\Folder |
|
| 153 | - * @throws \OCP\Files\NotPermittedException |
|
| 154 | - */ |
|
| 155 | - public function newFolder($path) { |
|
| 156 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 157 | - $fullPath = $this->getFullPath($path); |
|
| 158 | - $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath); |
|
| 159 | - $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 160 | - $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 161 | - if(!$this->view->mkdir($fullPath)) { |
|
| 162 | - throw new NotPermittedException('Could not create folder'); |
|
| 163 | - } |
|
| 164 | - $node = new Folder($this->root, $this->view, $fullPath); |
|
| 165 | - $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 166 | - $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 167 | - return $node; |
|
| 168 | - } else { |
|
| 169 | - throw new NotPermittedException('No create permission for folder'); |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @param string $path |
|
| 175 | - * @return \OC\Files\Node\File |
|
| 176 | - * @throws \OCP\Files\NotPermittedException |
|
| 177 | - */ |
|
| 178 | - public function newFile($path) { |
|
| 179 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 180 | - $fullPath = $this->getFullPath($path); |
|
| 181 | - $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); |
|
| 182 | - $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 183 | - $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 184 | - if (!$this->view->touch($fullPath)) { |
|
| 185 | - throw new NotPermittedException('Could not create path'); |
|
| 186 | - } |
|
| 187 | - $node = new File($this->root, $this->view, $fullPath); |
|
| 188 | - $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 189 | - $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 190 | - return $node; |
|
| 191 | - } |
|
| 192 | - throw new NotPermittedException('No create permission for path'); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * search for files with the name matching $query |
|
| 197 | - * |
|
| 198 | - * @param string|ISearchOperator $query |
|
| 199 | - * @return \OC\Files\Node\Node[] |
|
| 200 | - */ |
|
| 201 | - public function search($query) { |
|
| 202 | - if (is_string($query)) { |
|
| 203 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 204 | - } else { |
|
| 205 | - return $this->searchCommon('searchQuery', array($query)); |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * search for files by mimetype |
|
| 211 | - * |
|
| 212 | - * @param string $mimetype |
|
| 213 | - * @return Node[] |
|
| 214 | - */ |
|
| 215 | - public function searchByMime($mimetype) { |
|
| 216 | - return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * search for files by tag |
|
| 221 | - * |
|
| 222 | - * @param string|int $tag name or tag id |
|
| 223 | - * @param string $userId owner of the tags |
|
| 224 | - * @return Node[] |
|
| 225 | - */ |
|
| 226 | - public function searchByTag($tag, $userId) { |
|
| 227 | - return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * @param string $method cache method |
|
| 232 | - * @param array $args call args |
|
| 233 | - * @return \OC\Files\Node\Node[] |
|
| 234 | - */ |
|
| 235 | - private function searchCommon($method, $args) { |
|
| 236 | - $files = array(); |
|
| 237 | - $rootLength = strlen($this->path); |
|
| 238 | - $mount = $this->root->getMount($this->path); |
|
| 239 | - $storage = $mount->getStorage(); |
|
| 240 | - $internalPath = $mount->getInternalPath($this->path); |
|
| 241 | - $internalPath = rtrim($internalPath, '/'); |
|
| 242 | - if ($internalPath !== '') { |
|
| 243 | - $internalPath = $internalPath . '/'; |
|
| 244 | - } |
|
| 245 | - $internalRootLength = strlen($internalPath); |
|
| 246 | - |
|
| 247 | - $cache = $storage->getCache(''); |
|
| 248 | - |
|
| 249 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 250 | - foreach ($results as $result) { |
|
| 251 | - if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) { |
|
| 252 | - $result['internalPath'] = $result['path']; |
|
| 253 | - $result['path'] = substr($result['path'], $internalRootLength); |
|
| 254 | - $result['storage'] = $storage; |
|
| 255 | - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - $mounts = $this->root->getMountsIn($this->path); |
|
| 260 | - foreach ($mounts as $mount) { |
|
| 261 | - $storage = $mount->getStorage(); |
|
| 262 | - if ($storage) { |
|
| 263 | - $cache = $storage->getCache(''); |
|
| 264 | - |
|
| 265 | - $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); |
|
| 266 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 267 | - foreach ($results as $result) { |
|
| 268 | - $result['internalPath'] = $result['path']; |
|
| 269 | - $result['path'] = $relativeMountPoint . $result['path']; |
|
| 270 | - $result['storage'] = $storage; |
|
| 271 | - $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - return array_map(function (FileInfo $file) { |
|
| 277 | - return $this->createNode($file->getPath(), $file); |
|
| 278 | - }, $files); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * @param int $id |
|
| 283 | - * @return \OC\Files\Node\Node[] |
|
| 284 | - */ |
|
| 285 | - public function getById($id) { |
|
| 286 | - $mountCache = $this->root->getUserMountCache(); |
|
| 287 | - if (strpos($this->getPath(), '/', 1) > 0) { |
|
| 288 | - list(, $user) = explode('/', $this->getPath()); |
|
| 289 | - } else { |
|
| 290 | - $user = null; |
|
| 291 | - } |
|
| 292 | - $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user); |
|
| 293 | - $mounts = $this->root->getMountsIn($this->path); |
|
| 294 | - $mounts[] = $this->root->getMount($this->path); |
|
| 295 | - /** @var IMountPoint[] $folderMounts */ |
|
| 296 | - $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) { |
|
| 297 | - return $mountPoint->getMountPoint(); |
|
| 298 | - }, $mounts), $mounts); |
|
| 299 | - |
|
| 300 | - /** @var ICachedMountInfo[] $mountsContainingFile */ |
|
| 301 | - $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) { |
|
| 302 | - return isset($folderMounts[$cachedMountInfo->getMountPoint()]); |
|
| 303 | - })); |
|
| 304 | - |
|
| 305 | - if (count($mountsContainingFile) === 0) { |
|
| 306 | - return []; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) { |
|
| 310 | - $mount = $folderMounts[$cachedMountInfo->getMountPoint()]; |
|
| 311 | - $cacheEntry = $mount->getStorage()->getCache()->get((int)$id); |
|
| 312 | - if (!$cacheEntry) { |
|
| 313 | - return null; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - // cache jails will hide the "true" internal path |
|
| 317 | - $internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/'); |
|
| 318 | - $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath())); |
|
| 319 | - $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
| 320 | - $absolutePath = rtrim($cachedMountInfo->getMountPoint() . $pathRelativeToMount, '/'); |
|
| 321 | - return $this->root->createNode($absolutePath, new \OC\Files\FileInfo( |
|
| 322 | - $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
| 323 | - \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
| 324 | - )); |
|
| 325 | - }, $mountsContainingFile); |
|
| 326 | - |
|
| 327 | - $nodes = array_filter($nodes); |
|
| 328 | - |
|
| 329 | - return array_filter($nodes, function (Node $node) { |
|
| 330 | - return $this->getRelativePath($node->getPath()); |
|
| 331 | - }); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - public function getFreeSpace() { |
|
| 335 | - return $this->view->free_space($this->path); |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - public function delete() { |
|
| 339 | - if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 340 | - $this->sendHooks(array('preDelete')); |
|
| 341 | - $fileInfo = $this->getFileInfo(); |
|
| 342 | - $this->view->rmdir($this->path); |
|
| 343 | - $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); |
|
| 344 | - $this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); |
|
| 345 | - $this->exists = false; |
|
| 346 | - } else { |
|
| 347 | - throw new NotPermittedException('No delete permission for path'); |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Add a suffix to the name in case the file exists |
|
| 353 | - * |
|
| 354 | - * @param string $name |
|
| 355 | - * @return string |
|
| 356 | - * @throws NotPermittedException |
|
| 357 | - */ |
|
| 358 | - public function getNonExistingName($name) { |
|
| 359 | - $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view); |
|
| 360 | - return trim($this->getRelativePath($uniqueName), '/'); |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * @param int $limit |
|
| 365 | - * @param int $offset |
|
| 366 | - * @return \OCP\Files\Node[] |
|
| 367 | - */ |
|
| 368 | - public function getRecent($limit, $offset = 0) { |
|
| 369 | - $mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
| 370 | - $mounts = $this->root->getMountsIn($this->path); |
|
| 371 | - $mounts[] = $this->getMountPoint(); |
|
| 372 | - |
|
| 373 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 374 | - return $mount->getStorage(); |
|
| 375 | - }); |
|
| 376 | - $storageIds = array_map(function (IMountPoint $mount) { |
|
| 377 | - return $mount->getStorage()->getCache()->getNumericStorageId(); |
|
| 378 | - }, $mounts); |
|
| 379 | - /** @var IMountPoint[] $mountMap */ |
|
| 380 | - $mountMap = array_combine($storageIds, $mounts); |
|
| 381 | - $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER); |
|
| 382 | - |
|
| 383 | - // Search in batches of 500 entries |
|
| 384 | - $searchLimit = 500; |
|
| 385 | - $results = []; |
|
| 386 | - do { |
|
| 387 | - $searchResult = $this->recentSearch($searchLimit, $offset, $storageIds, $folderMimetype); |
|
| 388 | - |
|
| 389 | - // Exit condition if there are no more results |
|
| 390 | - if (count($searchResult) === 0) { |
|
| 391 | - break; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - $parseResult = $this->recentParse($searchResult, $mountMap, $mimetypeLoader); |
|
| 395 | - |
|
| 396 | - foreach ($parseResult as $result) { |
|
| 397 | - $results[] = $result; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - $offset += $searchLimit; |
|
| 401 | - } while (count($results) < $limit); |
|
| 402 | - |
|
| 403 | - return array_slice($results, 0, $limit); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - private function recentSearch($limit, $offset, $storageIds, $folderMimetype) { |
|
| 407 | - $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 408 | - $query = $builder |
|
| 409 | - ->select('f.*') |
|
| 410 | - ->from('filecache', 'f') |
|
| 411 | - ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 412 | - ->andWhere($builder->expr()->orX( |
|
| 413 | - // handle non empty folders separate |
|
| 414 | - $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)), |
|
| 415 | - $builder->expr()->eq('f.size', new Literal(0)) |
|
| 416 | - )) |
|
| 417 | - ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_versions/%'))) |
|
| 418 | - ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_trashbin/%'))) |
|
| 419 | - ->orderBy('f.mtime', 'DESC') |
|
| 420 | - ->setMaxResults($limit) |
|
| 421 | - ->setFirstResult($offset); |
|
| 422 | - return $query->execute()->fetchAll(); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - private function recentParse($result, $mountMap, $mimetypeLoader) { |
|
| 426 | - $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) { |
|
| 427 | - $mount = $mountMap[$entry['storage']]; |
|
| 428 | - $entry['internalPath'] = $entry['path']; |
|
| 429 | - $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']); |
|
| 430 | - $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']); |
|
| 431 | - $path = $this->getAbsolutePath($mount, $entry['path']); |
|
| 432 | - if (is_null($path)) { |
|
| 433 | - return null; |
|
| 434 | - } |
|
| 435 | - $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount); |
|
| 436 | - return $this->root->createNode($fileInfo->getPath(), $fileInfo); |
|
| 437 | - }, $result)); |
|
| 438 | - |
|
| 439 | - return array_values(array_filter($files, function (Node $node) { |
|
| 440 | - $relative = $this->getRelativePath($node->getPath()); |
|
| 441 | - return $relative !== null && $relative !== '/'; |
|
| 442 | - })); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - private function getAbsolutePath(IMountPoint $mount, $path) { |
|
| 446 | - $storage = $mount->getStorage(); |
|
| 447 | - if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) { |
|
| 448 | - if ($storage->instanceOfStorage(SharedStorage::class)) { |
|
| 449 | - $storage->getSourceStorage(); |
|
| 450 | - } |
|
| 451 | - /** @var \OC\Files\Storage\Wrapper\Jail $storage */ |
|
| 452 | - $jailRoot = $storage->getUnjailedPath(''); |
|
| 453 | - $rootLength = strlen($jailRoot) + 1; |
|
| 454 | - if ($path === $jailRoot) { |
|
| 455 | - return $mount->getMountPoint(); |
|
| 456 | - } else if (substr($path, 0, $rootLength) === $jailRoot . '/') { |
|
| 457 | - return $mount->getMountPoint() . substr($path, $rootLength); |
|
| 458 | - } else { |
|
| 459 | - return null; |
|
| 460 | - } |
|
| 461 | - } else { |
|
| 462 | - return $mount->getMountPoint() . $path; |
|
| 463 | - } |
|
| 464 | - } |
|
| 40 | + /** |
|
| 41 | + * Creates a Folder that represents a non-existing path |
|
| 42 | + * |
|
| 43 | + * @param string $path path |
|
| 44 | + * @return string non-existing node class |
|
| 45 | + */ |
|
| 46 | + protected function createNonExistingNode($path) { |
|
| 47 | + return new NonExistingFolder($this->root, $this->view, $path); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param string $path path relative to the folder |
|
| 52 | + * @return string |
|
| 53 | + * @throws \OCP\Files\NotPermittedException |
|
| 54 | + */ |
|
| 55 | + public function getFullPath($path) { |
|
| 56 | + if (!$this->isValidPath($path)) { |
|
| 57 | + throw new NotPermittedException('Invalid path'); |
|
| 58 | + } |
|
| 59 | + return $this->path . $this->normalizePath($path); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param string $path |
|
| 64 | + * @return string |
|
| 65 | + */ |
|
| 66 | + public function getRelativePath($path) { |
|
| 67 | + if ($this->path === '' or $this->path === '/') { |
|
| 68 | + return $this->normalizePath($path); |
|
| 69 | + } |
|
| 70 | + if ($path === $this->path) { |
|
| 71 | + return '/'; |
|
| 72 | + } else if (strpos($path, $this->path . '/') !== 0) { |
|
| 73 | + return null; |
|
| 74 | + } else { |
|
| 75 | + $path = substr($path, strlen($this->path)); |
|
| 76 | + return $this->normalizePath($path); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * check if a node is a (grand-)child of the folder |
|
| 82 | + * |
|
| 83 | + * @param \OC\Files\Node\Node $node |
|
| 84 | + * @return bool |
|
| 85 | + */ |
|
| 86 | + public function isSubNode($node) { |
|
| 87 | + return strpos($node->getPath(), $this->path . '/') === 0; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * get the content of this directory |
|
| 92 | + * |
|
| 93 | + * @throws \OCP\Files\NotFoundException |
|
| 94 | + * @return Node[] |
|
| 95 | + */ |
|
| 96 | + public function getDirectoryListing() { |
|
| 97 | + $folderContent = $this->view->getDirectoryContent($this->path); |
|
| 98 | + |
|
| 99 | + return array_map(function (FileInfo $info) { |
|
| 100 | + if ($info->getMimetype() === 'httpd/unix-directory') { |
|
| 101 | + return new Folder($this->root, $this->view, $info->getPath(), $info); |
|
| 102 | + } else { |
|
| 103 | + return new File($this->root, $this->view, $info->getPath(), $info); |
|
| 104 | + } |
|
| 105 | + }, $folderContent); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param string $path |
|
| 110 | + * @param FileInfo $info |
|
| 111 | + * @return File|Folder |
|
| 112 | + */ |
|
| 113 | + protected function createNode($path, FileInfo $info = null) { |
|
| 114 | + if (is_null($info)) { |
|
| 115 | + $isDir = $this->view->is_dir($path); |
|
| 116 | + } else { |
|
| 117 | + $isDir = $info->getType() === FileInfo::TYPE_FOLDER; |
|
| 118 | + } |
|
| 119 | + if ($isDir) { |
|
| 120 | + return new Folder($this->root, $this->view, $path, $info); |
|
| 121 | + } else { |
|
| 122 | + return new File($this->root, $this->view, $path, $info); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Get the node at $path |
|
| 128 | + * |
|
| 129 | + * @param string $path |
|
| 130 | + * @return \OC\Files\Node\Node |
|
| 131 | + * @throws \OCP\Files\NotFoundException |
|
| 132 | + */ |
|
| 133 | + public function get($path) { |
|
| 134 | + return $this->root->get($this->getFullPath($path)); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param string $path |
|
| 139 | + * @return bool |
|
| 140 | + */ |
|
| 141 | + public function nodeExists($path) { |
|
| 142 | + try { |
|
| 143 | + $this->get($path); |
|
| 144 | + return true; |
|
| 145 | + } catch (NotFoundException $e) { |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @param string $path |
|
| 152 | + * @return \OC\Files\Node\Folder |
|
| 153 | + * @throws \OCP\Files\NotPermittedException |
|
| 154 | + */ |
|
| 155 | + public function newFolder($path) { |
|
| 156 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 157 | + $fullPath = $this->getFullPath($path); |
|
| 158 | + $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath); |
|
| 159 | + $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 160 | + $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 161 | + if(!$this->view->mkdir($fullPath)) { |
|
| 162 | + throw new NotPermittedException('Could not create folder'); |
|
| 163 | + } |
|
| 164 | + $node = new Folder($this->root, $this->view, $fullPath); |
|
| 165 | + $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 166 | + $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 167 | + return $node; |
|
| 168 | + } else { |
|
| 169 | + throw new NotPermittedException('No create permission for folder'); |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @param string $path |
|
| 175 | + * @return \OC\Files\Node\File |
|
| 176 | + * @throws \OCP\Files\NotPermittedException |
|
| 177 | + */ |
|
| 178 | + public function newFile($path) { |
|
| 179 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) { |
|
| 180 | + $fullPath = $this->getFullPath($path); |
|
| 181 | + $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath); |
|
| 182 | + $this->root->emit('\OC\Files', 'preWrite', array($nonExisting)); |
|
| 183 | + $this->root->emit('\OC\Files', 'preCreate', array($nonExisting)); |
|
| 184 | + if (!$this->view->touch($fullPath)) { |
|
| 185 | + throw new NotPermittedException('Could not create path'); |
|
| 186 | + } |
|
| 187 | + $node = new File($this->root, $this->view, $fullPath); |
|
| 188 | + $this->root->emit('\OC\Files', 'postWrite', array($node)); |
|
| 189 | + $this->root->emit('\OC\Files', 'postCreate', array($node)); |
|
| 190 | + return $node; |
|
| 191 | + } |
|
| 192 | + throw new NotPermittedException('No create permission for path'); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * search for files with the name matching $query |
|
| 197 | + * |
|
| 198 | + * @param string|ISearchOperator $query |
|
| 199 | + * @return \OC\Files\Node\Node[] |
|
| 200 | + */ |
|
| 201 | + public function search($query) { |
|
| 202 | + if (is_string($query)) { |
|
| 203 | + return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 204 | + } else { |
|
| 205 | + return $this->searchCommon('searchQuery', array($query)); |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * search for files by mimetype |
|
| 211 | + * |
|
| 212 | + * @param string $mimetype |
|
| 213 | + * @return Node[] |
|
| 214 | + */ |
|
| 215 | + public function searchByMime($mimetype) { |
|
| 216 | + return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * search for files by tag |
|
| 221 | + * |
|
| 222 | + * @param string|int $tag name or tag id |
|
| 223 | + * @param string $userId owner of the tags |
|
| 224 | + * @return Node[] |
|
| 225 | + */ |
|
| 226 | + public function searchByTag($tag, $userId) { |
|
| 227 | + return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * @param string $method cache method |
|
| 232 | + * @param array $args call args |
|
| 233 | + * @return \OC\Files\Node\Node[] |
|
| 234 | + */ |
|
| 235 | + private function searchCommon($method, $args) { |
|
| 236 | + $files = array(); |
|
| 237 | + $rootLength = strlen($this->path); |
|
| 238 | + $mount = $this->root->getMount($this->path); |
|
| 239 | + $storage = $mount->getStorage(); |
|
| 240 | + $internalPath = $mount->getInternalPath($this->path); |
|
| 241 | + $internalPath = rtrim($internalPath, '/'); |
|
| 242 | + if ($internalPath !== '') { |
|
| 243 | + $internalPath = $internalPath . '/'; |
|
| 244 | + } |
|
| 245 | + $internalRootLength = strlen($internalPath); |
|
| 246 | + |
|
| 247 | + $cache = $storage->getCache(''); |
|
| 248 | + |
|
| 249 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 250 | + foreach ($results as $result) { |
|
| 251 | + if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) { |
|
| 252 | + $result['internalPath'] = $result['path']; |
|
| 253 | + $result['path'] = substr($result['path'], $internalRootLength); |
|
| 254 | + $result['storage'] = $storage; |
|
| 255 | + $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + $mounts = $this->root->getMountsIn($this->path); |
|
| 260 | + foreach ($mounts as $mount) { |
|
| 261 | + $storage = $mount->getStorage(); |
|
| 262 | + if ($storage) { |
|
| 263 | + $cache = $storage->getCache(''); |
|
| 264 | + |
|
| 265 | + $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/'); |
|
| 266 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 267 | + foreach ($results as $result) { |
|
| 268 | + $result['internalPath'] = $result['path']; |
|
| 269 | + $result['path'] = $relativeMountPoint . $result['path']; |
|
| 270 | + $result['storage'] = $storage; |
|
| 271 | + $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount); |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + return array_map(function (FileInfo $file) { |
|
| 277 | + return $this->createNode($file->getPath(), $file); |
|
| 278 | + }, $files); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * @param int $id |
|
| 283 | + * @return \OC\Files\Node\Node[] |
|
| 284 | + */ |
|
| 285 | + public function getById($id) { |
|
| 286 | + $mountCache = $this->root->getUserMountCache(); |
|
| 287 | + if (strpos($this->getPath(), '/', 1) > 0) { |
|
| 288 | + list(, $user) = explode('/', $this->getPath()); |
|
| 289 | + } else { |
|
| 290 | + $user = null; |
|
| 291 | + } |
|
| 292 | + $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user); |
|
| 293 | + $mounts = $this->root->getMountsIn($this->path); |
|
| 294 | + $mounts[] = $this->root->getMount($this->path); |
|
| 295 | + /** @var IMountPoint[] $folderMounts */ |
|
| 296 | + $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) { |
|
| 297 | + return $mountPoint->getMountPoint(); |
|
| 298 | + }, $mounts), $mounts); |
|
| 299 | + |
|
| 300 | + /** @var ICachedMountInfo[] $mountsContainingFile */ |
|
| 301 | + $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) { |
|
| 302 | + return isset($folderMounts[$cachedMountInfo->getMountPoint()]); |
|
| 303 | + })); |
|
| 304 | + |
|
| 305 | + if (count($mountsContainingFile) === 0) { |
|
| 306 | + return []; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) { |
|
| 310 | + $mount = $folderMounts[$cachedMountInfo->getMountPoint()]; |
|
| 311 | + $cacheEntry = $mount->getStorage()->getCache()->get((int)$id); |
|
| 312 | + if (!$cacheEntry) { |
|
| 313 | + return null; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + // cache jails will hide the "true" internal path |
|
| 317 | + $internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/'); |
|
| 318 | + $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath())); |
|
| 319 | + $pathRelativeToMount = ltrim($pathRelativeToMount, '/'); |
|
| 320 | + $absolutePath = rtrim($cachedMountInfo->getMountPoint() . $pathRelativeToMount, '/'); |
|
| 321 | + return $this->root->createNode($absolutePath, new \OC\Files\FileInfo( |
|
| 322 | + $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount, |
|
| 323 | + \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount)) |
|
| 324 | + )); |
|
| 325 | + }, $mountsContainingFile); |
|
| 326 | + |
|
| 327 | + $nodes = array_filter($nodes); |
|
| 328 | + |
|
| 329 | + return array_filter($nodes, function (Node $node) { |
|
| 330 | + return $this->getRelativePath($node->getPath()); |
|
| 331 | + }); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + public function getFreeSpace() { |
|
| 335 | + return $this->view->free_space($this->path); |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + public function delete() { |
|
| 339 | + if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) { |
|
| 340 | + $this->sendHooks(array('preDelete')); |
|
| 341 | + $fileInfo = $this->getFileInfo(); |
|
| 342 | + $this->view->rmdir($this->path); |
|
| 343 | + $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo); |
|
| 344 | + $this->root->emit('\OC\Files', 'postDelete', array($nonExisting)); |
|
| 345 | + $this->exists = false; |
|
| 346 | + } else { |
|
| 347 | + throw new NotPermittedException('No delete permission for path'); |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Add a suffix to the name in case the file exists |
|
| 353 | + * |
|
| 354 | + * @param string $name |
|
| 355 | + * @return string |
|
| 356 | + * @throws NotPermittedException |
|
| 357 | + */ |
|
| 358 | + public function getNonExistingName($name) { |
|
| 359 | + $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view); |
|
| 360 | + return trim($this->getRelativePath($uniqueName), '/'); |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * @param int $limit |
|
| 365 | + * @param int $offset |
|
| 366 | + * @return \OCP\Files\Node[] |
|
| 367 | + */ |
|
| 368 | + public function getRecent($limit, $offset = 0) { |
|
| 369 | + $mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
| 370 | + $mounts = $this->root->getMountsIn($this->path); |
|
| 371 | + $mounts[] = $this->getMountPoint(); |
|
| 372 | + |
|
| 373 | + $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 374 | + return $mount->getStorage(); |
|
| 375 | + }); |
|
| 376 | + $storageIds = array_map(function (IMountPoint $mount) { |
|
| 377 | + return $mount->getStorage()->getCache()->getNumericStorageId(); |
|
| 378 | + }, $mounts); |
|
| 379 | + /** @var IMountPoint[] $mountMap */ |
|
| 380 | + $mountMap = array_combine($storageIds, $mounts); |
|
| 381 | + $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER); |
|
| 382 | + |
|
| 383 | + // Search in batches of 500 entries |
|
| 384 | + $searchLimit = 500; |
|
| 385 | + $results = []; |
|
| 386 | + do { |
|
| 387 | + $searchResult = $this->recentSearch($searchLimit, $offset, $storageIds, $folderMimetype); |
|
| 388 | + |
|
| 389 | + // Exit condition if there are no more results |
|
| 390 | + if (count($searchResult) === 0) { |
|
| 391 | + break; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + $parseResult = $this->recentParse($searchResult, $mountMap, $mimetypeLoader); |
|
| 395 | + |
|
| 396 | + foreach ($parseResult as $result) { |
|
| 397 | + $results[] = $result; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + $offset += $searchLimit; |
|
| 401 | + } while (count($results) < $limit); |
|
| 402 | + |
|
| 403 | + return array_slice($results, 0, $limit); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + private function recentSearch($limit, $offset, $storageIds, $folderMimetype) { |
|
| 407 | + $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 408 | + $query = $builder |
|
| 409 | + ->select('f.*') |
|
| 410 | + ->from('filecache', 'f') |
|
| 411 | + ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY))) |
|
| 412 | + ->andWhere($builder->expr()->orX( |
|
| 413 | + // handle non empty folders separate |
|
| 414 | + $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)), |
|
| 415 | + $builder->expr()->eq('f.size', new Literal(0)) |
|
| 416 | + )) |
|
| 417 | + ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_versions/%'))) |
|
| 418 | + ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_trashbin/%'))) |
|
| 419 | + ->orderBy('f.mtime', 'DESC') |
|
| 420 | + ->setMaxResults($limit) |
|
| 421 | + ->setFirstResult($offset); |
|
| 422 | + return $query->execute()->fetchAll(); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + private function recentParse($result, $mountMap, $mimetypeLoader) { |
|
| 426 | + $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) { |
|
| 427 | + $mount = $mountMap[$entry['storage']]; |
|
| 428 | + $entry['internalPath'] = $entry['path']; |
|
| 429 | + $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']); |
|
| 430 | + $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']); |
|
| 431 | + $path = $this->getAbsolutePath($mount, $entry['path']); |
|
| 432 | + if (is_null($path)) { |
|
| 433 | + return null; |
|
| 434 | + } |
|
| 435 | + $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount); |
|
| 436 | + return $this->root->createNode($fileInfo->getPath(), $fileInfo); |
|
| 437 | + }, $result)); |
|
| 438 | + |
|
| 439 | + return array_values(array_filter($files, function (Node $node) { |
|
| 440 | + $relative = $this->getRelativePath($node->getPath()); |
|
| 441 | + return $relative !== null && $relative !== '/'; |
|
| 442 | + })); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + private function getAbsolutePath(IMountPoint $mount, $path) { |
|
| 446 | + $storage = $mount->getStorage(); |
|
| 447 | + if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) { |
|
| 448 | + if ($storage->instanceOfStorage(SharedStorage::class)) { |
|
| 449 | + $storage->getSourceStorage(); |
|
| 450 | + } |
|
| 451 | + /** @var \OC\Files\Storage\Wrapper\Jail $storage */ |
|
| 452 | + $jailRoot = $storage->getUnjailedPath(''); |
|
| 453 | + $rootLength = strlen($jailRoot) + 1; |
|
| 454 | + if ($path === $jailRoot) { |
|
| 455 | + return $mount->getMountPoint(); |
|
| 456 | + } else if (substr($path, 0, $rootLength) === $jailRoot . '/') { |
|
| 457 | + return $mount->getMountPoint() . substr($path, $rootLength); |
|
| 458 | + } else { |
|
| 459 | + return null; |
|
| 460 | + } |
|
| 461 | + } else { |
|
| 462 | + return $mount->getMountPoint() . $path; |
|
| 463 | + } |
|
| 464 | + } |
|
| 465 | 465 | } |