Merge pull request #1245 from nextcloud/get-external-mounts

get files_external mounts more efficiently
This commit is contained in:
Morris Jobke 2016-09-06 15:43:32 +02:00 committed by GitHub
commit 836c938583
3 changed files with 68 additions and 29 deletions

View File

@ -126,7 +126,7 @@ class ConfigAdapter implements IMountProvider {
$this->userStoragesService->setUser($user); $this->userStoragesService->setUser($user);
$this->userGlobalStoragesService->setUser($user); $this->userGlobalStoragesService->setUser($user);
foreach ($this->userGlobalStoragesService->getUniqueStorages() as $storage) { foreach ($this->userGlobalStoragesService->getAllStoragesForUser() as $storage) {
try { try {
$this->prepareStorageConfig($storage, $user); $this->prepareStorageConfig($storage, $user);
$impl = $this->constructStorage($storage); $impl = $this->constructStorage($storage);
@ -147,26 +147,7 @@ class ConfigAdapter implements IMountProvider {
$impl = new FailedStorage(['exception' => $e]); $impl = new FailedStorage(['exception' => $e]);
} }
$mount = new MountPoint( if ($storage->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) {
$impl,
'/' . $user->getUID() . '/files' . $storage->getMountPoint(),
null,
$loader,
$storage->getMountOptions(),
$storage->getId()
);
$mounts[$storage->getMountPoint()] = $mount;
}
foreach ($this->userStoragesService->getStorages() as $storage) {
try {
$this->prepareStorageConfig($storage, $user);
$impl = $this->constructStorage($storage);
} catch (\Exception $e) {
// propagate exception into filesystem
$impl = new FailedStorage(['exception' => $e]);
}
$mount = new PersonalMount( $mount = new PersonalMount(
$this->userStoragesService, $this->userStoragesService,
$storage->getId(), $storage->getId(),
@ -176,6 +157,16 @@ class ConfigAdapter implements IMountProvider {
$loader, $loader,
$storage->getMountOptions() $storage->getMountOptions()
); );
} else {
$mount = new MountPoint(
$impl,
'/' . $user->getUID() . '/files' . $storage->getMountPoint(),
null,
$loader,
$storage->getMountOptions(),
$storage->getId()
);
}
$mounts[$storage->getMountPoint()] = $mount; $mounts[$storage->getMountPoint()] = $mount;
} }

View File

@ -89,6 +89,29 @@ class DBConfigService {
return $this->getMountsFromQuery($query); return $this->getMountsFromQuery($query);
} }
public function getMountsForUser($userId, $groupIds) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type'])
->from('external_mounts', 'm')
->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id'))
->where($builder->expr()->orX(
$builder->expr()->andX( // global mounts
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)),
$builder->expr()->isNull('a.value')
),
$builder->expr()->andX( // mounts for user
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)),
$builder->expr()->eq('a.value', $builder->createNamedParameter($userId))
),
$builder->expr()->andX( // mounts for group
$builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GROUP, IQueryBuilder::PARAM_INT)),
$builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_INT_ARRAY))
)
));
return $this->getMountsFromQuery($query);
}
/** /**
* Get admin defined mounts * Get admin defined mounts
* *

View File

@ -172,4 +172,29 @@ class UserGlobalStoragesService extends GlobalStoragesService {
} }
return false; return false;
} }
/**
* Gets all storages for the user, admin, personal, global, etc
*
* @return StorageConfig[] array of storage configs
*/
public function getAllStoragesForUser() {
if (is_null($this->getUser())) {
return [];
}
$groupIds = $this->groupManager->getUserGroupIds($this->getUser());
$mounts = $this->dbConfig->getMountsForUser($this->getUser()->getUID(), $groupIds);
$configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts);
$configs = array_filter($configs, function ($config) {
return $config instanceof StorageConfig;
});
$keys = array_map(function (StorageConfig $config) {
return $config->getId();
}, $configs);
$storages = array_combine($keys, $configs);
return array_filter($storages, [$this, 'validateStorage']);
}
} }