2015-11-26 16:47:53 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-11-26 16:47:53 +00:00
|
|
|
*
|
|
|
|
* @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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Files\Config;
|
|
|
|
|
2016-03-08 10:23:20 +00:00
|
|
|
use OCA\Files_Sharing\SharedMount;
|
2016-01-26 13:50:57 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2017-08-22 13:44:52 +00:00
|
|
|
use OCP\Files\Config\ICachedMountFileInfo;
|
2015-11-26 16:47:53 +00:00
|
|
|
use OCP\Files\Config\ICachedMountInfo;
|
|
|
|
use OCP\Files\Config\IUserMountCache;
|
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2016-01-22 15:02:21 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
2015-11-26 16:47:53 +00:00
|
|
|
use OCP\ICache;
|
|
|
|
use OCP\IDBConnection;
|
2015-12-01 11:41:48 +00:00
|
|
|
use OCP\ILogger;
|
2015-11-26 16:47:53 +00:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2016-06-10 11:06:45 +00:00
|
|
|
use OC\Cache\CappedMemoryCache;
|
2015-11-26 16:47:53 +00:00
|
|
|
|
2015-12-02 12:36:33 +00:00
|
|
|
/**
|
|
|
|
* Cache mounts points per user in the cache so we can easilly look them up
|
|
|
|
*/
|
2015-11-26 16:47:53 +00:00
|
|
|
class UserMountCache implements IUserMountCache {
|
|
|
|
/**
|
|
|
|
* @var IDBConnection
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IUserManager
|
|
|
|
*/
|
|
|
|
private $userManager;
|
|
|
|
|
2016-06-10 11:06:45 +00:00
|
|
|
/**
|
|
|
|
* Cached mount info.
|
|
|
|
* Map of $userId to ICachedMountInfo.
|
|
|
|
*
|
|
|
|
* @var ICache
|
|
|
|
**/
|
|
|
|
private $mountsForUsers;
|
2015-11-26 16:47:53 +00:00
|
|
|
|
2015-12-01 11:41:48 +00:00
|
|
|
/**
|
|
|
|
* @var ILogger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
2016-06-10 11:06:45 +00:00
|
|
|
/**
|
|
|
|
* @var ICache
|
|
|
|
*/
|
|
|
|
private $cacheInfoCache;
|
2016-01-22 15:02:21 +00:00
|
|
|
|
2015-11-26 16:47:53 +00:00
|
|
|
/**
|
|
|
|
* UserMountCache constructor.
|
|
|
|
*
|
|
|
|
* @param IDBConnection $connection
|
|
|
|
* @param IUserManager $userManager
|
2015-12-01 11:41:48 +00:00
|
|
|
* @param ILogger $logger
|
2015-11-26 16:47:53 +00:00
|
|
|
*/
|
2015-12-01 11:41:48 +00:00
|
|
|
public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) {
|
2015-11-26 16:47:53 +00:00
|
|
|
$this->connection = $connection;
|
|
|
|
$this->userManager = $userManager;
|
2015-12-01 11:41:48 +00:00
|
|
|
$this->logger = $logger;
|
2016-06-10 11:06:45 +00:00
|
|
|
$this->cacheInfoCache = new CappedMemoryCache();
|
|
|
|
$this->mountsForUsers = new CappedMemoryCache();
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function registerMounts(IUser $user, array $mounts) {
|
2015-12-02 12:36:33 +00:00
|
|
|
// filter out non-proper storages coming from unit tests
|
2015-11-30 16:29:42 +00:00
|
|
|
$mounts = array_filter($mounts, function (IMountPoint $mount) {
|
2016-03-08 10:23:20 +00:00
|
|
|
return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache();
|
2015-11-30 16:29:42 +00:00
|
|
|
});
|
2015-11-26 16:47:53 +00:00
|
|
|
/** @var ICachedMountInfo[] $newMounts */
|
|
|
|
$newMounts = array_map(function (IMountPoint $mount) use ($user) {
|
2015-12-02 12:36:33 +00:00
|
|
|
// filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
|
2016-04-15 12:03:48 +00:00
|
|
|
if ($mount->getStorageRootId() === -1) {
|
2015-11-30 16:29:42 +00:00
|
|
|
return null;
|
|
|
|
} else {
|
2016-04-15 12:03:48 +00:00
|
|
|
return new LazyStorageMountInfo($user, $mount);
|
2015-11-30 16:29:42 +00:00
|
|
|
}
|
2015-11-26 16:47:53 +00:00
|
|
|
}, $mounts);
|
2015-11-30 16:29:42 +00:00
|
|
|
$newMounts = array_values(array_filter($newMounts));
|
2018-08-16 17:56:49 +00:00
|
|
|
$newMountRootIds = array_map(function (ICachedMountInfo $mount) {
|
|
|
|
return $mount->getRootId();
|
|
|
|
}, $newMounts);
|
|
|
|
$newMounts = array_combine($newMountRootIds, $newMounts);
|
2015-11-26 16:47:53 +00:00
|
|
|
|
2015-12-02 12:36:33 +00:00
|
|
|
$cachedMounts = $this->getMountsForUser($user);
|
2018-08-16 17:56:49 +00:00
|
|
|
$cachedMountRootIds = array_map(function (ICachedMountInfo $mount) {
|
|
|
|
return $mount->getRootId();
|
|
|
|
}, $cachedMounts);
|
|
|
|
$cachedMounts = array_combine($cachedMountRootIds, $cachedMounts);
|
2015-11-26 16:47:53 +00:00
|
|
|
|
2018-08-16 17:56:49 +00:00
|
|
|
$addedMounts = [];
|
|
|
|
$removedMounts = [];
|
|
|
|
|
|
|
|
foreach ($newMounts as $rootId => $newMount) {
|
|
|
|
if (!isset($cachedMounts[$rootId])) {
|
|
|
|
$addedMounts[] = $newMount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($cachedMounts as $rootId => $cachedMount) {
|
|
|
|
if (!isset($newMounts[$rootId])) {
|
|
|
|
$removedMounts[] = $cachedMount;
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 16:47:53 +00:00
|
|
|
|
2016-07-13 14:29:51 +00:00
|
|
|
$changedMounts = $this->findChangedMounts($newMounts, $cachedMounts);
|
2016-01-11 12:06:10 +00:00
|
|
|
|
2015-11-26 16:47:53 +00:00
|
|
|
foreach ($addedMounts as $mount) {
|
|
|
|
$this->addToCache($mount);
|
|
|
|
$this->mountsForUsers[$user->getUID()][] = $mount;
|
|
|
|
}
|
|
|
|
foreach ($removedMounts as $mount) {
|
|
|
|
$this->removeFromCache($mount);
|
2015-12-02 12:36:33 +00:00
|
|
|
$index = array_search($mount, $this->mountsForUsers[$user->getUID()]);
|
|
|
|
unset($this->mountsForUsers[$user->getUID()][$index]);
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|
2016-01-11 12:06:10 +00:00
|
|
|
foreach ($changedMounts as $mount) {
|
2016-07-13 14:29:51 +00:00
|
|
|
$this->updateCachedMount($mount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ICachedMountInfo[] $newMounts
|
|
|
|
* @param ICachedMountInfo[] $cachedMounts
|
|
|
|
* @return ICachedMountInfo[]
|
|
|
|
*/
|
|
|
|
private function findChangedMounts(array $newMounts, array $cachedMounts) {
|
2018-08-16 16:24:10 +00:00
|
|
|
$new = [];
|
|
|
|
foreach ($newMounts as $mount) {
|
|
|
|
$new[$mount->getRootId()] = $mount;
|
|
|
|
}
|
2016-07-13 14:29:51 +00:00
|
|
|
$changed = [];
|
2018-08-16 16:24:10 +00:00
|
|
|
foreach ($cachedMounts as $cachedMount) {
|
|
|
|
$rootId = $cachedMount->getRootId();
|
|
|
|
if (isset($new[$rootId])) {
|
|
|
|
$newMount = $new[$rootId];
|
2016-07-13 14:29:51 +00:00
|
|
|
if (
|
2018-08-16 16:24:10 +00:00
|
|
|
$newMount->getMountPoint() !== $cachedMount->getMountPoint() ||
|
|
|
|
$newMount->getStorageId() !== $cachedMount->getStorageId() ||
|
|
|
|
$newMount->getMountId() !== $cachedMount->getMountId()
|
2016-07-13 14:29:51 +00:00
|
|
|
) {
|
|
|
|
$changed[] = $newMount;
|
|
|
|
}
|
|
|
|
}
|
2016-01-11 12:06:10 +00:00
|
|
|
}
|
2016-07-13 14:29:51 +00:00
|
|
|
return $changed;
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function addToCache(ICachedMountInfo $mount) {
|
2016-06-13 11:31:45 +00:00
|
|
|
if ($mount->getStorageId() !== -1) {
|
|
|
|
$this->connection->insertIfNotExist('*PREFIX*mounts', [
|
|
|
|
'storage_id' => $mount->getStorageId(),
|
|
|
|
'root_id' => $mount->getRootId(),
|
|
|
|
'user_id' => $mount->getUser()->getUID(),
|
2016-07-13 14:29:51 +00:00
|
|
|
'mount_point' => $mount->getMountPoint(),
|
|
|
|
'mount_id' => $mount->getMountId()
|
2016-06-13 11:31:45 +00:00
|
|
|
], ['root_id', 'user_id']);
|
|
|
|
} else {
|
2016-08-25 16:06:13 +00:00
|
|
|
// in some cases this is legitimate, like orphaned shares
|
|
|
|
$this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint());
|
2016-06-13 11:31:45 +00:00
|
|
|
}
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
2016-07-13 14:29:51 +00:00
|
|
|
private function updateCachedMount(ICachedMountInfo $mount) {
|
2016-01-11 12:06:10 +00:00
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$query = $builder->update('mounts')
|
2016-12-13 11:10:44 +00:00
|
|
|
->set('storage_id', $builder->createNamedParameter($mount->getStorageId()))
|
2016-01-11 12:06:10 +00:00
|
|
|
->set('mount_point', $builder->createNamedParameter($mount->getMountPoint()))
|
2016-07-13 14:29:51 +00:00
|
|
|
->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT))
|
2016-01-11 12:06:10 +00:00
|
|
|
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
|
2016-01-26 13:50:57 +00:00
|
|
|
->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
|
2016-01-11 12:06:10 +00:00
|
|
|
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2015-11-26 16:47:53 +00:00
|
|
|
private function removeFromCache(ICachedMountInfo $mount) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$query = $builder->delete('mounts')
|
|
|
|
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
|
2016-01-26 13:50:57 +00:00
|
|
|
->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
|
2015-11-26 16:47:53 +00:00
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function dbRowToMountInfo(array $row) {
|
|
|
|
$user = $this->userManager->get($row['user_id']);
|
2016-11-24 10:44:18 +00:00
|
|
|
if (is_null($user)) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-27 21:22:35 +00:00
|
|
|
$mount_id = $row['mount_id'];
|
|
|
|
if (!is_null($mount_id)) {
|
2018-08-16 16:24:10 +00:00
|
|
|
$mount_id = (int)$mount_id;
|
2017-10-27 21:22:35 +00:00
|
|
|
}
|
|
|
|
return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $mount_id, isset($row['path']) ? $row['path'] : '');
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @return ICachedMountInfo[]
|
|
|
|
*/
|
|
|
|
public function getMountsForUser(IUser $user) {
|
2015-12-02 12:36:33 +00:00
|
|
|
if (!isset($this->mountsForUsers[$user->getUID()])) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
2016-09-18 16:36:53 +00:00
|
|
|
$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
|
|
|
|
->from('mounts', 'm')
|
|
|
|
->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
|
2015-12-02 12:36:33 +00:00
|
|
|
->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID())));
|
2015-11-26 16:47:53 +00:00
|
|
|
|
2015-12-02 12:36:33 +00:00
|
|
|
$rows = $query->execute()->fetchAll();
|
2015-11-26 16:47:53 +00:00
|
|
|
|
2016-11-24 10:44:18 +00:00
|
|
|
$this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
|
2015-12-02 12:36:33 +00:00
|
|
|
}
|
|
|
|
return $this->mountsForUsers[$user->getUID()];
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $numericStorageId
|
2017-04-21 15:11:26 +00:00
|
|
|
* @param string|null $user limit the results to a single user
|
2015-11-26 16:47:53 +00:00
|
|
|
* @return CachedMountInfo[]
|
|
|
|
*/
|
2017-04-21 15:11:26 +00:00
|
|
|
public function getMountsForStorageId($numericStorageId, $user = null) {
|
2015-11-26 16:47:53 +00:00
|
|
|
$builder = $this->connection->getQueryBuilder();
|
2016-09-18 16:36:53 +00:00
|
|
|
$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
|
|
|
|
->from('mounts', 'm')
|
2017-06-12 13:24:55 +00:00
|
|
|
->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
|
2016-01-26 13:50:57 +00:00
|
|
|
->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT)));
|
2015-11-26 16:47:53 +00:00
|
|
|
|
2017-04-21 15:11:26 +00:00
|
|
|
if ($user) {
|
|
|
|
$query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user)));
|
|
|
|
}
|
|
|
|
|
2015-11-26 16:47:53 +00:00
|
|
|
$rows = $query->execute()->fetchAll();
|
|
|
|
|
2016-11-24 10:44:18 +00:00
|
|
|
return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $rootFileId
|
|
|
|
* @return CachedMountInfo[]
|
|
|
|
*/
|
|
|
|
public function getMountsForRootId($rootFileId) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
2016-09-18 16:36:53 +00:00
|
|
|
$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
|
|
|
|
->from('mounts', 'm')
|
|
|
|
->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
|
2016-01-26 13:50:57 +00:00
|
|
|
->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT)));
|
2015-11-26 16:47:53 +00:00
|
|
|
|
|
|
|
$rows = $query->execute()->fetchAll();
|
|
|
|
|
2016-11-24 10:44:18 +00:00
|
|
|
return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|
2015-12-03 13:10:05 +00:00
|
|
|
|
2016-01-22 15:02:21 +00:00
|
|
|
/**
|
|
|
|
* @param $fileId
|
|
|
|
* @return array
|
|
|
|
* @throws \OCP\Files\NotFoundException
|
|
|
|
*/
|
|
|
|
private function getCacheInfoFromFileId($fileId) {
|
|
|
|
if (!isset($this->cacheInfoCache[$fileId])) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
2016-09-18 16:36:53 +00:00
|
|
|
$query = $builder->select('storage', 'path', 'mimetype')
|
2016-01-22 15:02:21 +00:00
|
|
|
->from('filecache')
|
2016-01-26 13:50:57 +00:00
|
|
|
->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
|
2016-01-22 15:02:21 +00:00
|
|
|
|
|
|
|
$row = $query->execute()->fetch();
|
|
|
|
if (is_array($row)) {
|
|
|
|
$this->cacheInfoCache[$fileId] = [
|
|
|
|
(int)$row['storage'],
|
2016-09-18 16:36:53 +00:00
|
|
|
$row['path'],
|
|
|
|
(int)$row['mimetype']
|
2016-01-22 15:02:21 +00:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
throw new NotFoundException('File with id "' . $fileId . '" not found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->cacheInfoCache[$fileId];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $fileId
|
2017-04-21 15:11:26 +00:00
|
|
|
* @param string|null $user optionally restrict the results to a single user
|
2017-08-22 13:44:52 +00:00
|
|
|
* @return ICachedMountFileInfo[]
|
2016-01-22 15:02:21 +00:00
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
2017-04-21 15:11:26 +00:00
|
|
|
public function getMountsForFileId($fileId, $user = null) {
|
2016-01-22 15:02:21 +00:00
|
|
|
try {
|
|
|
|
list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId);
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return [];
|
|
|
|
}
|
2017-04-21 15:11:26 +00:00
|
|
|
$mountsForStorage = $this->getMountsForStorageId($storageId, $user);
|
2016-01-22 15:02:21 +00:00
|
|
|
|
|
|
|
// filter mounts that are from the same storage but a different directory
|
2017-08-22 13:44:52 +00:00
|
|
|
$filteredMounts = array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) {
|
2016-01-22 15:02:21 +00:00
|
|
|
if ($fileId === $mount->getRootId()) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-18 16:36:53 +00:00
|
|
|
$internalMountPath = $mount->getRootInternalPath();
|
2016-01-22 15:02:21 +00:00
|
|
|
|
|
|
|
return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/';
|
|
|
|
});
|
2017-08-22 13:44:52 +00:00
|
|
|
|
|
|
|
return array_map(function (ICachedMountInfo $mount) use ($internalPath) {
|
|
|
|
return new CachedMountFileInfo(
|
|
|
|
$mount->getUser(),
|
|
|
|
$mount->getStorageId(),
|
|
|
|
$mount->getRootId(),
|
|
|
|
$mount->getMountPoint(),
|
|
|
|
$mount->getMountId(),
|
|
|
|
$mount->getRootInternalPath(),
|
|
|
|
$internalPath
|
|
|
|
);
|
|
|
|
}, $filteredMounts);
|
2016-01-22 15:02:21 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 13:10:05 +00:00
|
|
|
/**
|
|
|
|
* Remove all cached mounts for a user
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
*/
|
|
|
|
public function removeUserMounts(IUser $user) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$query = $builder->delete('mounts')
|
2016-01-11 12:06:10 +00:00
|
|
|
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID())));
|
2015-12-03 13:10:05 +00:00
|
|
|
$query->execute();
|
|
|
|
}
|
2016-01-13 14:29:53 +00:00
|
|
|
|
|
|
|
public function removeUserStorageMount($storageId, $userId) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$query = $builder->delete('mounts')
|
|
|
|
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId)))
|
2016-01-26 13:50:57 +00:00
|
|
|
->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
|
2016-01-13 14:29:53 +00:00
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remoteStorageMounts($storageId) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$query = $builder->delete('mounts')
|
2016-01-26 13:50:57 +00:00
|
|
|
->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
|
2016-01-13 14:29:53 +00:00
|
|
|
$query->execute();
|
|
|
|
}
|
2017-06-12 13:24:55 +00:00
|
|
|
|
2017-07-20 20:48:13 +00:00
|
|
|
/**
|
|
|
|
* @param array $users
|
|
|
|
* @return array
|
|
|
|
* @suppress SqlInjectionChecker
|
|
|
|
*/
|
2017-06-12 13:24:55 +00:00
|
|
|
public function getUsedSpaceForUsers(array $users) {
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
|
|
|
$slash = $builder->createNamedParameter('/');
|
|
|
|
|
|
|
|
$mountPoint = $builder->func()->concat(
|
|
|
|
$builder->func()->concat($slash, 'user_id'),
|
|
|
|
$slash
|
|
|
|
);
|
|
|
|
|
|
|
|
$userIds = array_map(function (IUser $user) {
|
|
|
|
return $user->getUID();
|
|
|
|
}, $users);
|
|
|
|
|
|
|
|
$query = $builder->select('m.user_id', 'f.size')
|
|
|
|
->from('mounts', 'm')
|
|
|
|
->innerJoin('m', 'filecache', 'f',
|
|
|
|
$builder->expr()->andX(
|
|
|
|
$builder->expr()->eq('m.storage_id', 'f.storage'),
|
2018-07-16 14:10:34 +00:00
|
|
|
$builder->expr()->eq('f.path_hash', $builder->createNamedParameter(md5('files')))
|
2017-06-12 13:24:55 +00:00
|
|
|
))
|
|
|
|
->where($builder->expr()->eq('m.mount_point', $mountPoint))
|
|
|
|
->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
|
2017-07-20 18:13:35 +00:00
|
|
|
$results = [];
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$results[$row['user_id']] = $row['size'];
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
return $results;
|
2017-06-12 13:24:55 +00:00
|
|
|
}
|
2015-11-26 16:47:53 +00:00
|
|
|
}
|