From aa660a347563c29cdfc0e4b5af8ff9f18e923a13 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 2 Oct 2013 13:26:38 +0200 Subject: [PATCH 1/4] remove deleted shares from the database table oc_share --- apps/files_sharing/lib/updater.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/apps/files_sharing/lib/updater.php b/apps/files_sharing/lib/updater.php index a43ab2e2a0..e0f8fc5f0f 100644 --- a/apps/files_sharing/lib/updater.php +++ b/apps/files_sharing/lib/updater.php @@ -57,6 +57,14 @@ class Shared_Updater { } } + private static function removeShare($path) { + $fileInfo = \OC\Files\Filesystem::getFileInfo($path); + $fileSource = $fileInfo['fileid']; + + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_source`=?'); + \OC_DB::executeAudited($query, array($fileSource)); + } + /** * @param array $params */ @@ -77,8 +85,10 @@ class Shared_Updater { */ static public function deleteHook($params) { self::correctFolders($params['path']); + self::removeShare($params['path']); } + /** * @param array $params */ From 401740166b6cea7a18ffb86c27c220a7606f14a1 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 4 Oct 2013 16:50:53 +0200 Subject: [PATCH 2/4] add comments --- apps/files_sharing/lib/updater.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apps/files_sharing/lib/updater.php b/apps/files_sharing/lib/updater.php index e0f8fc5f0f..e40cbc17dc 100644 --- a/apps/files_sharing/lib/updater.php +++ b/apps/files_sharing/lib/updater.php @@ -57,6 +57,11 @@ class Shared_Updater { } } + /** + * @brief remove all shares for a given file if the file was deleted + * + * @param string $path + */ private static function removeShare($path) { $fileInfo = \OC\Files\Filesystem::getFileInfo($path); $fileSource = $fileInfo['fileid']; From 0346167bb2ab3c26fed26415470d52c21110d70e Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 7 Oct 2013 10:59:09 +0200 Subject: [PATCH 3/4] clean up oc_share table from files which are no longer exists --- apps/files_sharing/appinfo/update.php | 22 ++++++++++++++++------ apps/files_sharing/appinfo/version | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/apps/files_sharing/appinfo/update.php b/apps/files_sharing/appinfo/update.php index 48e41e9304..bc33dd4043 100644 --- a/apps/files_sharing/appinfo/update.php +++ b/apps/files_sharing/appinfo/update.php @@ -68,11 +68,21 @@ if (version_compare($installedVersion, '0.3', '<')) { // $query = OCP\DB::prepare('DROP TABLE `*PREFIX*sharing`'); // $query->execute(); } -if (version_compare($installedVersion, '0.3.3', '<')) { - OC_User::useBackend(new OC_User_Database()); - OC_App::loadApps(array('authentication')); - $users = OC_User::getUsers(); - foreach ($users as $user) { -// OC_FileCache::delete('Shared', '/'.$user.'/files/'); + +// clean up oc_share table from files which are no longer exists +if (version_compare($installedVersion, '0.3.4', '<')) { + + // get all shares where the original file no longer exists + $findShares = \OC_DB::prepare('SELECT `file_source` FROM `*PREFIX*share` LEFT JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` WHERE `*PREFIX*filecache`.`fileid` IS NULL'); + $sharesFound = $findShares->execute(array())->fetchAll(); + + // delete those shares from the oc_share table + if (is_array($sharesFound) && !empty($sharesFound)) { + $delArray = array(); + foreach ($sharesFound as $share) { + $delArray[] = $share['file_source']; + } + $removeShares = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_source` IN (?)'); + $result = $removeShares->execute(array(implode(',', $delArray))); } } diff --git a/apps/files_sharing/appinfo/version b/apps/files_sharing/appinfo/version index 87a0871112..448a0fa11c 100644 --- a/apps/files_sharing/appinfo/version +++ b/apps/files_sharing/appinfo/version @@ -1 +1 @@ -0.3.3 \ No newline at end of file +0.3.4 \ No newline at end of file From 288f01bf6262d825527d71ba262bcaa5d082268d Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Mon, 7 Oct 2013 11:06:24 +0200 Subject: [PATCH 4/4] catch exception if db query execution fails --- apps/files_sharing/lib/updater.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/files_sharing/lib/updater.php b/apps/files_sharing/lib/updater.php index e40cbc17dc..08aaa62e25 100644 --- a/apps/files_sharing/lib/updater.php +++ b/apps/files_sharing/lib/updater.php @@ -67,7 +67,11 @@ class Shared_Updater { $fileSource = $fileInfo['fileid']; $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `file_source`=?'); - \OC_DB::executeAudited($query, array($fileSource)); + try { + \OC_DB::executeAudited($query, array($fileSource)); + } catch (\Exception $e) { + \OCP\Util::writeLog('files_sharing', "can't remove share: " . $e->getMessage(), \OCP\Util::WARN); + } } /**