2016-08-23 08:03:05 -04:00
< ? php
2025-06-30 09:04:05 -04:00
2016-08-23 08:03:05 -04:00
/**
2024-06-02 09:26:54 -04:00
* SPDX - FileCopyrightText : 2019 - 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud GmbH .
* SPDX - License - Identifier : AGPL - 3.0 - only
2016-08-23 08:03:05 -04:00
*/
namespace OCA\Files_Trashbin\Command ;
2025-09-04 08:07:14 -04:00
use OC\Core\Command\Base ;
use OC\Files\SetupManager ;
2016-08-23 08:03:05 -04:00
use OCA\Files_Trashbin\Expiration ;
use OCA\Files_Trashbin\Trashbin ;
2025-09-04 08:07:14 -04:00
use OCP\Files\Folder ;
use OCP\Files\IRootFolder ;
2019-11-22 14:52:10 -05:00
use OCP\IUser ;
use OCP\IUserManager ;
2016-08-23 08:03:05 -04:00
use Symfony\Component\Console\Helper\ProgressBar ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Output\OutputInterface ;
2025-09-04 08:07:14 -04:00
class ExpireTrash extends Base {
2016-08-23 08:03:05 -04:00
2024-10-18 06:04:22 -04:00
public function __construct (
2025-09-04 08:07:14 -04:00
private readonly ? IUserManager $userManager ,
private readonly ? Expiration $expiration ,
private readonly SetupManager $setupManager ,
private readonly IRootFolder $rootFolder ,
2024-10-18 06:04:22 -04:00
) {
2016-08-23 08:03:05 -04:00
parent :: __construct ();
}
2025-09-04 08:07:14 -04:00
protected function configure () : void {
parent :: configure ();
2016-08-23 08:03:05 -04:00
$this
-> setName ( 'trashbin:expire' )
-> setDescription ( 'Expires the users trashbin' )
-> addArgument (
'user_id' ,
InputArgument :: OPTIONAL | InputArgument :: IS_ARRAY ,
'expires the trashbin of the given user(s), if no user is given the trash for all users will be expired'
);
}
2020-06-26 09:12:11 -04:00
protected function execute ( InputInterface $input , OutputInterface $output ) : int {
2025-05-18 18:58:22 -04:00
$minAge = $this -> expiration -> getMinAgeAsTimestamp ();
2016-08-23 08:03:05 -04:00
$maxAge = $this -> expiration -> getMaxAgeAsTimestamp ();
2025-05-18 18:58:22 -04:00
if ( $minAge === false && $maxAge === false ) {
2021-07-28 06:58:01 -04:00
$output -> writeln ( 'Auto expiration is configured - keeps files and folders in the trash bin for 30 days and automatically deletes anytime after that if space is needed (note: files may not be deleted if space is not needed)' );
2020-06-26 09:12:11 -04:00
return 1 ;
2016-08-23 08:03:05 -04:00
}
2025-09-04 08:07:14 -04:00
$userIds = $input -> getArgument ( 'user_id' );
if ( ! empty ( $userIds )) {
foreach ( $userIds as $userId ) {
$user = $this -> userManager -> get ( $userId );
if ( $user ) {
$output -> writeln ( " Remove deleted files of <info> $userId </info> " );
$this -> expireTrashForUser ( $user , $output );
$output -> writeln ( " <error>Unknown user $userId </error> " );
return 1 ;
2016-08-23 08:03:05 -04:00
} else {
2025-09-04 08:07:14 -04:00
$output -> writeln ( " <error>Unknown user $userId </error> " );
2020-06-26 09:12:11 -04:00
return 1 ;
2016-08-23 08:03:05 -04:00
}
}
} else {
$p = new ProgressBar ( $output );
$p -> start ();
2025-04-02 06:44:54 -04:00
$users = $this -> userManager -> getSeenUsers ();
foreach ( $users as $user ) {
2016-08-23 08:03:05 -04:00
$p -> advance ();
2025-09-04 08:07:14 -04:00
$this -> expireTrashForUser ( $user , $output );
2025-04-02 06:44:54 -04:00
}
2016-08-23 08:03:05 -04:00
$p -> finish ();
$output -> writeln ( '' );
}
2020-06-26 09:12:11 -04:00
return 0 ;
2016-08-23 08:03:05 -04:00
}
2025-09-04 08:07:14 -04:00
private function expireTrashForUser ( IUser $user , OutputInterface $output ) : void {
2025-04-02 06:45:00 -04:00
try {
2025-09-04 08:07:14 -04:00
$trashRoot = $this -> getTrashRoot ( $user );
Trashbin :: expire ( $trashRoot , $user );
2025-04-02 06:45:00 -04:00
} catch ( \Throwable $e ) {
2025-09-04 08:07:14 -04:00
$output -> writeln ( '<error>Error while expiring trashbin for user ' . $user -> getUID () . '</error>' );
throw $e ;
} finally {
$this -> setupManager -> tearDown ();
2016-08-23 08:03:05 -04:00
}
}
2025-09-04 08:07:14 -04:00
private function getTrashRoot ( IUser $user ) : Folder {
$this -> setupManager -> setupForUser ( $user );
2016-08-23 08:03:05 -04:00
2025-09-04 08:07:14 -04:00
$folder = $this -> rootFolder -> getUserFolder ( $user -> getUID ()) -> getParent () -> get ( 'files_trashbin' );
if ( ! $folder instanceof Folder ) {
throw new \LogicException ( " Didn't expect files_trashbin to be a file instead of a folder " );
2016-08-23 08:03:05 -04:00
}
2025-09-04 08:07:14 -04:00
return $folder ;
2016-08-23 08:03:05 -04:00
}
}