Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Stop BackgroundCleanupJob when previews are disabled
Signed-off-by: Carl Csaposs <[email protected]>
  • Loading branch information
Carl Csaposs committed May 16, 2022
commit cbf91ed06cf8a6037178ac004c06294e0ce3fef8
9 changes: 8 additions & 1 deletion lib/private/Preview/BackgroundCleanupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IDBConnection;
use OCP\IConfig;

class BackgroundCleanupJob extends TimedJob {

Expand All @@ -47,20 +48,26 @@ 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;
$this->mimeTypeLoader = $mimeTypeLoader;
}

public function run($argument) {
if (!$this->config->getSystemValue('enable_previews', true)) {
return;
}

foreach ($this->getDeletedFiles() as $fileId) {
try {
$preview = $this->previewFolder->getFolder((string)$fileId);
Expand Down
32 changes: 29 additions & 3 deletions tests/lib/Preview/BackgroundCleanupJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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();
Expand Down Expand Up @@ -142,7 +143,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) {
Expand All @@ -166,7 +167,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) {
Expand All @@ -185,6 +186,31 @@ 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));

$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', true);
}

public function testOldPreviews() {
$appdata = \OC::$server->getAppDataDir('preview');

Expand All @@ -196,7 +222,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');
Expand Down