2015-08-11 15:21:32 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2024-06-02 09:26:54 -04:00
|
|
|
* 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;
|
|
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
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;
|
2022-06-28 06:55:26 -04:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
|
use OCP\BackgroundJob\TimedJob;
|
2025-03-26 16:01:47 -04:00
|
|
|
use OCP\IAppConfig;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\IUserManager;
|
2025-04-02 06:45:00 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-08-11 15:21:32 -04:00
|
|
|
|
2022-06-28 06:55:26 -04:00
|
|
|
class ExpireTrash extends TimedJob {
|
|
|
|
|
public function __construct(
|
2025-03-26 16:01:47 -04:00
|
|
|
private IAppConfig $appConfig,
|
2024-10-18 06:04:22 -04:00
|
|
|
private IUserManager $userManager,
|
|
|
|
|
private Expiration $expiration,
|
2025-04-02 06:45:00 -04:00
|
|
|
private LoggerInterface $logger,
|
2022-06-28 06:55:26 -04:00
|
|
|
ITimeFactory $time,
|
|
|
|
|
) {
|
|
|
|
|
parent::__construct($time);
|
2015-08-11 15:21:32 -04:00
|
|
|
// Run once per 30 minutes
|
|
|
|
|
$this->setInterval(60 * 30);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function run($argument) {
|
2025-03-26 16:01:47 -04:00
|
|
|
$backgroundJob = $this->appConfig->getValueString('files_trashbin', 'background_job_expire_trash', 'yes');
|
2021-10-07 15:34:16 -04:00
|
|
|
if ($backgroundJob === 'no') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-11 15:21:32 -04:00
|
|
|
$maxAge = $this->expiration->getMaxAgeAsTimestamp();
|
|
|
|
|
if (!$maxAge) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-04-20 04:36:15 -04:00
|
|
|
|
2025-03-26 16:01:47 -04:00
|
|
|
$stopTime = time() + 60 * 30; // Stops after 30 minutes.
|
|
|
|
|
$offset = $this->appConfig->getValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
|
|
|
|
|
$users = $this->userManager->getSeenUsers($offset);
|
|
|
|
|
|
|
|
|
|
foreach ($users as $user) {
|
2025-04-02 06:45:00 -04:00
|
|
|
try {
|
|
|
|
|
$uid = $user->getUID();
|
|
|
|
|
if (!$this->setupFS($uid)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$dirContent = Helper::getTrashFiles('/', $uid, 'mtime');
|
|
|
|
|
Trashbin::deleteExpiredFiles($dirContent, $uid);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
$this->logger->error('Error while expiring trashbin for user ' . $user->getUID(), ['exception' => $e]);
|
2015-08-11 15:21:32 -04:00
|
|
|
}
|
2021-10-07 15:34:16 -04:00
|
|
|
|
2025-03-26 16:01:47 -04:00
|
|
|
$offset++;
|
|
|
|
|
|
|
|
|
|
if ($stopTime < time()) {
|
|
|
|
|
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', $offset);
|
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->appConfig->setValueInt('files_trashbin', 'background_job_expire_trash_offset', 0);
|
2015-08-11 15:21:32 -04:00
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Act on behalf on trash item owner
|
|
|
|
|
*/
|
2022-06-28 06:55:26 -04:00
|
|
|
protected function setupFS(string $user): bool {
|
2016-04-20 04:36:15 -04:00
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
|
\OC_Util::setupFS($user);
|
2015-08-11 15:21:32 -04:00
|
|
|
|
2016-04-20 04:36:15 -04:00
|
|
|
// Check if this user has a trashbin directory
|
2024-10-10 06:40:31 -04:00
|
|
|
$view = new View('/' . $user);
|
2016-04-20 04:36:15 -04:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|