Skip to content

Commit

Permalink
Delete orphaned shares in a background job
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Petry committed Mar 3, 2015
1 parent be27188 commit ab5e634
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
30 changes: 30 additions & 0 deletions apps/files_sharing/command/deleteorphanedsharescommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Copyright (c) 2015 Vincent Petry <[email protected]>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/

namespace OCA\Files_sharing\Command;

use OCP\Command\ICommand;
use OCP\IDBConnection;

/**
* Delete all share entries that have no matching entries in the file cache table.
*/
class DeleteOrphanedSharesCommand implements ICommand {
public function __construct() {
}

public function handle() {
$connection = \OC::$server->getDatabaseConnection();
$connection->executeUpdate(
'DELETE `s` FROM `*PREFIX*share` `s` ' .
'LEFT JOIN `*PREFIX*filecache` `f` ON `s`.`file_source`=`f`.`fileid` ' .
'WHERE `f`.`fileid` IS NULL;'
);
}

}
14 changes: 14 additions & 0 deletions apps/files_sharing/lib/updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace OC\Files\Cache;

use OCA\Files_sharing\Command\DeleteOrphanedSharesCommand;

class Shared_Updater {

// shares which can be removed from oc_share after the delete operation was successful
Expand Down Expand Up @@ -92,10 +94,22 @@ private static function removeShare($path) {
} catch (\Exception $e) {
\OCP\Util::writeLog('files_sharing', "can't remove share: " . $e->getMessage(), \OCP\Util::WARN);
}

// it might have been a folder, schedule deletion of orphaned shares
self::scheduleDeleteOrphanedShares();
}
unset(self::$toRemove[$path]);
}

/**
* Schedule a background job for the deletion of orphaned shares
*/
static private function scheduleDeleteOrphanedShares() {
\OC::$server->getCommandBus()->push(
new DeleteOrphanedSharesCommand()
);
}

/**
* @param array $params
*/
Expand Down
22 changes: 22 additions & 0 deletions apps/files_sharing/tests/updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ function testDeleteParentFolder() {
\OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
}

function testDeleteFolderWithOutgoingSharesInside() {
OC_FileProxy::register(new OCA\Files\Share\Proxy());

$fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder . '/' . $this->filename);
$this->assertTrue($fileinfo instanceof \OC\Files\FileInfo);

\OCP\Share::shareItem('file', $fileinfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 31);

$foldersShared = \OCP\Share::getItemsShared('file');
$this->assertCount(1, $foldersShared);

$this->view->unlink($this->folder);

$foldersShared = \OCP\Share::getItemsShared('file');
$this->assertCount(1, $foldersShared);

// TODO: run commands

$foldersShared = \OCP\Share::getItemsShared('file');
$this->assertCount(0, $foldersShared);
}

/**
* if a file gets shared the etag for the recipients root should change
*/
Expand Down

0 comments on commit ab5e634

Please sign in to comment.