diff options
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Files/AppData/AppData.php | 16 | ||||
-rw-r--r-- | lib/private/Files/AppData/Factory.php | 3 | ||||
-rw-r--r-- | lib/private/Preview/BackgroundCleanupJob.php | 91 | ||||
-rw-r--r-- | lib/private/Preview/Watcher.php | 44 | ||||
-rw-r--r-- | lib/private/Preview/WatcherConnector.php | 11 | ||||
-rw-r--r-- | lib/private/Repair.php | 4 | ||||
-rw-r--r-- | lib/private/Repair/NC14/AddPreviewBackgroundCleanupJob.php | 48 | ||||
-rw-r--r-- | lib/private/Setup.php | 2 |
8 files changed, 161 insertions, 58 deletions
diff --git a/lib/private/Files/AppData/AppData.php b/lib/private/Files/AppData/AppData.php index 270e834b8e5..e25bf450446 100644 --- a/lib/private/Files/AppData/AppData.php +++ b/lib/private/Files/AppData/AppData.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> * @@ -31,6 +32,7 @@ use OC\SystemConfig; use OCP\Files\Node; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\Files\SimpleFS\ISimpleFolder; class AppData implements IAppData { @@ -55,7 +57,7 @@ class AppData implements IAppData { */ public function __construct(IRootFolder $rootFolder, SystemConfig $systemConfig, - $appId) { + string $appId) { $this->rootFolder = $rootFolder; $this->config = $systemConfig; @@ -66,7 +68,7 @@ class AppData implements IAppData { * @return Folder * @throws \RuntimeException */ - private function getAppDataFolder() { + private function getAppDataFolder(): Folder { if ($this->folder === null) { $instanceId = $this->config->getValue('instanceid', null); if ($instanceId === null) { @@ -101,20 +103,20 @@ class AppData implements IAppData { return $this->folder; } - public function getFolder($name) { + public function getFolder(string $name): ISimpleFolder { $node = $this->getAppDataFolder()->get($name); /** @var Folder $node */ return new SimpleFolder($node); } - public function newFolder($name) { + public function newFolder(string $name): ISimpleFolder { $folder = $this->getAppDataFolder()->newFolder($name); return new SimpleFolder($folder); } - public function getDirectoryListing() { + public function getDirectoryListing(): array { $listing = $this->getAppDataFolder()->getDirectoryListing(); $fileListing = array_map(function(Node $folder) { @@ -128,4 +130,8 @@ class AppData implements IAppData { return array_values($fileListing); } + + public function getId(): int { + return $this->getAppDataFolder()->getId(); + } } diff --git a/lib/private/Files/AppData/Factory.php b/lib/private/Files/AppData/Factory.php index 85c75733796..fba2232db06 100644 --- a/lib/private/Files/AppData/Factory.php +++ b/lib/private/Files/AppData/Factory.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl> * @@ -44,7 +45,7 @@ class Factory { * @param string $appId * @return AppData */ - public function get($appId) { + public function get(string $appId): AppData { return new AppData($this->rootFolder, $this->config, $appId); } } diff --git a/lib/private/Preview/BackgroundCleanupJob.php b/lib/private/Preview/BackgroundCleanupJob.php new file mode 100644 index 00000000000..25bf354e28b --- /dev/null +++ b/lib/private/Preview/BackgroundCleanupJob.php @@ -0,0 +1,91 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @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 OC\Preview; + +use OC\BackgroundJob\TimedJob; +use OC\Files\AppData\Factory; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\IDBConnection; + +class BackgroundCleanupJob extends TimedJob { + + /** @var IDBConnection */ + private $connection; + + /** @var Factory */ + private $appDataFactory; + + /** @var bool */ + private $isCLI; + + public function __construct(IDBConnection $connection, + Factory $appDataFactory, + bool $isCLI) { + // Run at most once an hour + $this->setInterval(3600); + + $this->connection = $connection; + $this->appDataFactory = $appDataFactory; + $this->isCLI = $isCLI; + } + + public function run($argument) { + $previews = $this->appDataFactory->get('preview'); + + $previewFodlerId = $previews->getId(); + + $qb = $this->connection->getQueryBuilder(); + $qb->select('a.name') + ->from('filecache', 'a') + ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq( + $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid' + )) + ->where( + $qb->expr()->isNull('b.fileid') + )->andWhere( + $qb->expr()->eq('a.parent', $qb->createNamedParameter($previewFodlerId)) + ); + + if (!$this->isCLI) { + $qb->setMaxResults(10); + } + + $cursor = $qb->execute(); + + while ($row = $cursor->fetch()) { + try { + $preview = $previews->getFolder($row['name']); + $preview->delete(); + } catch (NotFoundException $e) { + // continue + } catch (NotPermittedException $e) { + // continue + } + } + + $cursor->closeCursor(); + } +} diff --git a/lib/private/Preview/Watcher.php b/lib/private/Preview/Watcher.php index 8d091b84b0e..be462d9c935 100644 --- a/lib/private/Preview/Watcher.php +++ b/lib/private/Preview/Watcher.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> * @@ -22,7 +23,6 @@ */ namespace OC\Preview; -use OCP\Files\File; use OCP\Files\Node; use OCP\Files\Folder; use OCP\Files\IAppData; @@ -39,9 +39,6 @@ class Watcher { /** @var IAppData */ private $appData; - /** @var int[] */ - private $toDelete = []; - /** * Watcher constructor. * @@ -58,47 +55,10 @@ class Watcher { } try { - $folder = $this->appData->getFolder($node->getId()); + $folder = $this->appData->getFolder((string)$node->getId()); $folder->delete(); } catch (NotFoundException $e) { //Nothing to do } } - - public function preDelete(Node $node) { - // To avoid cycles - if ($this->toDelete !== []) { - return; - } - - if ($node instanceof File) { - $this->toDelete[] = $node->getId(); - return; - } - - /** @var Folder $node */ - $this->deleteFolder($node); - } - - private function deleteFolder(Folder $folder) { - $nodes = $folder->getDirectoryListing(); - foreach ($nodes as $node) { - if ($node instanceof File) { - $this->toDelete[] = $node->getId(); - } else if ($node instanceof Folder) { - $this->deleteFolder($node); - } - } - } - - public function postDelete(Node $node) { - foreach ($this->toDelete as $fid) { - try { - $folder = $this->appData->getFolder($fid); - $folder->delete(); - } catch (NotFoundException $e) { - // continue - } - } - } } diff --git a/lib/private/Preview/WatcherConnector.php b/lib/private/Preview/WatcherConnector.php index 4e6e786cec7..bf9e6c29e4f 100644 --- a/lib/private/Preview/WatcherConnector.php +++ b/lib/private/Preview/WatcherConnector.php @@ -1,4 +1,5 @@ <?php +declare(strict_types=1); /** * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> * @@ -49,7 +50,7 @@ class WatcherConnector { /** * @return Watcher */ - private function getWatcher() { + private function getWatcher(): Watcher { return \OC::$server->query(Watcher::class); } @@ -59,14 +60,6 @@ class WatcherConnector { $this->root->listen('\OC\Files', 'postWrite', function (Node $node) { $this->getWatcher()->postWrite($node); }); - - $this->root->listen('\OC\Files', 'preDelete', function (Node $node) { - $this->getWatcher()->preDelete($node); - }); - - $this->root->listen('\OC\Files', 'postDelete', function (Node $node) { - $this->getWatcher()->postDelete($node); - }); } } } diff --git a/lib/private/Repair.php b/lib/private/Repair.php index a257ef061e7..8746f1e6f27 100644 --- a/lib/private/Repair.php +++ b/lib/private/Repair.php @@ -36,6 +36,7 @@ use OC\Repair\Collation; use OC\Repair\MoveUpdaterStepFile; use OC\Repair\NC11\FixMountStorages; use OC\Repair\NC13\AddLogRotateJob; +use OC\Repair\NC14\AddPreviewBackgroundCleanupJob; use OC\Repair\OldGroupMembershipShares; use OC\Repair\Owncloud\DropAccountTermsTable; use OC\Repair\Owncloud\SaveAccountsTableData; @@ -132,7 +133,8 @@ class Repair implements IOutput{ new FixMountStorages(\OC::$server->getDatabaseConnection()), new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()), new AddLogRotateJob(\OC::$server->getJobList()), - new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)) + new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)), + new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()), ]; } diff --git a/lib/private/Repair/NC14/AddPreviewBackgroundCleanupJob.php b/lib/private/Repair/NC14/AddPreviewBackgroundCleanupJob.php new file mode 100644 index 00000000000..b58fabcba50 --- /dev/null +++ b/lib/private/Repair/NC14/AddPreviewBackgroundCleanupJob.php @@ -0,0 +1,48 @@ +<?php +declare(strict_types=1); +/** + * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OC\Repair\NC14; + +use OC\Preview\BackgroundCleanupJob; +use OCP\BackgroundJob\IJobList; +use OCP\Migration\IOutput; +use OCP\Migration\IRepairStep; + +class AddPreviewBackgroundCleanupJob implements IRepairStep { + + /** @var IJobList */ + private $jobList; + + public function __construct(IJobList $jobList) { + $this->jobList = $jobList; + } + + public function getName(): string { + return 'Add preview background cleanup job'; + } + + public function run(IOutput $output) { + $this->jobList->add(BackgroundCleanupJob::class); + } + +} diff --git a/lib/private/Setup.php b/lib/private/Setup.php index 5564bb5b072..25e0b4d8817 100644 --- a/lib/private/Setup.php +++ b/lib/private/Setup.php @@ -47,6 +47,7 @@ use OC\App\AppStore\Bundles\BundleFetcher; use OC\Authentication\Token\DefaultTokenCleanupJob; use OC\Authentication\Token\DefaultTokenProvider; use OC\Log\Rotate; +use OC\Preview\BackgroundCleanupJob; use OCP\Defaults; use OCP\IL10N; use OCP\ILogger; @@ -419,6 +420,7 @@ class Setup { $jobList = \OC::$server->getJobList(); $jobList->add(DefaultTokenCleanupJob::class); $jobList->add(Rotate::class); + $jobList->add(BackgroundCleanupJob::class); } /** |