2021-02-07 16:48:58 -05:00
< ? php
declare ( strict_types = 1 );
/**
2024-05-24 13:43:47 -04:00
* SPDX - FileCopyrightText : 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX - License - Identifier : AGPL - 3.0 - or - later
2021-02-07 16:48:58 -05:00
*/
namespace OC\Core\Command\Preview ;
2025-09-16 05:34:41 -04:00
use OC\Preview\Db\Preview ;
use OC\Preview\PreviewService ;
2021-02-07 16:48:58 -05:00
use OCP\Files\NotFoundException ;
use OCP\Files\NotPermittedException ;
use OCP\IAvatarManager ;
use OCP\IDBConnection ;
use OCP\IUserManager ;
2025-09-26 08:22:38 -04:00
use Override ;
2021-02-07 16:48:58 -05:00
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Output\OutputInterface ;
class ResetRenderedTexts extends Command {
2023-06-12 11:10:19 -04:00
public function __construct (
2025-09-16 05:34:41 -04:00
protected readonly IDBConnection $connection ,
protected readonly IUserManager $userManager ,
protected readonly IAvatarManager $avatarManager ,
private readonly PreviewService $previewService ,
2023-06-12 11:10:19 -04:00
) {
2021-02-07 16:48:58 -05:00
parent :: __construct ();
}
2025-09-26 08:22:38 -04:00
#[Override]
2025-09-16 05:34:41 -04:00
protected function configure () : void {
2021-02-07 16:48:58 -05:00
$this
-> setName ( 'preview:reset-rendered-texts' )
-> setDescription ( 'Deletes all generated avatars and previews of text and md files' )
-> addOption ( 'dry' , 'd' , InputOption :: VALUE_NONE , 'Dry mode - will not delete any files - in combination with the verbose mode one could check the operations.' );
}
2025-09-26 08:22:38 -04:00
#[Override]
2021-02-07 16:48:58 -05:00
protected function execute ( InputInterface $input , OutputInterface $output ) : int {
$dryMode = $input -> getOption ( 'dry' );
if ( $dryMode ) {
$output -> writeln ( 'INFO: The command is run in dry mode and will not modify anything.' );
$output -> writeln ( '' );
}
$this -> deleteAvatars ( $output , $dryMode );
$this -> deletePreviews ( $output , $dryMode );
return 0 ;
}
private function deleteAvatars ( OutputInterface $output , bool $dryMode ) : void {
$avatarsToDeleteCount = 0 ;
foreach ( $this -> getAvatarsToDelete () as [ $userId , $avatar ]) {
$output -> writeln ( 'Deleting avatar for ' . $userId , OutputInterface :: VERBOSITY_VERBOSE );
$avatarsToDeleteCount ++ ;
if ( $dryMode ) {
continue ;
}
try {
$avatar -> remove ();
2025-09-26 06:26:43 -04:00
} catch ( NotFoundException | NotPermittedException ) {
2021-02-07 16:48:58 -05:00
// continue
}
}
$output -> writeln ( 'Deleted ' . $avatarsToDeleteCount . ' avatars' );
$output -> writeln ( '' );
}
private function getAvatarsToDelete () : \Iterator {
2025-10-01 10:32:22 -04:00
foreach ( $this -> userManager -> searchDisplayName ( '' ) as $user ) {
2021-02-07 16:48:58 -05:00
$avatar = $this -> avatarManager -> getAvatar ( $user -> getUID ());
if ( ! $avatar -> isCustomAvatar ()) {
yield [ $user -> getUID (), $avatar ];
}
}
}
private function deletePreviews ( OutputInterface $output , bool $dryMode ) : void {
$previewsToDeleteCount = 0 ;
2025-09-26 06:26:43 -04:00
foreach ( $this -> getPreviewsToDelete () as $preview ) {
2025-09-30 09:56:31 -04:00
$output -> writeln ( 'Deleting preview ' . $preview -> getName () . ' for fileId ' . $preview -> getFileId (), OutputInterface :: VERBOSITY_VERBOSE );
2021-02-07 16:48:58 -05:00
$previewsToDeleteCount ++ ;
if ( $dryMode ) {
continue ;
}
2025-09-16 05:34:41 -04:00
$this -> previewService -> deletePreview ( $preview );
2021-02-07 16:48:58 -05:00
}
$output -> writeln ( 'Deleted ' . $previewsToDeleteCount . ' previews' );
}
2025-09-16 05:34:41 -04:00
/**
2025-09-26 06:26:43 -04:00
* @ return \Generator < Preview >
2025-09-16 05:34:41 -04:00
*/
2025-09-26 06:26:43 -04:00
private function getPreviewsToDelete () : \Generator {
return $this -> previewService -> getPreviewsForMimeTypes ([
2025-09-30 09:56:31 -04:00
'text/plain' ,
'text/markdown' ,
'text/x-markdown'
2025-09-26 06:26:43 -04:00
]);
2021-02-07 16:48:58 -05:00
}
}