nextcloud/apps/files_trashbin/lib/BackgroundJob/ExpireTrash.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
1.7 KiB
PHP
Raw Normal View History

2015-08-11 15:21:32 -04:00
<?php
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
2015-08-11 15:21:32 -04:00
*/
namespace OCA\Files_Trashbin\BackgroundJob;
use OC\Files\View;
2015-08-11 15:21:32 -04:00
use OCA\Files_Trashbin\Expiration;
use OCA\Files_Trashbin\Helper;
use OCA\Files_Trashbin\Trashbin;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
2015-08-11 15:21:32 -04:00
class ExpireTrash extends TimedJob {
public function __construct(
private IConfig $config,
private IUserManager $userManager,
private Expiration $expiration,
ITimeFactory $time,
) {
parent::__construct($time);
2015-08-11 15:21:32 -04:00
// Run once per 30 minutes
$this->setInterval(60 * 30);
}
2015-09-15 08:28:04 -04:00
/**
* @param $argument
* @throws \Exception
*/
2015-08-11 15:21:32 -04:00
protected function run($argument) {
$backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
if ($backgroundJob === 'no') {
return;
}
2015-08-11 15:21:32 -04:00
$maxAge = $this->expiration->getMaxAgeAsTimestamp();
if (!$maxAge) {
return;
}
$this->userManager->callForSeenUsers(function (IUser $user): void {
2015-08-11 15:21:32 -04:00
$uid = $user->getUID();
if (!$this->setupFS($uid)) {
return;
2015-08-11 15:21:32 -04:00
}
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
Trashbin::deleteExpiredFiles($dirContent, $uid);
});
2015-08-11 15:21:32 -04:00
\OC_Util::tearDownFS();
}
/**
* Act on behalf on trash item owner
*/
protected function setupFS(string $user): bool {
\OC_Util::tearDownFS();
\OC_Util::setupFS($user);
2015-08-11 15:21:32 -04:00
// Check if this user has a trashbin directory
$view = new View('/' . $user);
if (!$view->is_dir('/files_trashbin/files')) {
2016-02-24 10:08:45 -05:00
return false;
}
2015-08-11 15:21:32 -04:00
return true;
}
}