Blame view
sources/apps/files_sharing/lib/cache.php
11 KB
|
03e52840d
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php
/**
* ownCloud
*
* @author Michael Gapczynski
* @copyright 2012 Michael Gapczynski mtgap@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
namespace OC\Files\Cache;
use OCP\Share_Backend_Collection;
/**
* Metadata cache for shared files
*
* don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
*/
class Shared_Cache extends Cache {
private $storage;
private $files = array();
public function __construct($storage) {
$this->storage = $storage;
}
/**
* @brief Get the source cache of a shared file or folder
* @param string $target Shared target file path
* @return \OC\Files\Cache\Cache
*/
private function getSourceCache($target) {
$source = \OC_Share_Backend_File::getSource($target);
if (isset($source['path']) && isset($source['fileOwner'])) {
\OC\Files\Filesystem::initMountPoints($source['fileOwner']);
|
|
31b7f2792
|
48 49 50 |
$mount = \OC\Files\Filesystem::getMountByNumericId($source['storage']);
if (is_array($mount)) {
$fullPath = $mount[key($mount)]->getMountPoint().$source['path'];
|
|
03e52840d
|
51 52 53 54 55 56 57 58 59 60 61 62 |
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($fullPath);
if ($storage) {
$this->files[$target] = $internalPath;
$cache = $storage->getCache();
$this->storageId = $storage->getId();
$this->numericId = $cache->getNumericStorageId();
return $cache;
}
}
}
return false;
}
|
|
31b7f2792
|
63 64 65 66 67 68 69 |
public function getNumericStorageId() {
if (isset($this->numericId)) {
return $this->numericId;
} else {
return false;
}
}
|
|
03e52840d
|
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
/**
* get the stored metadata of a file or folder
*
* @param string/int $file
* @return array
*/
public function get($file) {
if ($file == '') {
$data = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_FILE_APP_ROOT);
$etag = \OCP\Config::getUserValue(\OCP\User::getUser(), 'files_sharing', 'etag');
if (!isset($etag)) {
$etag = $this->storage->getETag('');
\OCP\Config::setUserValue(\OCP\User::getUser(), 'files_sharing', 'etag', $etag);
}
$data['etag'] = $etag;
return $data;
} else if (is_string($file)) {
if ($cache = $this->getSourceCache($file)) {
return $cache->get($this->files[$file]);
}
} else {
$query = \OC_DB::prepare(
'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`,'
|
|
a293d369c
|
93 |
.' `size`, `mtime`, `encrypted`, `unencrypted_size`' |
|
03e52840d
|
94 95 96 97 |
.' FROM `*PREFIX*filecache` WHERE `fileid` = ?'); $result = $query->execute(array($file)); $data = $result->fetchRow(); $data['fileid'] = (int)$data['fileid']; |
|
03e52840d
|
98 |
$data['mtime'] = (int)$data['mtime']; |
|
31b7f2792
|
99 |
$data['storage_mtime'] = (int)$data['storage_mtime']; |
|
03e52840d
|
100 101 102 |
$data['encrypted'] = (bool)$data['encrypted']; $data['mimetype'] = $this->getMimetype($data['mimetype']); $data['mimepart'] = $this->getMimetype($data['mimepart']); |
|
31b7f2792
|
103 104 105 |
if ($data['storage_mtime'] === 0) {
$data['storage_mtime'] = $data['mtime'];
}
|
|
a293d369c
|
106 107 108 109 110 111 |
if ($data['encrypted'] or ($data['unencrypted_size'] > 0 and $data['mimetype'] === 'httpd/unix-directory')) {
$data['encrypted_size'] = (int)$data['size'];
$data['size'] = (int)$data['unencrypted_size'];
} else {
$data['size'] = (int)$data['size'];
}
|
|
03e52840d
|
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
return $data;
}
return false;
}
/**
* get the metadata of all files stored in $folder
*
* @param string $folder
* @return array
*/
public function getFolderContents($folder) {
if ($folder == '') {
$files = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_FOLDER_CONTENTS);
foreach ($files as &$file) {
$file['mimetype'] = $this->getMimetype($file['mimetype']);
$file['mimepart'] = $this->getMimetype($file['mimepart']);
|
|
a293d369c
|
129 |
$file['usersPath'] = 'files/Shared/' . ltrim($file['path'], '/'); |
|
03e52840d
|
130 131 132 |
}
return $files;
} else {
|
|
a293d369c
|
133 134 135 136 137 138 139 140 |
$cache = $this->getSourceCache($folder);
if ($cache) {
$sourceFolderContent = $cache->getFolderContents($this->files[$folder]);
foreach ($sourceFolderContent as $key => $c) {
$sourceFolderContent[$key]['usersPath'] = 'files/Shared/' . $folder . '/' . $c['name'];
}
return $sourceFolderContent;
|
|
03e52840d
|
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
}
}
return false;
}
/**
* store meta data for a file or folder
*
* @param string $file
* @param array $data
*
* @return int file id
*/
public function put($file, array $data) {
if ($file === '' && isset($data['etag'])) {
return \OCP\Config::setUserValue(\OCP\User::getUser(), 'files_sharing', 'etag', $data['etag']);
} else if ($cache = $this->getSourceCache($file)) {
return $cache->put($this->files[$file], $data);
}
return false;
}
/**
* get the file id for a file
*
* @param string $file
* @return int
*/
public function getId($file) {
if ($cache = $this->getSourceCache($file)) {
return $cache->getId($this->files[$file]);
}
return -1;
}
/**
* check if a file is available in the cache
*
* @param string $file
* @return bool
*/
public function inCache($file) {
if ($file == '') {
return true;
}
return parent::inCache($file);
}
/**
* remove a file or folder from the cache
*
* @param string $file
*/
public function remove($file) {
if ($cache = $this->getSourceCache($file)) {
$cache->remove($this->files[$file]);
}
}
/**
* Move a file or folder in the cache
*
* @param string $source
* @param string $target
*/
public function move($source, $target) {
if ($cache = $this->getSourceCache($source)) {
$file = \OC_Share_Backend_File::getSource($target);
if ($file && isset($file['path'])) {
$cache->move($this->files[$source], $file['path']);
}
}
}
/**
* remove all entries for files that are stored on the storage from the cache
*/
public function clear() {
// Not a valid action for Shared Cache
}
/**
* @param string $file
*
* @return int, Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
*/
public function getStatus($file) {
if ($file == '') {
return self::COMPLETE;
}
if ($cache = $this->getSourceCache($file)) {
return $cache->getStatus($this->files[$file]);
}
return self::NOT_FOUND;
}
/**
* search for files matching $pattern
*
* @param string $pattern
* @return array of file data
*/
public function search($pattern) {
$where = '`name` LIKE ? AND ';
// normalize pattern
$value = $this->normalize($pattern);
|
|
31b7f2792
|
249 |
return $this->searchWithWhere($where, $value); |
|
03e52840d
|
250 251 252 253 254 255 256 257 258 259 |
}
/**
* search for files by mimetype
*
* @param string $mimetype
* @return array
*/
public function searchByMime($mimetype) {
|
|
a293d369c
|
260 261 262 263 |
$mimepart = null;
if (strpos($mimetype, '/') === false) {
$mimepart = $mimetype;
$mimetype = null;
|
|
03e52840d
|
264 |
} |
|
a293d369c
|
265 266 267 |
// note: searchWithWhere is currently broken as it doesn't // recurse into subdirs nor returns the correct // file paths, so using getFolderContents() for now |
|
03e52840d
|
268 |
|
|
a293d369c
|
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
$result = array();
$exploreDirs = array('');
while (count($exploreDirs) > 0) {
$dir = array_pop($exploreDirs);
$files = $this->getFolderContents($dir);
// no results?
if (!$files) {
continue;
}
foreach ($files as $file) {
if ($file['mimetype'] === 'httpd/unix-directory') {
$exploreDirs[] = ltrim($dir . '/' . $file['name'], '/');
}
else if (($mimepart && $file['mimepart'] === $mimepart) || ($mimetype && $file['mimetype'] === $mimetype)) {
// usersPath not reliable
//$file['path'] = $file['usersPath'];
$file['path'] = ltrim($dir . '/' . $file['name'], '/');
$result[] = $file;
}
}
}
return $result;
|
|
03e52840d
|
291 |
} |
|
a293d369c
|
292 |
|
|
03e52840d
|
293 294 |
/** * The maximum number of placeholders that can be used in an SQL query. |
|
31b7f2792
|
295 296 297 |
* Value MUST be <= 1000 for oracle: * see ORA-01795 maximum number of expressions in a list is 1000 * FIXME we should get this from doctrine as other DBs allow a lot more placeholders |
|
03e52840d
|
298 |
*/ |
|
31b7f2792
|
299 |
const MAX_SQL_CHUNK_SIZE = 1000; |
|
a293d369c
|
300 |
|
|
03e52840d
|
301 302 303 304 305 306 307 308 309 310 311 312 313 |
/**
* search for files with a custom where clause and value
* the $wherevalue will be array_merge()d with the file id chunks
*
* @param string $sqlwhere
* @param string $wherevalue
* @return array
*/
private function searchWithWhere($sqlwhere, $wherevalue, $chunksize = self::MAX_SQL_CHUNK_SIZE) {
$ids = $this->getAll();
$files = array();
|
|
a293d369c
|
314 |
|
|
31b7f2792
|
315 |
// divide into chunks |
|
03e52840d
|
316 |
$chunks = array_chunk($ids, $chunksize); |
|
a293d369c
|
317 |
|
|
03e52840d
|
318 319 320 321 322 |
foreach ($chunks as $chunk) {
$placeholders = join(',', array_fill(0, count($chunk), '?'));
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
`encrypted`, `unencrypted_size`, `etag`
FROM `*PREFIX*filecache` WHERE ' . $sqlwhere . ' `fileid` IN (' . $placeholders . ')';
|
|
a293d369c
|
323 |
|
|
31b7f2792
|
324 |
$stmt = \OC_DB::prepare($sql); |
|
03e52840d
|
325 326 327 328 329 330 331 332 |
$result = $stmt->execute(array_merge(array($wherevalue), $chunk));
while ($row = $result->fetchRow()) {
if (substr($row['path'], 0, 6) === 'files/') {
$row['path'] = substr($row['path'], 6); // remove 'files/' from path as it's relative to '/Shared'
}
$row['mimetype'] = $this->getMimetype($row['mimetype']);
$row['mimepart'] = $this->getMimetype($row['mimepart']);
|
|
a293d369c
|
333 334 335 336 337 338 |
if ($row['encrypted'] or ($row['unencrypted_size'] > 0 and $row['mimetype'] === 'httpd/unix-directory')) {
$row['encrypted_size'] = $row['size'];
$row['size'] = $row['unencrypted_size'];
} else {
$row['size'] = $row['size'];
}
|
|
03e52840d
|
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
$files[] = $row;
}
}
return $files;
}
/**
* get the size of a folder and set it in the cache
*
* @param string $path
* @return int
*/
public function calculateFolderSize($path) {
if ($cache = $this->getSourceCache($path)) {
return $cache->calculateFolderSize($this->files[$path]);
}
return 0;
}
/**
* get all file ids on the files on the storage
*
* @return int[]
*/
public function getAll() {
$ids = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_ALL);
$folderBackend = \OCP\Share::getBackend('folder');
if ($folderBackend instanceof Share_Backend_Collection) {
foreach ($ids as $file) {
/** @var $folderBackend Share_Backend_Collection */
$children = $folderBackend->getChildren($file);
foreach ($children as $child) {
$ids[] = (int)$child['source'];
}
}
}
return $ids;
}
|
|
31b7f2792
|
379 380 381 382 383 384 385 386 387 388 389 390 |
/**
* find a folder in the cache which has not been fully scanned
*
* If multiply incomplete folders are in the cache, the one with the highest id will be returned,
* use the one with the highest id gives the best result with the background scanner, since that is most
* likely the folder where we stopped scanning previously
*
* @return string|bool the path of the folder or false when no folder matched
*/
public function getIncomplete() {
return false;
}
|
|
03e52840d
|
391 |
} |