Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a public api for apps to add mounts #12577

Merged
merged 2 commits into from
Dec 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/files_external/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,5 @@
'password' => '*'.$l->t('Password'),
'root' => '&'.$l->t('Root'))));

$mountProvider = new \OCA\Files_External\Config\ConfigAdapter();
\OC::$server->getMountProviderCollection()->registerProvider($mountProvider);
16 changes: 0 additions & 16 deletions apps/files_external/lib/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,6 @@ public static function getBackends() {
* @param array $data
*/
public static function initMountPointsHook($data) {
$mountPoints = self::getAbsoluteMountPoints($data['user']);
$loader = \OC\Files\Filesystem::getLoader();
$manager = \OC\Files\Filesystem::getMountManager();
foreach ($mountPoints as $mountPoint => $options) {
if (isset($options['options']['objectstore'])) {
$objectClass = $options['options']['objectstore']['class'];
$options['options']['objectstore'] = new $objectClass($options['options']['objectstore']);
}
if (isset($options['personal']) && $options['personal']) {
$mount = new \OCA\Files_External\PersonalMount($options['class'], $mountPoint, $options['options'], $loader);
} else{
$mount = new \OC\Files\Mount\Mount($options['class'], $mountPoint, $options['options'], $loader);
}
$manager->addMount($mount);
}

if ($data['user']) {
$user = \OC::$server->getUserManager()->get($data['user']);
if (!$user) {
Expand Down
44 changes: 44 additions & 0 deletions apps/files_external/lib/config/configadapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/

namespace OCA\Files_External\Config;

use OC\Files\Mount\MountPoint;
use OCP\Files\Storage\IStorageFactory;
use OCA\Files_External\PersonalMount;
use OCP\Files\Config\IMountProvider;
use OCP\IUser;

/**
* Make the old files_external config work with the new public mount config api
*/
class ConfigAdapter implements IMountProvider {
/**
* Get all mountpoints applicable for the user
*
* @param \OCP\IUser $user
* @param \OCP\Files\Storage\IStorageFactory $loader
* @return \OCP\Files\Mount\IMountPoint[]
*/
public function getMountsForUser(IUser $user, IStorageFactory $loader) {
$mountPoints = \OC_Mount_Config::getAbsoluteMountPoints($user->getUID());
$mounts = array();
foreach ($mountPoints as $mountPoint => $options) {
if (isset($options['options']['objectstore'])) {
$objectClass = $options['options']['objectstore']['class'];
$options['options']['objectstore'] = new $objectClass($options['options']['objectstore']);
}
if (isset($options['personal']) && $options['personal']) {
$mounts[] = new PersonalMount($options['class'], $mountPoint, $options['options'], $loader);
} else {
$mounts[] = new MountPoint($options['class'], $mountPoint, $options['options'], $loader);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, are PersonalMount and MountPoint public classes ? Or should we also "hide" them behind factories ?

In general it feels a bit uncomfortable when I see mountpoints instantiated directly like this.
What do you think ?

Also: will other external storage apps also need to instantiate new MountPoint() directly ? Do they need to implement their own IMountPoint ?

Edit: wording

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current mount providers need to instantiate the mounts themselves since the class used determines behavior such as mount moving

}
}
return $mounts;
}
}
4 changes: 2 additions & 2 deletions apps/files_external/lib/personalmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

namespace OCA\Files_External;

use OC\Files\Mount\Mount;
use OC\Files\Mount\MountPoint;
use OC\Files\Mount\MoveableMount;

/**
* Person mount points can be moved by the user
*/
class PersonalMount extends Mount implements MoveableMount {
class PersonalMount extends MountPoint implements MoveableMount {
/**
* Move the mount point to $target
*
Expand Down
6 changes: 3 additions & 3 deletions apps/files_sharing/lib/external/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Manager {
private $mountManager;

/**
* @var \OC\Files\Storage\Loader
* @var \OC\Files\Storage\StorageFactory
*/
private $storageLoader;

Expand All @@ -37,10 +37,10 @@ class Manager {
* @param \OCP\IDBConnection $connection
* @param \OC\Files\Mount\Manager $mountManager
* @param \OC\User\Session $userSession
* @param \OC\Files\Storage\Loader $storageLoader
* @param \OC\Files\Storage\StorageFactory $storageLoader
*/
public function __construct(\OCP\IDBConnection $connection, \OC\Files\Mount\Manager $mountManager,
\OC\Files\Storage\Loader $storageLoader, \OC\User\Session $userSession) {
\OC\Files\Storage\StorageFactory $storageLoader, \OC\User\Session $userSession) {
$this->connection = $connection;
$this->mountManager = $mountManager;
$this->userSession = $userSession;
Expand Down
5 changes: 3 additions & 2 deletions apps/files_sharing/lib/external/mount.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

namespace OCA\Files_Sharing\External;

use OC\Files\Mount\MountPoint;
use OC\Files\Mount\MoveableMount;

class Mount extends \OC\Files\Mount\Mount implements MoveableMount {
class Mount extends MountPoint implements MoveableMount {

/**
* @var \OCA\Files_Sharing\External\Manager
Expand All @@ -22,7 +23,7 @@ class Mount extends \OC\Files\Mount\Mount implements MoveableMount {
* @param string $mountpoint
* @param array $options
* @param \OCA\Files_Sharing\External\Manager $manager
* @param \OC\Files\Storage\Loader $loader
* @param \OC\Files\Storage\StorageFactory $loader
*/
public function __construct($storage, $mountpoint, $options, $manager, $loader = null) {
parent::__construct($storage, $mountpoint, $options, $loader);
Expand Down
4 changes: 2 additions & 2 deletions apps/files_sharing/lib/sharedmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

namespace OCA\Files_Sharing;

use OC\Files\Mount\Mount;
use OC\Files\Mount\MountPoint;
use OC\Files\Mount\MoveableMount;

/**
* Shared mount points can be moved by the user
*/
class SharedMount extends Mount implements MoveableMount {
class SharedMount extends MountPoint implements MoveableMount {
/**
* @var \OC\Files\Storage\Shared $storage
*/
Expand Down
55 changes: 55 additions & 0 deletions lib/private/files/config/mountprovidercollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/

namespace OC\Files\Config;

use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\Config\IMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OCP\IUser;

class MountProviderCollection implements IMountProviderCollection {
/**
* @var \OCP\Files\Config\IMountProvider[]
*/
private $providers = array();

/**
* @var \OCP\Files\Storage\IStorageFactory
*/
private $loader;

/**
* @param \OCP\Files\Storage\IStorageFactory $loader
*/
public function __construct(IStorageFactory $loader) {
$this->loader = $loader;
}

/**
* Get all configured mount points for the user
*
* @param \OCP\IUser $user
* @return \OCP\Files\Mount\IMountPoint[]
*/
public function getMountsForUser(IUser $user) {
$loader = $this->loader;
return array_reduce($this->providers, function ($mounts, IMountProvider $provider) use ($user, $loader) {
return array_merge($mounts, $provider->getMountsForUser($user, $loader));
}, array());
}

/**
* Add a provider for mount points
*
* @param \OCP\Files\Config\IMountProvider $provider
*/
public function registerProvider(IMountProvider $provider) {
$this->providers[] = $provider;
}
}
17 changes: 11 additions & 6 deletions lib/private/files/filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

namespace OC\Files;

use OC\Files\Storage\Loader;
use OC\Files\Storage\StorageFactory;

class Filesystem {

Expand Down Expand Up @@ -165,7 +165,7 @@ class Filesystem {
const signal_param_users = 'users';

/**
* @var \OC\Files\Storage\Loader $loader
* @var \OC\Files\Storage\StorageFactory $loader
*/
private static $loader;

Expand All @@ -183,7 +183,7 @@ public static function addStorageWrapper($wrapperName, $wrapper) {

public static function getLoader() {
if (!self::$loader) {
self::$loader = new Loader();
self::$loader = new StorageFactory();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a OC::$server service instead ? 😄

}
return self::$loader;
}
Expand Down Expand Up @@ -250,7 +250,7 @@ public static function getStorage($mountPoint) {

/**
* @param string $id
* @return Mount\Mount[]
* @return Mount\MountPoint[]
*/
public static function getMountByStorageId($id) {
if (!self::$mounts) {
Expand All @@ -261,7 +261,7 @@ public static function getMountByStorageId($id) {

/**
* @param int $id
* @return Mount\Mount[]
* @return Mount\MountPoint[]
*/
public static function getMountByNumericId($id) {
if (!self::$mounts) {
Expand Down Expand Up @@ -370,6 +370,11 @@ public static function initMountPoints($user = '') {
self::mountCacheDir($user);

// Chance to mount for other storages
if($userObject) {
$mountConfigManager = \OC::$server->getMountProviderCollection();
$mounts = $mountConfigManager->getMountsForUser($userObject);
array_walk($mounts, array(self::$mounts, 'addMount'));
}
\OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user, 'user_dir' => $root));
}

Expand Down Expand Up @@ -447,7 +452,7 @@ static public function mount($class, $arguments, $mountpoint) {
if (!self::$mounts) {
\OC_Util::setupFS();
}
$mount = new Mount\Mount($class, $mountpoint, $arguments, self::getLoader());
$mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader());
self::$mounts->addMount($mount);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/private/files/mount/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

class Manager {
/**
* @var Mount[]
* @var MountPoint[]
*/
private $mounts = array();

/**
* @param Mount $mount
* @param MountPoint $mount
*/
public function addMount(Mount $mount) {
public function addMount(MountPoint $mount) {
$this->mounts[$mount->getMountPoint()] = $mount;
}

Expand Down Expand Up @@ -47,7 +47,7 @@ public function moveMount($mountPoint, $target){
* Find the mount for $path
*
* @param string $path
* @return Mount
* @return MountPoint
*/
public function find($path) {
\OC_Util::setupFS();
Expand Down Expand Up @@ -75,7 +75,7 @@ public function find($path) {
* Find all mounts in $path
*
* @param string $path
* @return Mount[]
* @return MountPoint[]
*/
public function findIn($path) {
\OC_Util::setupFS();
Expand All @@ -99,7 +99,7 @@ public function clear() {
* Find mounts by storage id
*
* @param string $id
* @return Mount[]
* @return MountPoint[]
*/
public function findByStorageId($id) {
\OC_Util::setupFS();
Expand All @@ -116,7 +116,7 @@ public function findByStorageId($id) {
}

/**
* @return Mount[]
* @return MountPoint[]
*/
public function getAll() {
return $this->mounts;
Expand All @@ -126,7 +126,7 @@ public function getAll() {
* Find mounts by numeric storage id
*
* @param int $id
* @return Mount[]
* @return MountPoint[]
*/
public function findByNumericId($id) {
$storageId = \OC\Files\Cache\Storage::getStorageId($id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
namespace OC\Files\Mount;

use \OC\Files\Filesystem;
use OC\Files\Storage\Loader;
use OC\Files\Storage\StorageFactory;
use OC\Files\Storage\Storage;
use OCP\Files\Mount\IMountPoint;

class Mount {
class MountPoint implements IMountPoint {
/**
* @var \OC\Files\Storage\Storage $storage
*/
Expand All @@ -23,22 +24,22 @@ class Mount {
protected $mountPoint;

/**
* @var \OC\Files\Storage\Loader $loader
* @var \OC\Files\Storage\StorageFactory $loader
*/
private $loader;

/**
* @param string|\OC\Files\Storage\Storage $storage
* @param string $mountpoint
* @param array $arguments (optional)\
* @param \OC\Files\Storage\Loader $loader
* @param \OCP\Files\Storage\IStorageFactory $loader
*/
public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
if (is_null($arguments)) {
$arguments = array();
}
if (is_null($loader)) {
$this->loader = new Loader();
$this->loader = new StorageFactory();
} else {
$this->loader = $loader;
}
Expand Down Expand Up @@ -67,15 +68,6 @@ public function getMountPoint() {
return $this->mountPoint;
}

/**
* get name of the mount point
*
* @return string
*/
public function getMountPointName() {
return basename(rtrim($this->mountPoint, '/'));
}

/**
* @param string $mountPoint new mount point
*/
Expand All @@ -91,7 +83,7 @@ public function setMountPoint($mountPoint) {
private function createStorage() {
if (class_exists($this->class)) {
try {
return $this->loader->load($this->mountPoint, $this->class, $this->arguments);
return $this->loader->getInstance($this->mountPoint, $this->class, $this->arguments);
} catch (\Exception $exception) {
if ($this->mountPoint === '/') {
// the root storage could not be initialized, show the user!
Expand Down
Loading