Skip to content

Commit

Permalink
Add a command to check federated share updates
Browse files Browse the repository at this point in the history
  • Loading branch information
VicDeo committed Mar 26, 2019
1 parent 67c4ba1 commit da39051
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/federatedfilesharing/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@
<personal>OCA\FederatedFileSharing\Panels\GeneralPersonalPanel</personal>
<personal>OCA\FederatedFileSharing\Panels\SharingPersonalPanel</personal>
</settings>
<commands>
<command>OCA\FederatedFileSharing\Command\SyncIncomingShares</command>
</commands>
</info>
86 changes: 86 additions & 0 deletions apps/federatedfilesharing/lib/Command/SyncIncomingShares.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* @author Viktar Dubiniuk <dubiniuk@owncloud.com>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\FederatedFileSharing\Command;

use OCA\Files_Sharing\AppInfo\Application;
use OCP\Files\StorageNotAvailableException;
use OCP\IDBConnection;
use OCP\IUserManager;
use OCP\Lock\LockedException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SyncIncomingShares extends Command {
/** @var IDBConnection */
private $dbConnection;

/** @var IUserManager */
private $userManager;

public function __construct(IDBConnection $dbConnection, IUserManager $userManager) {
parent::__construct();
$this->dbConnection = $dbConnection;
$this->userManager = $userManager;
}

protected function configure() {
$this
->setName('incomingShares:sync')
->setDescription('Sync external shares');
}

public function execute(InputInterface $input, OutputInterface $output) {
$app = new Application();
/** @var \OCA\Files_Sharing\External\MountProvider $mountProvider */
$mountProvider = $app->getContainer()->query('ExternalMountProvider');
$loader = \OC\Files\Filesystem::getLoader();

$qb = $this->dbConnection->getQueryBuilder();
$qb->selectDistinct('user')
->from('share_external')
->where($qb->expr()->eq('accepted', $qb->expr()->literal('1')));
$cursor = $qb->execute();
while ($data = $cursor->fetch()) {
$user = $this->userManager->get($data['user']);
$userMounts = $mountProvider->getMountsForUser($user, $loader);
foreach ($userMounts as $mount) {
/** @var \OCA\Files_Sharing\External\Storage $storage */
$storage = $mount->getStorage();
try {
$localMtime = $storage->filemtime('');
if ($storage->hasUpdated('', $localMtime)) {
// sync
try {
$storage->getScanner('')->scan('', false, 0);
} catch (LockedException $e) {
// it can be locked, let's skip it then
}
}
} catch (StorageNotAvailableException $e) {
// pass
}
}
}
$cursor->closeCursor();
}
}

0 comments on commit da39051

Please sign in to comment.