2012-12-16 04:44:46 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2013-01-29 00:25:19 +04:00
|
|
|
* 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/>.
|
|
|
|
*/
|
2012-12-16 04:44:46 +04:00
|
|
|
|
|
|
|
namespace OC\Files\Cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
|
2013-02-20 05:42:48 +04:00
|
|
|
private $storage;
|
2012-12-16 04:44:46 +04:00
|
|
|
private $files = array();
|
|
|
|
|
2012-12-29 00:06:12 +04:00
|
|
|
public function __construct($storage) {
|
2013-02-20 05:42:48 +04:00
|
|
|
$this->storage = $storage;
|
2012-12-29 00:06:12 +04:00
|
|
|
}
|
|
|
|
|
2012-12-16 04:44:46 +04:00
|
|
|
/**
|
2013-01-29 00:25:19 +04:00
|
|
|
* @brief Get the source cache of a shared file or folder
|
|
|
|
* @param string $target Shared target file path
|
|
|
|
* @return \OC\Files\Cache\Cache
|
|
|
|
*/
|
2012-12-16 04:44:46 +04:00
|
|
|
private function getSourceCache($target) {
|
|
|
|
$source = \OC_Share_Backend_File::getSource($target);
|
2013-03-08 23:27:30 +04:00
|
|
|
if (isset($source['path']) && isset($source['fileOwner'])) {
|
|
|
|
\OC\Files\Filesystem::initMountPoints($source['fileOwner']);
|
2013-04-26 02:01:36 +04:00
|
|
|
$mount = \OC\Files\Filesystem::getMountByNumericId($source['storage']);
|
2013-05-03 01:47:11 +04:00
|
|
|
if (is_array($mount)) {
|
|
|
|
$fullPath = $mount[key($mount)]->getMountPoint().$source['path'];
|
2013-03-08 23:27:30 +04:00
|
|
|
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;
|
|
|
|
}
|
2012-12-29 00:06:12 +04:00
|
|
|
}
|
2012-12-16 04:44:46 +04:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-03 01:47:11 +04:00
|
|
|
public function getNumericStorageId() {
|
|
|
|
if (isset($this->numericId)) {
|
|
|
|
return $this->numericId;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-16 04:44:46 +04:00
|
|
|
/**
|
|
|
|
* get the stored metadata of a file or folder
|
|
|
|
*
|
|
|
|
* @param string/int $file
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get($file) {
|
2012-12-29 20:45:13 +04:00
|
|
|
if ($file == '') {
|
2013-02-20 05:42:48 +04:00
|
|
|
$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;
|
2012-12-29 20:45:13 +04:00
|
|
|
} else if (is_string($file)) {
|
2012-12-16 04:44:46 +04:00
|
|
|
if ($cache = $this->getSourceCache($file)) {
|
|
|
|
return $cache->get($this->files[$file]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$query = \OC_DB::prepare(
|
2013-02-15 01:37:49 +04:00
|
|
|
'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`,'
|
|
|
|
.' `size`, `mtime`, `encrypted`'
|
|
|
|
.' FROM `*PREFIX*filecache` WHERE `fileid` = ?');
|
2012-12-16 04:44:46 +04:00
|
|
|
$result = $query->execute(array($file));
|
|
|
|
$data = $result->fetchRow();
|
|
|
|
$data['fileid'] = (int)$data['fileid'];
|
|
|
|
$data['size'] = (int)$data['size'];
|
|
|
|
$data['mtime'] = (int)$data['mtime'];
|
|
|
|
$data['encrypted'] = (bool)$data['encrypted'];
|
2013-01-08 00:27:22 +04:00
|
|
|
$data['mimetype'] = $this->getMimetype($data['mimetype']);
|
|
|
|
$data['mimepart'] = $this->getMimetype($data['mimepart']);
|
2012-12-16 04:44:46 +04:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the metadata of all files stored in $folder
|
|
|
|
*
|
|
|
|
* @param string $folder
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getFolderContents($folder) {
|
|
|
|
if ($folder == '') {
|
2013-01-08 00:27:22 +04:00
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
return $files;
|
2012-12-16 04:44:46 +04:00
|
|
|
} else {
|
2012-12-29 00:06:12 +04:00
|
|
|
if ($cache = $this->getSourceCache($folder)) {
|
|
|
|
return $cache->getFolderContents($this->files[$folder]);
|
|
|
|
}
|
2012-12-16 04:44:46 +04:00
|
|
|
}
|
2012-12-29 00:06:12 +04:00
|
|
|
return false;
|
2012-12-16 04:44:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* store meta data for a file or folder
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return int file id
|
|
|
|
*/
|
|
|
|
public function put($file, array $data) {
|
2013-02-26 10:21:48 +04:00
|
|
|
if ($file === '' && isset($data['etag'])) {
|
|
|
|
return \OCP\Config::setUserValue(\OCP\User::getUser(), 'files_sharing', 'etag', $data['etag']);
|
2013-02-20 05:42:48 +04:00
|
|
|
} else if ($cache = $this->getSourceCache($file)) {
|
2013-01-01 21:10:38 +04:00
|
|
|
return $cache->put($this->files[$file], $data);
|
2012-12-16 04:44:46 +04:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-08 05:52:51 +04:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2012-12-16 04:44:46 +04:00
|
|
|
/**
|
|
|
|
* 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)) {
|
2013-05-02 04:29:07 +04:00
|
|
|
$file = \OC_Share_Backend_File::getSource($target);
|
|
|
|
if ($file && isset($file['path'])) {
|
|
|
|
$cache->move($this->files[$source], $file['path']);
|
2012-12-16 04:44:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2013-01-01 20:19:33 +04:00
|
|
|
if ($file == '') {
|
|
|
|
return self::COMPLETE;
|
|
|
|
}
|
2012-12-16 04:44:46 +04:00
|
|
|
if ($cache = $this->getSourceCache($file)) {
|
|
|
|
return $cache->getStatus($this->files[$file]);
|
|
|
|
}
|
2013-01-11 07:29:47 +04:00
|
|
|
return self::NOT_FOUND;
|
2012-12-16 04:44:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search for files matching $pattern
|
|
|
|
*
|
|
|
|
* @param string $pattern
|
|
|
|
* @return array of file data
|
|
|
|
*/
|
|
|
|
public function search($pattern) {
|
2013-07-26 16:11:59 +04:00
|
|
|
|
|
|
|
// normalize pattern
|
|
|
|
$pattern = $this->normalize($pattern);
|
|
|
|
|
|
|
|
$ids = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_ALL);
|
|
|
|
foreach ($ids as $file) {
|
|
|
|
$folderBackend = \OCP\Share::getBackend('folder');
|
|
|
|
$children = $folderBackend->getChildren($file);
|
|
|
|
foreach ($children as $child) {
|
|
|
|
$ids[] = (int)$child['source'];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
$placeholders = join(',', array_fill(0, count($ids), '?'));
|
|
|
|
|
|
|
|
$sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`, `unencrypted_size`, `etag`
|
|
|
|
FROM `*PREFIX*filecache` WHERE `name` LIKE ? AND `fileid` IN (' . $placeholders . ')';
|
|
|
|
$result = \OC_DB::executeAudited($sql, array_merge(array($pattern), $ids));
|
|
|
|
$files = array();
|
|
|
|
while ($row = $result->fetchRow()) {
|
|
|
|
$row['mimetype'] = $this->getMimetype($row['mimetype']);
|
|
|
|
$row['mimepart'] = $this->getMimetype($row['mimepart']);
|
|
|
|
$files[] = $row;
|
|
|
|
}
|
|
|
|
return $files;
|
2012-12-16 04:44:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* search for files by mimetype
|
|
|
|
*
|
|
|
|
* @param string $part1
|
|
|
|
* @param string $part2
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function searchByMime($mimetype) {
|
|
|
|
if (strpos($mimetype, '/')) {
|
|
|
|
$where = '`mimetype` = ?';
|
|
|
|
} else {
|
|
|
|
$where = '`mimepart` = ?';
|
|
|
|
}
|
2013-01-29 00:25:19 +04:00
|
|
|
$mimetype = $this->getMimetypeId($mimetype);
|
2012-12-16 04:44:46 +04:00
|
|
|
$ids = $this->getAll();
|
|
|
|
$placeholders = join(',', array_fill(0, count($ids), '?'));
|
|
|
|
$query = \OC_DB::prepare('
|
|
|
|
SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`
|
2013-01-29 00:25:19 +04:00
|
|
|
FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `fileid` IN (' . $placeholders . ')'
|
2012-12-16 04:44:46 +04:00
|
|
|
);
|
|
|
|
$result = $query->execute(array_merge(array($mimetype), $ids));
|
|
|
|
return $result->fetchAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
2013-01-01 21:07:10 +04:00
|
|
|
return \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_ALL);
|
2012-12-16 04:44:46 +04:00
|
|
|
}
|
|
|
|
|
2013-05-03 01:47:11 +04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2013-08-18 13:02:08 +04:00
|
|
|
}
|