目前owncloud的外接存储信息存放在data文件夹下的mount.json文件中,在这里我修改了这种存储方式使之成为了数据库存储方式:
一、建表:
DROP TABLE IF EXISTS `oc_mount`;
CREATE TABLE `oc_mount` (
`id` int(64) NOT NULL AUTO_INCREMENT COMMENT '主键',
`category` tinyint(1) DEFAULT '0' COMMENT '用户或者组标示符(0:user;1:group;)',
`name` varchar(64) COLLATE utf8_bin DEFAULT NULL COMMENT '用户uid或者组名',
`storage_name` varchar(64) COLLATE utf8_bin DEFAULT NULL COMMENT '使用该存储对应的文件名',
`class` varchar(64) COLLATE utf8_bin DEFAULT NULL COMMENT '使用的存储类',
`options` varchar(512) COLLATE utf8_bin DEFAULT NULL COMMENT '存储具体内容',
`priority` int(11) DEFAULT NULL COMMENT '优先级',
`storage_id` varchar(11) COLLATE utf8_bin DEFAULT NULL COMMENT '存储的id(从oc_storage中返回)',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
如上,建立oc_mount表
二、加入操作oc_mount数据库的方法
首先,在\lib\public\files\mount文件夹中加入了一个接口文件,叫做:IMountDatabase.php接口,具体接口内容如下:
<?php /** * Created by PhpStorm. * User: sunny * Date: 2015/9/16 * Time: 14:58 * 操作mount数据库信息的接口 */ namespace OCP\Files\Mount; interface IMountDatabase{ //获取所有的数据 public function getAllMountPoints(); //根据用户id以及storage_name获取mount数据 public function getMountPointByUserAndStorage($uid,$storage); //根据用户id获取mount数据 public function getMountPointByUser($uid); //根据传来的数据,将数据插入到数据库中 public function insertMountPoint($mountpoint); //根据传来的数据,将数据删除出数据库 public function deleteMountPoint($uid,$storage); }其次,在\lib\private\files\mount文件夹中实现这个接口,实现文件名叫mountdatabase.php,具体内容如下:
<?php /** * Created by PhpStorm. * User: sunny * Date: 2015/9/16 * Time: 14:56 */ namespace OC\Files\Mount; use OCP\Files\Mount\IMountDatabase; class MountDatabase implements IMountDatabase{ /** * 获取mount表中所有mount信息 * time:2015.9.16 * by sunny * @return 所有mount信息 */ public function getAllMountPoints() { $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*mount` '); $result = $query->execute(); $res=$result->fetchAll(); return $res; } /** * 获取对应用户名的mount信息 * time:2015.9.16 * by sunny * @return 某个mount信息 */ public function getMountPointByUserAndStorage($uid,$storage) { $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*mount` WHERE LOWER(`name`) = LOWER(?) AND `storage_name`=?'); $result = $query->execute(array($uid,$storage)); $res=$result->fetchRow(); return $res; } /** * 获取对应用户名的mount信息 * time:2015.9.16 * by sunny * @return 某个mount信息 */ public function getMountPointByUser($uid) { $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*mount` WHERE LOWER(`name`) = LOWER(?)'); $result = $query->execute(array($uid)); $res=$result->fetchRow(); return $res; } /** * 插入mount数据库 * time:2015.9.16 * by sunny * @return true or false */ public function insertMountPoint($mountpoint) { $name=key($mountpoint); $storage_name=key($mountpoint[$name]); $storage=$mountpoint[$name][$storage_name]; $class=$storage['class']; $option=json_encode($storage['options']); $priority=$storage['priority']; $storage_id=$storage['storage_id']; $query = \OC_DB::prepare('INSERT INTO `*PREFIX*mount` ( `category`, `name`, `storage_name`, `class`, `options`, `priority`, `storage_id` ) VALUES( ?, ?, ?, ?, ?, ?, ? )'); $result = $query->execute(array(0,$name, $storage_name,$class,$option,$priority,$storage_id)); return $result ? true : false; } /** * 根据用户uid删除mount信息 * time:2015.9.16 * by sunny * @return 删除某个mount信息 */ public function deleteMountPoint($uid,$storage) { $query = \OC_DB::prepare('DELETE FROM `*PREFIX*mount` WHERE `name` = ? AND `storage_name` = ?'); $result = $query->execute(array($uid,$storage)); return $result ? true : false; } }三、修改更新mount.json文件信息的文件,文件位置在:\apps\files_external\lib\config.php,具体更改一下方法:
1、addMountPoint方法:
/** * Add a mount point to the filesystem * * @param string $mountPoint Mount point * @param string $class Backend class * @param array $classOptions Backend parameters for the class * @param string $mountType MOUNT_TYPE_GROUP | MOUNT_TYPE_USER * @param string $applicable User or group to apply mount to * @param bool $isPersonal Personal or system mount point i.e. is this being called from the personal or admin page * @param int|null $priority Mount point priority, null for default * @return boolean */ public static function addMountPoint($mountPoint, $class, $classOptions, $mountType, $applicable, $isPersonal = false, $priority = null) { $backends = self::getBackends(); $mountPoint = OC\Files\Filesystem::normalizePath($mountPoint); $relMountPoint = $mountPoint; if ($mountPoint === '' || $mountPoint === '/') { // can't mount at root folder return false; } if (!isset($backends[$class])) { // invalid backend return false; } if ($isPersonal) { // Verify that the mount point applies for the current user // Prevent non-admin users from mounting local storage and other disabled backends $allowed_backends = self::getPersonalBackends(); if ($applicable != OCP\User::getUser() || !isset($allowed_backends[$class])) { return false; } $mountPoint = '/' . $applicable . '/files/' . ltrim($mountPoint, '/'); } else { $mountPoint = '/$user/files/' . ltrim($mountPoint, '/');//��moutPoint����Ҫ����� } //$mount数组,用于存到数据库 $mount = array($applicable => array( $mountPoint => array( 'class' => $class, 'options' => self::encryptPasswords($classOptions), 'priority'=> 100), ) ); //给$mount赋值storage_id self::addStorageId($mount[$applicable][$mountPoint]); //获取mount的数据库对象 $mountdatabase=new \OC\Files\Mount\MountDatabase(); $res=$mountdatabase->getMountPointByUserAndStorage($applicable,$mountPoint); //判断是否数据库已经存在,存在则返回true if(!empty($res)){ return '已经存在改数据!'; }else { //不存在则添加后返回true $flag = $mountdatabase->insertMountPoint($mount); /*if (!$isPersonal && !is_null($priority)) { $mount[$applicable][$mountPoint]['priority'] = $priority; } $mountPoints = self::readData($isPersonal ? OCP\User::getUser() : null); // who else loves multi-dimensional array ? $isNew = !isset($mountPoints[$mountType]) || !isset($mountPoints[$mountType][$applicable]) || !isset($mountPoints[$mountType][$applicable][$mountPoint]); $mountPoints = self::mergeMountPoints($mountPoints, $mount, $mountType); // Set default priority if none set if (!isset($mountPoints[$mountType][$applicable][$mountPoint]['priority'])) { if (isset($backends[$class]['priority'])) { $mountPoints[$mountType][$applicable][$mountPoint]['priority'] = $backends[$class]['priority']; } else { $mountPoints[$mountType][$applicable][$mountPoint]['priority'] = 100; } } self::writeData($isPersonal ? OCP\User::getUser() : null, $mountPoints);*/ $result = self::getBackendStatus($class, $classOptions, $isPersonal); if ($result && $flag) { \OC_Hook::emit( \OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create_mount, array( \OC\Files\Filesystem::signal_param_path => $relMountPoint, \OC\Files\Filesystem::signal_param_mount_type => $mountType, \OC\Files\Filesystem::signal_param_users => $applicable, ) ); } //return $result; return '新增数据成功!'; } }2、removeMountPoint方法:
/** * * @param string $mountPoint Mount point * @param string $mountType MOUNT_TYPE_GROUP | MOUNT_TYPE_USER * @param string $applicable User or group to remove mount from * @param bool $isPersonal Personal or system mount point * @return bool */ public static function removeMountPoint($mountPoint, $mountType, $applicable, $isPersonal = false) { // Verify that the mount point applies for the current user $relMountPoints = $mountPoint; if ($isPersonal) { if ($applicable != OCP\User::getUser()) { return false; } $mountPoint = '/' . $applicable . '/files/' . ltrim($mountPoint, '/'); } else { $mountPoint = '/$user/files/' . ltrim($mountPoint, '/'); } $mountPoint = \OC\Files\Filesystem::normalizePath($mountPoint); //从mount数据库删除数据 $mountdatabase=new \OC\Files\Mount\MountDatabase(); $res=$mountdatabase->getMountPointByUserAndStorage($applicable,$mountPoint); if(empty($res)){ return false; }else { $result = $mountdatabase->deleteMountPoint($applicable, $mountPoint); /*$mountPoints = self::readData($isPersonal ? OCP\User::getUser() : null); // Remove mount point unset($mountPoints[$mountType][$applicable][$mountPoint]); // Unset parent arrays if empty if (empty($mountPoints[$mountType][$applicable])) { unset($mountPoints[$mountType][$applicable]); if (empty($mountPoints[$mountType])) { unset($mountPoints[$mountType]); } } self::writeData($isPersonal ? OCP\User::getUser() : null, $mountPoints);*/ \OC_Hook::emit( \OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_delete_mount, array( \OC\Files\Filesystem::signal_param_path => $relMountPoints, \OC\Files\Filesystem::signal_param_mount_type => $mountType, \OC\Files\Filesystem::signal_param_users => $applicable, ) ); //return true; return $result; } }3、readData方法:
/** * Read the mount points in the config file into an array * * @param string|null $user If not null, personal for $user, otherwise system * @return array */ private static function readData($user = null) { /*$parser = new \OC\ArrayParser(); if (isset($user)) { $phpFile = OC_User::getHome($user) . '/mount.php'; $jsonFile = OC_User::getHome($user) . '/mount.json'; } else { $phpFile = OC::$SERVERROOT . '/config/mount.php'; $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data/'); $jsonFile = \OC_Config::getValue('mount_file', $datadir . '/mount.json'); } if (is_file($jsonFile)) { $mountPoints = json_decode(file_get_contents($jsonFile), true); if (is_array($mountPoints)) { return $mountPoints; } } elseif (is_file($phpFile)) { $mountPoints = $parser->parsePHP(file_get_contents($phpFile)); if (is_array($mountPoints)) { return $mountPoints; } } return array();*/ //修改从json文件中获取变为从数据库中获取mount信息 $mountdatabase=new \OC\Files\Mount\MountDatabase(); $result=$mountdatabase->getAllMountPoints(); if(!empty($result)) { $users = array(); foreach ($result as $res) { $options = json_decode($res['options'], true); $arr = array($res['name'] => array( $res['storage_name']=>array( 'class' => $res['class'], 'options' => $options, 'priority' => $res['priority'], 'storage_id' => $res['storage_id'] ) ) ); $users = $users+$arr; } $mountpoints = array('user' => $users); return $mountpoints; }else{ return array(); } }以上,至此,我们就实现了从存储到mount.json文件转换为存储到数据库oc_mount表,但是以上实现知识局限在存储分组为user的数据,因为上面代码固定写死了。
最后、
实现数据库存储之后还没有真正利用数据库存储的优势来进行增删改查操作,所以后期要继续改进一些方法。