diff --git a/apps/federatedfilesharing/appinfo/info.xml b/apps/federatedfilesharing/appinfo/info.xml index e4f7777cf249..2a0cbe3cd9da 100644 --- a/apps/federatedfilesharing/appinfo/info.xml +++ b/apps/federatedfilesharing/appinfo/info.xml @@ -20,4 +20,7 @@ OCA\FederatedFileSharing\Panels\GeneralPersonalPanel OCA\FederatedFileSharing\Panels\SharingPersonalPanel + + OCA\FederatedFileSharing\Command\SyncIncomingShares + diff --git a/apps/federatedfilesharing/lib/Command/SyncIncomingShares.php b/apps/federatedfilesharing/lib/Command/SyncIncomingShares.php new file mode 100644 index 000000000000..64555b1e4bf3 --- /dev/null +++ b/apps/federatedfilesharing/lib/Command/SyncIncomingShares.php @@ -0,0 +1,113 @@ + + * + * @copyright Copyright (c) 2019, 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 + * + */ + +namespace OCA\FederatedFileSharing\Command; + +use OC\ServerNotAvailableException; +use OCA\Files_Sharing\AppInfo\Application; +use OCP\Files\Storage\IStorage; +use OCP\Files\StorageInvalidException; +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; + + /** + * SyncIncomingShares constructor. + * + * @param IDBConnection $dbConnection + * @param IUserManager $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'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * + * @return int|null|void + */ + 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); + /** @var \OCA\Files_Sharing\External\Mount $mount */ + foreach ($userMounts as $mount) { + /** @var Storage $storage */ + $storage = $mount->getStorage(); + $this->refreshStorageRoot($storage); + } + } + $cursor->closeCursor(); + } + + /** + * @param IStorage $storage + */ + protected function refreshStorageRoot(IStorage $storage) { + try { + $localMtime = $storage->filemtime(''); + /** @var \OCA\Files_Sharing\External\Storage $storage */ + if ($storage->hasUpdated('', $localMtime)) { + try { + $storage->getScanner('')->scan('', false, 0); + } catch (LockedException $e) { + // it can be locked, let's skip it then + } catch (ServerNotAvailableException $e) { + // remote server hasn't responded + } + } + } catch (StorageNotAvailableException $e) { + // pass + } catch (StorageInvalidException $e) { + // pass + } + } +}