mirror of
https://github.com/nextcloud/server.git
synced 2026-02-20 00:12:30 -05:00
134 lines
3.8 KiB
PHP
134 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
namespace OC\Core\Command\Preview;
|
|
|
|
use OC\Preview\Db\Preview;
|
|
use OC\Preview\PreviewService;
|
|
use OCP\Files\IMimeTypeLoader;
|
|
use OCP\Files\NotFoundException;
|
|
use OCP\Files\NotPermittedException;
|
|
use OCP\IAvatarManager;
|
|
use OCP\IDBConnection;
|
|
use OCP\IUserManager;
|
|
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 {
|
|
public function __construct(
|
|
protected readonly IDBConnection $connection,
|
|
protected readonly IUserManager $userManager,
|
|
protected readonly IAvatarManager $avatarManager,
|
|
private readonly PreviewService $previewService,
|
|
private readonly IMimeTypeLoader $mimeTypeLoader,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure(): void {
|
|
$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.');
|
|
}
|
|
|
|
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();
|
|
} catch (NotFoundException $e) {
|
|
// continue
|
|
} catch (NotPermittedException $e) {
|
|
// continue
|
|
}
|
|
}
|
|
|
|
$output->writeln('Deleted ' . $avatarsToDeleteCount . ' avatars');
|
|
$output->writeln('');
|
|
}
|
|
|
|
private function getAvatarsToDelete(): \Iterator {
|
|
foreach ($this->userManager->searchDisplayName('') as $user) {
|
|
$avatar = $this->avatarManager->getAvatar($user->getUID());
|
|
|
|
if (!$avatar->isCustomAvatar()) {
|
|
yield [$user->getUID(), $avatar];
|
|
}
|
|
}
|
|
}
|
|
|
|
private function deletePreviews(OutputInterface $output, bool $dryMode): void {
|
|
$previewsToDeleteCount = 0;
|
|
|
|
foreach ($this->getPreviewsToDelete() as ['path' => $filePath, 'preview' => $preview]) {
|
|
$output->writeln('Deleting previews for ' . $filePath, OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
$previewsToDeleteCount++;
|
|
|
|
if ($dryMode) {
|
|
continue;
|
|
}
|
|
|
|
$this->previewService->deletePreview($preview);
|
|
}
|
|
|
|
$output->writeln('Deleted ' . $previewsToDeleteCount . ' previews');
|
|
}
|
|
|
|
/**
|
|
* @return \Iterator<array{path: string, preview: Preview}>
|
|
*/
|
|
private function getPreviewsToDelete(): \Iterator {
|
|
$qb = $this->connection->getQueryBuilder();
|
|
$qb->select('fileid', 'path')
|
|
->from('filecache')
|
|
->where(
|
|
$qb->expr()->orX(
|
|
$qb->expr()->eq('mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/plain'))),
|
|
$qb->expr()->eq('mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/markdown'))),
|
|
$qb->expr()->eq('mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('text/x-markdown')))
|
|
)
|
|
);
|
|
|
|
$cursor = $qb->executeQuery();
|
|
|
|
while ($row = $cursor->fetch()) {
|
|
foreach ($this->previewService->getAvailablePreviewForFile($row['fileid']) as $preview) {
|
|
yield ['path' => $row['path'], 'preview' => $preview];
|
|
}
|
|
}
|
|
|
|
$cursor->closeCursor();
|
|
}
|
|
}
|