2016-08-23 08:03:05 -04:00
< ? php
/**
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 ;
2024-10-10 06:40:31 -04:00
use OC\Files\View ;
2016-08-23 08:03:05 -04:00
use OCA\Files_Trashbin\Expiration ;
use OCA\Files_Trashbin\Helper ;
use OCA\Files_Trashbin\Trashbin ;
2019-11-22 14:52:10 -05:00
use OCP\IUser ;
use OCP\IUserManager ;
2025-04-02 06:45:00 -04:00
use Psr\Log\LoggerInterface ;
2016-08-23 08:03:05 -04:00
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Helper\ProgressBar ;
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Output\OutputInterface ;
class ExpireTrash extends Command {
/**
* @ param IUserManager | null $userManager
* @ param Expiration | null $expiration
*/
2024-10-18 06:04:22 -04:00
public function __construct (
2025-04-02 06:45:00 -04:00
private LoggerInterface $logger ,
2024-10-18 06:04:22 -04:00
private ? IUserManager $userManager = null ,
private ? Expiration $expiration = null ,
) {
2016-08-23 08:03:05 -04:00
parent :: __construct ();
}
protected function configure () {
$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 {
2016-08-23 08:03:05 -04:00
$maxAge = $this -> expiration -> getMaxAgeAsTimestamp ();
if ( ! $maxAge ) {
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
}
$users = $input -> getArgument ( 'user_id' );
if ( ! empty ( $users )) {
foreach ( $users as $user ) {
if ( $this -> userManager -> userExists ( $user )) {
$output -> writeln ( " Remove deleted files of <info> $user </info> " );
$userObject = $this -> userManager -> get ( $user );
$this -> expireTrashForUser ( $userObject );
} else {
$output -> writeln ( " <error>Unknown user $user </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 ();
$this -> expireTrashForUser ( $user );
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
}
2020-04-10 10:51:06 -04:00
public function expireTrashForUser ( IUser $user ) {
2025-04-02 06:45:00 -04:00
try {
$uid = $user -> getUID ();
if ( ! $this -> setupFS ( $uid )) {
return ;
}
$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 ]);
2016-08-23 08:03:05 -04:00
}
}
/**
* Act on behalf on trash item owner
* @ param string $user
* @ return boolean
*/
protected function setupFS ( $user ) {
\OC_Util :: tearDownFS ();
\OC_Util :: setupFS ( $user );
// Check if this user has a trashbin directory
2024-10-10 06:40:31 -04:00
$view = new View ( '/' . $user );
2016-08-23 08:03:05 -04:00
if ( ! $view -> is_dir ( '/files_trashbin/files' )) {
return false ;
}
return true ;
}
}