Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 11 additions & 1 deletion lib/private/Preview/BackgroundCleanupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -47,20 +51,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
36 changes: 33 additions & 3 deletions tests/lib/Preview/BackgroundCleanupJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class BackgroundCleanupJobTest extends \Test\TestCase {
/** @var bool */
private $trashEnabled;

/** @var IConfig */
private $config;

/** @var IDBConnection */
private $connection;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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');

Expand All @@ -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');
Expand Down