diff --git a/lib/private/Preview/BackgroundCleanupJob.php b/lib/private/Preview/BackgroundCleanupJob.php index ab40aeaaa79bd..28dca276026e0 100644 --- a/lib/private/Preview/BackgroundCleanupJob.php +++ b/lib/private/Preview/BackgroundCleanupJob.php @@ -32,9 +32,13 @@ use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\IDBConnection; +use OCP\IConfig; class BackgroundCleanupJob extends TimedJob { + /** @var IConfig */ + private $config; + /** @var IDBConnection */ private $connection; @@ -47,13 +51,15 @@ class BackgroundCleanupJob extends TimedJob { /** @var IMimeTypeLoader */ private $mimeTypeLoader; - public function __construct(IDBConnection $connection, + public function __construct(IConfig $config, + IDBConnection $connection, Root $previewFolder, IMimeTypeLoader $mimeTypeLoader, bool $isCLI) { // Run at most once an hour $this->setInterval(3600); + $this->config = $config; $this->connection = $connection; $this->previewFolder = $previewFolder; $this->isCLI = $isCLI; @@ -61,6 +67,10 @@ public function __construct(IDBConnection $connection, } public function run($argument) { + if (!$this->config->getSystemValue('enable_previews', true)) { + return; + } + foreach ($this->getDeletedFiles() as $fileId) { try { $preview = $this->previewFolder->getFolder((string)$fileId); diff --git a/tests/lib/Preview/BackgroundCleanupJobTest.php b/tests/lib/Preview/BackgroundCleanupJobTest.php index cd9f6ef03991d..78f292b3ce772 100644 --- a/tests/lib/Preview/BackgroundCleanupJobTest.php +++ b/tests/lib/Preview/BackgroundCleanupJobTest.php @@ -50,6 +50,9 @@ class BackgroundCleanupJobTest extends \Test\TestCase { /** @var bool */ private $trashEnabled; + /** @var IConfig */ + private $config; + /** @var IDBConnection */ private $connection; @@ -79,6 +82,7 @@ protected function setUp(): void { $this->trashEnabled = $appManager->isEnabledForUser('files_trashbin', $this->userId); $appManager->disableApp('files_trashbin'); + $this->config = \OC::$server->getConfig(); $this->connection = \OC::$server->getDatabaseConnection(); $this->previewManager = \OC::$server->getPreviewManager(); $this->rootFolder = \OC::$server->getRootFolder(); @@ -142,7 +146,7 @@ public function testCleanupSystemCron() { $root = $this->getRoot(); $this->assertSame(11, $this->countPreviews($root, $fileIds)); - $job = new BackgroundCleanupJob($this->connection, $root, $this->mimeTypeLoader, true); + $job = new BackgroundCleanupJob($this->config, $this->connection, $root, $this->mimeTypeLoader, true); $job->run([]); foreach ($files as $file) { @@ -166,7 +170,7 @@ public function testCleanupAjax() { $root = $this->getRoot(); $this->assertSame(11, $this->countPreviews($root, $fileIds)); - $job = new BackgroundCleanupJob($this->connection, $root, $this->mimeTypeLoader, false); + $job = new BackgroundCleanupJob($this->config, $this->connection, $root, $this->mimeTypeLoader, false); $job->run([]); foreach ($files as $file) { @@ -185,6 +189,32 @@ public function testCleanupAjax() { $this->assertSame(0, $this->countPreviews($root, $fileIds)); } + public function testPreviewsDisabled() { + $files = $this->setup11Previews(); + $fileIds = array_map(function (File $f) { + return $f->getId(); + }, $files); + + foreach ($files as $file) { + $file->delete(); + } + + $root = $this->getRoot(); + $this->assertSame(11, $this->countPreviews($root, $fileIds)); + + $oldConfig = $this->config->getSystemValueBool('enable_previews', true); + $this->config->setSystemValue('enable_previews', false); + + $job = new BackgroundCleanupJob($this->config, $this->connection, $root, $this->mimeTypeLoader, true); + $job->run([]); + + $root = $this->getRoot(); + $this->assertSame(11, $this->countPreviews($root, $fileIds)); + + // Cleanup + $this->config->setSystemValue('enable_previews', $oldConfig); + } + public function testOldPreviews() { $appdata = \OC::$server->getAppDataDir('preview'); @@ -196,7 +226,7 @@ public function testOldPreviews() { $appdata = \OC::$server->getAppDataDir('preview'); $this->assertSame(2, count($appdata->getDirectoryListing())); - $job = new BackgroundCleanupJob($this->connection, $this->getRoot(), $this->mimeTypeLoader, true); + $job = new BackgroundCleanupJob($this->config, $this->connection, $this->getRoot(), $this->mimeTypeLoader, true); $job->run([]); $appdata = \OC::$server->getAppDataDir('preview');