aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2024-10-02 19:00:04 +0200
committerThomas Citharel <tcit@tcit.fr>2024-10-03 13:41:32 +0200
commitfe0f89c237f9923630ac59ffdef22f80d097e3f5 (patch)
tree56e0d13366c377ecbece08a561c3b3ccf351c624 /core
parentbfcf4dc4869f047186867bb5c15d0b187692f3bd (diff)
downloadnextcloud-server-fe0f89c237f9923630ac59ffdef22f80d097e3f5.tar.gz
nextcloud-server-fe0f89c237f9923630ac59ffdef22f80d097e3f5.zip
feat(preview): add command to cleanup preview folderadd-command-to-cleanup-preview
When changing preview parameters (size, format, …), one can simply delete the preview folder and run files:scan-app-data in order to retrigger updated preview recreation because files are absent. However, that's not possible when using ObjectStorage as primary storage, as there's no concept of "preview folder". This command allows to simply delete and recreate the preview folder properly from Nextcloud, so that the abstraction levels handle all the subfolder & file deleting part. Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'core')
-rw-r--r--core/Command/Preview/Cleanup.php88
-rw-r--r--core/register_command.php1
2 files changed, 89 insertions, 0 deletions
diff --git a/core/Command/Preview/Cleanup.php b/core/Command/Preview/Cleanup.php
new file mode 100644
index 00000000000..2049bb98bc4
--- /dev/null
+++ b/core/Command/Preview/Cleanup.php
@@ -0,0 +1,88 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OC\Core\Command\Preview;
+
+use OC\Core\Command\Base;
+use OCP\Files\Folder;
+use OCP\Files\IRootFolder;
+use OCP\Files\NotFoundException;
+use OCP\Files\NotPermittedException;
+use Psr\Log\LoggerInterface;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Cleanup extends Base {
+
+ public function __construct(
+ private IRootFolder $rootFolder,
+ private LoggerInterface $logger,
+ ) {
+ parent::__construct();
+ }
+
+ protected function configure(): void {
+ $this
+ ->setName('preview:cleanup')
+ ->setDescription('Removes existing preview files');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int {
+ try {
+ $appDataFolder = $this->rootFolder->get($this->rootFolder->getAppDataDirectoryName());
+
+ if (!$appDataFolder instanceof Folder) {
+ $this->logger->error("Previews can't be removed: appdata is not a folder");
+ $output->writeln("Previews can't be removed: appdata is not a folder");
+ return 1;
+ }
+
+ /** @var Folder $previewFolder */
+ $previewFolder = $appDataFolder->get('preview');
+
+ } catch (NotFoundException $e) {
+ $this->logger->error("Previews can't be removed: appdata folder can't be found", ['exception' => $e]);
+ $output->writeln("Previews can't be removed: preview folder isn't deletable");
+ return 1;
+ }
+
+ if (!$previewFolder->isDeletable()) {
+ $this->logger->error("Previews can't be removed: preview folder isn't deletable");
+ $output->writeln("Previews can't be removed: preview folder isn't deletable");
+ return 1;
+ }
+
+ try {
+ $previewFolder->delete();
+ $this->logger->debug('Preview folder deleted');
+ $output->writeln('Preview folder deleted', OutputInterface::VERBOSITY_VERBOSE);
+ } catch (NotFoundException $e) {
+ $output->writeln("Previews weren't deleted: preview folder was not found while deleting it");
+ $this->logger->error("Previews weren't deleted: preview folder was not found while deleting it", ['exception' => $e]);
+ return 1;
+ } catch (NotPermittedException $e) {
+ $output->writeln("Previews weren't deleted: you don't have the permission to delete preview folder");
+ $this->logger->error("Previews weren't deleted: you don't have the permission to delete preview folder", ['exception' => $e]);
+ return 1;
+ }
+
+ try {
+ $appDataFolder->newFolder('preview');
+ $this->logger->debug('Preview folder recreated');
+ $output->writeln('Preview folder recreated', OutputInterface::VERBOSITY_VERBOSE);
+ } catch (NotPermittedException $e) {
+ $output->writeln("Preview folder was deleted, but you don't have the permission to create preview folder");
+ $this->logger->error("Preview folder was deleted, but you don't have the permission to create preview folder", ['exception' => $e]);
+ return 1;
+ }
+
+ $output->writeln('Previews removed');
+ return 0;
+ }
+}
diff --git a/core/register_command.php b/core/register_command.php
index faa2c3021a7..08a01d7ff6e 100644
--- a/core/register_command.php
+++ b/core/register_command.php
@@ -102,6 +102,7 @@ if ($config->getSystemValueBool('installed', false)) {
$application->add(Server::get(Command\Maintenance\Repair::class));
$application->add(Server::get(Command\Maintenance\RepairShareOwnership::class));
+ $application->add(Server::get(Command\Preview\Cleanup::class));
$application->add(Server::get(Command\Preview\Generate::class));
$application->add(Server::get(Command\Preview\Repair::class));
$application->add(Server::get(Command\Preview\ResetRenderedTexts::class));