Merge branch 'stable31' into backport/53001/stable31
commit
82c4a88512
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Command;
|
||||
|
||||
use OCP\Files\AppData\IAppDataFactory;
|
||||
use OCP\Files\NotPermittedException;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Helper\ProgressBar;
|
||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'dav:clear-contacts-photo-cache',
|
||||
description: 'Clear cached contact photos',
|
||||
hidden: false,
|
||||
)]
|
||||
class ClearContactsPhotoCache extends Command {
|
||||
|
||||
public function __construct(
|
||||
private IAppDataFactory $appDataFactory,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$photoCacheAppData = $this->appDataFactory->get('dav-photocache');
|
||||
|
||||
$folders = $photoCacheAppData->getDirectoryListing();
|
||||
$countFolders = count($folders);
|
||||
|
||||
if ($countFolders === 0) {
|
||||
$output->writeln('No cached contact photos found.');
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$output->writeln('Found ' . count($folders) . ' cached contact photos.');
|
||||
|
||||
/** @var QuestionHelper $helper */
|
||||
$helper = $this->getHelper('question');
|
||||
$question = new ConfirmationQuestion('Please confirm to clear the contacts photo cache [y/n] ', true);
|
||||
|
||||
if ($helper->ask($input, $output, $question) === false) {
|
||||
$output->writeln('Clearing the contacts photo cache aborted.');
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$progressBar = new ProgressBar($output, $countFolders);
|
||||
$progressBar->start();
|
||||
|
||||
foreach ($folders as $folder) {
|
||||
try {
|
||||
$folder->delete();
|
||||
} catch (NotPermittedException) {
|
||||
}
|
||||
$progressBar->advance();
|
||||
}
|
||||
|
||||
$progressBar->finish();
|
||||
|
||||
$output->writeln('');
|
||||
$output->writeln('Contacts photo cache cleared.');
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
namespace OCA\Files\Command;
|
||||
|
||||
use Exception;
|
||||
use OC\Core\Command\Base;
|
||||
use OC\Files\FilenameValidator;
|
||||
use OCP\Files\Folder;
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCP\Files\NotPermittedException;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserManager;
|
||||
use OCP\IUserSession;
|
||||
use OCP\L10N\IFactory;
|
||||
use OCP\Lock\LockedException;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class SanitizeFilenames extends Base {
|
||||
|
||||
private OutputInterface $output;
|
||||
private string $charReplacement;
|
||||
private bool $dryRun;
|
||||
|
||||
public function __construct(
|
||||
private IUserManager $userManager,
|
||||
private IRootFolder $rootFolder,
|
||||
private IUserSession $session,
|
||||
private IFactory $l10nFactory,
|
||||
private FilenameValidator $filenameValidator,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
parent::configure();
|
||||
|
||||
$forbiddenCharacter = $this->filenameValidator->getForbiddenCharacters();
|
||||
$charReplacement = array_diff([' ', '_', '-'], $forbiddenCharacter);
|
||||
$charReplacement = reset($charReplacement) ?: '';
|
||||
|
||||
$this
|
||||
->setName('files:sanitize-filenames')
|
||||
->setDescription('Renames files to match naming constraints')
|
||||
->addArgument(
|
||||
'user_id',
|
||||
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
|
||||
'will only rename files the given user(s) have access to'
|
||||
)
|
||||
->addOption(
|
||||
'dry-run',
|
||||
mode: InputOption::VALUE_NONE,
|
||||
description: 'Do not actually rename any files but just check filenames.',
|
||||
)
|
||||
->addOption(
|
||||
'char-replacement',
|
||||
'c',
|
||||
mode: InputOption::VALUE_REQUIRED,
|
||||
description: 'Replacement for invalid character (by default space, underscore or dash is used)',
|
||||
default: $charReplacement,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$this->charReplacement = $input->getOption('char-replacement');
|
||||
if ($this->charReplacement === '' || mb_strlen($this->charReplacement) > 1) {
|
||||
$output->writeln('<error>No character replacement given</error>');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$this->dryRun = $input->getOption('dry-run');
|
||||
if ($this->dryRun) {
|
||||
$output->writeln('<info>Dry run is enabled, no actual renaming will be applied.</>');
|
||||
}
|
||||
|
||||
$this->output = $output;
|
||||
$users = $input->getArgument('user_id');
|
||||
if (!empty($users)) {
|
||||
foreach ($users as $userId) {
|
||||
$user = $this->userManager->get($userId);
|
||||
if ($user === null) {
|
||||
$output->writeln("<error>User '$userId' does not exist - skipping</>");
|
||||
continue;
|
||||
}
|
||||
$this->sanitizeUserFiles($user);
|
||||
}
|
||||
} else {
|
||||
$this->userManager->callForSeenUsers($this->sanitizeUserFiles(...));
|
||||
}
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function sanitizeUserFiles(IUser $user): void {
|
||||
// Set an active user so that event listeners can correctly work (e.g. files versions)
|
||||
$this->session->setVolatileActiveUser($user);
|
||||
|
||||
$this->output->writeln('<info>Analyzing files of ' . $user->getUID() . '</>');
|
||||
|
||||
$folder = $this->rootFolder->getUserFolder($user->getUID());
|
||||
$this->sanitizeFiles($folder);
|
||||
}
|
||||
|
||||
private function sanitizeFiles(Folder $folder): void {
|
||||
foreach ($folder->getDirectoryListing() as $node) {
|
||||
$this->output->writeln('scanning: ' . $node->getPath(), OutputInterface::VERBOSITY_VERBOSE);
|
||||
|
||||
try {
|
||||
$oldName = $node->getName();
|
||||
if (!$this->filenameValidator->isFilenameValid($oldName)) {
|
||||
$newName = $this->sanitizeName($oldName);
|
||||
$newName = $folder->getNonExistingName($newName);
|
||||
$path = rtrim(dirname($node->getPath()), '/');
|
||||
|
||||
if (!$this->dryRun) {
|
||||
$node->move("$path/$newName");
|
||||
} elseif (!$folder->isCreatable()) {
|
||||
// simulate error for dry run
|
||||
throw new NotPermittedException();
|
||||
}
|
||||
$this->output->writeln('renamed: "' . $oldName . '" to "' . $newName . '"');
|
||||
}
|
||||
} catch (LockedException) {
|
||||
$this->output->writeln('<comment>skipping: ' . $node->getPath() . ' (file is locked)</>');
|
||||
} catch (NotPermittedException) {
|
||||
$this->output->writeln('<comment>skipping: ' . $node->getPath() . ' (no permissions)</>');
|
||||
} catch (Exception) {
|
||||
$this->output->writeln('<error>failed: ' . $node->getPath() . '</>');
|
||||
}
|
||||
|
||||
if ($node instanceof Folder) {
|
||||
$this->sanitizeFiles($node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function sanitizeName(string $name): string {
|
||||
$l10n = $this->l10nFactory->get('files');
|
||||
|
||||
foreach ($this->filenameValidator->getForbiddenExtensions() as $extension) {
|
||||
if (str_ends_with($name, $extension)) {
|
||||
$name = substr($name, 0, strlen($name) - strlen($extension));
|
||||
}
|
||||
}
|
||||
|
||||
$basename = substr($name, 0, strpos($name, '.', 1) ?: null);
|
||||
if (in_array($basename, $this->filenameValidator->getForbiddenBasenames())) {
|
||||
$name = str_replace($basename, $l10n->t('%1$s (renamed)', [$basename]), $name);
|
||||
}
|
||||
|
||||
if ($name === '') {
|
||||
$name = $l10n->t('renamed file');
|
||||
}
|
||||
|
||||
$forbiddenCharacter = $this->filenameValidator->getForbiddenCharacters();
|
||||
$name = str_replace($forbiddenCharacter, $this->charReplacement, $name);
|
||||
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
namespace OCA\Files\Command;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
use OCA\Files\Service\SettingsService;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class WindowsCompatibleFilenames extends Base {
|
||||
|
||||
public function __construct(
|
||||
private SettingsService $service,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
parent::configure();
|
||||
|
||||
$this
|
||||
->setName('files:windows-compatible-filenames')
|
||||
->setDescription('Enforce naming constraints for windows compatible filenames')
|
||||
->addOption('enable', description: 'Enable windows naming constraints')
|
||||
->addOption('disable', description: 'Disable windows naming constraints');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
if ($input->getOption('enable')) {
|
||||
if ($this->service->hasFilesWindowsSupport()) {
|
||||
$output->writeln('<error>Windows compatible filenames already enforced.</error>', OutputInterface::VERBOSITY_VERBOSE);
|
||||
}
|
||||
$this->service->setFilesWindowsSupport(true);
|
||||
$output->writeln('Windows compatible filenames enforced.');
|
||||
} elseif ($input->getOption('disable')) {
|
||||
if (!$this->service->hasFilesWindowsSupport()) {
|
||||
$output->writeln('<error>Windows compatible filenames already disabled.</error>', OutputInterface::VERBOSITY_VERBOSE);
|
||||
}
|
||||
$this->service->setFilesWindowsSupport(false);
|
||||
$output->writeln('Windows compatible filename constraints removed.');
|
||||
} else {
|
||||
$output->writeln('Windows compatible filenames are ' . ($this->service->hasFilesWindowsSupport() ? 'enforced' : 'disabled'));
|
||||
}
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue