Skip to content

Commit

Permalink
feat: previews are physically deleted from disk and db in an async ma…
Browse files Browse the repository at this point in the history
…nner
  • Loading branch information
DeepDiver1975 committed Jul 1, 2022
1 parent 7e9caa4 commit ee94c75
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 359 deletions.
1 change: 1 addition & 0 deletions apps/files/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<job>OCA\Files\BackgroundJob\DeleteOrphanedItems</job>
<job>OCA\Files\BackgroundJob\CleanupFileLocks</job>
<job>OCA\Files\BackgroundJob\CleanupPersistentFileLocks</job>
<job>OCA\Files\BackgroundJob\PreviewCleanupJob</job>
</background-jobs>

<commands>
Expand Down
19 changes: 19 additions & 0 deletions apps/files/lib/BackgroundJob/PreviewCleanupJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace OCA\Files\BackgroundJob;

use OC\BackgroundJob\TimedJob;
use OC\PreviewCleanup;

class PreviewCleanupJob extends TimedJob {
public function __construct() {
$this->setInterval(3600); //execute job every hour
}

public function run($arguments) {
$cmd = new PreviewCleanup(\OC::$server->getDatabaseConnection());
$count = $cmd->process(false, 10);
$logger = \OC::$server->getLogger();
$logger->info("$count orphaned previews deleted");
}
}
6 changes: 1 addition & 5 deletions apps/files_sharing/ajax/publicpreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,8 @@
exit;
}

// $path is relative to the data directory but Preview expects it to be relative to the user's
// so strip the first component
$root = \substr($path, \strpos($path, '/') + 1);

try {
$preview = new \OC\Preview($userId, $root);
$preview = new \OC\Preview($userId);
$preview->setFile($sharedFile);
$preview->setMaxX($maxX);
$preview->setMaxY($maxY);
Expand Down
68 changes: 68 additions & 0 deletions core/Command/Previews/Cleanup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @copyright Copyright (c) 2022, 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 <http://www.gnu.org/licenses/>
*
*/
namespace OC\Core\Command\Previews;

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use OC\Core\Command\Base;
use OC\PreviewCleanup;
use OCP\Files\Folder;
use OCP\IDBConnection;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Cleanup extends Base {

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

public function __construct(IDBConnection $connection) {
parent::__construct();
$this->connection = $connection;
}

protected function configure() {
parent::configure();

$this
->setName('previews:cleanup')
->setDescription('Remove unreferenced previews')
->addOption('all')
->addArgument('chunk_size', InputArgument::OPTIONAL, '', 1000)
;
}

protected function execute(InputInterface $input, OutputInterface $output) {
$all = $input->hasOption('all');
$chunk_size = $input->getArgument('chunk_size');

$pc = new PreviewCleanup($this->connection);
$count = $pc->process($all, $chunk_size, static function ($userId, $name, $action) use ($output) {
$output->writeln("$name - $userId: $action");
});

$output->writeln("$count orphaned previews deleted");
return 0;
}
}
1 change: 1 addition & 0 deletions core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
$application->add(new OC\Core\Command\Maintenance\Mode(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Maintenance\SingleUser(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Maintenance\UpdateHtaccess());
$application->add(new OC\Core\Command\Previews\Cleanup(\OC::$server->getDatabaseConnection()));

$application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getMemCacheFactory()));
$application->add(
Expand Down
16 changes: 1 addition & 15 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ public static function init() {
self::registerCacheHooks();
self::registerFilesystemHooks();
if ($systemConfig->getValue('enable_previews', true)) {
self::registerPreviewHooks();
OC_Hook::connect('OC_Filesystem', 'post_write', 'OC\Preview', 'post_write');
}
self::registerShareHooks();
self::registerLogRotate();
Expand Down Expand Up @@ -796,20 +796,6 @@ public static function registerFilesystemHooks() {
OC_Hook::connect('OC_Filesystem', 'rename', 'OC\Files\Filesystem', 'isForbiddenFileOrDir_Hook');
}

/**
* register hooks for previews
*/
public static function registerPreviewHooks() {
OC_Hook::connect('OC_Filesystem', 'post_write', 'OC\Preview', 'post_write');
OC_Hook::connect('OC_Filesystem', 'delete', 'OC\Preview', 'prepare_delete_files');
OC_Hook::connect('\OCP\Versions', 'preDelete', 'OC\Preview', 'prepare_delete');
OC_Hook::connect('\OCP\Trashbin', 'preDelete', 'OC\Preview', 'prepare_delete');
OC_Hook::connect('OC_Filesystem', 'post_delete', 'OC\Preview', 'post_delete_files');
OC_Hook::connect('\OCP\Versions', 'delete', 'OC\Preview', 'post_delete_versions');
OC_Hook::connect('\OCP\Trashbin', 'delete', 'OC\Preview', 'post_delete');
OC_Hook::connect('\OCP\Versions', 'rollback', 'OC\Preview', 'post_delete_versions');
}

/**
* register hooks for sharing
*/
Expand Down
Loading

0 comments on commit ee94c75

Please sign in to comment.