aboutsummaryrefslogtreecommitdiffstats
path: root/core/Command/Preview
diff options
context:
space:
mode:
Diffstat (limited to 'core/Command/Preview')
-rw-r--r--core/Command/Preview/Cleanup.php88
-rw-r--r--core/Command/Preview/Generate.php53
-rw-r--r--core/Command/Preview/Repair.php68
-rw-r--r--core/Command/Preview/ResetRenderedTexts.php25
4 files changed, 135 insertions, 99 deletions
diff --git a/core/Command/Preview/Cleanup.php b/core/Command/Preview/Cleanup.php
new file mode 100644
index 00000000000..dad981a5243
--- /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/Command/Preview/Generate.php b/core/Command/Preview/Generate.php
index a885d224fc7..222c42f613b 100644
--- a/core/Command/Preview/Generate.php
+++ b/core/Command/Preview/Generate.php
@@ -2,23 +2,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\Preview;
@@ -48,38 +33,38 @@ class Generate extends Command {
$this
->setName('preview:generate')
->setDescription('generate a preview for a file')
- ->addArgument("file", InputArgument::REQUIRED, "path or fileid of the file to generate the preview for")
- ->addOption("size", "s", InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, "size to generate the preview for in pixels, defaults to 64x64", ["64x64"])
- ->addOption("crop", "c", InputOption::VALUE_NONE, "crop the previews instead of maintaining aspect ratio")
- ->addOption("mode", "m", InputOption::VALUE_REQUIRED, "mode for generating uncropped previews, 'cover' or 'fill'", IPreview::MODE_FILL);
+ ->addArgument('file', InputArgument::REQUIRED, 'path or fileid of the file to generate the preview for')
+ ->addOption('size', 's', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'size to generate the preview for in pixels, defaults to 64x64', ['64x64'])
+ ->addOption('crop', 'c', InputOption::VALUE_NONE, 'crop the previews instead of maintaining aspect ratio')
+ ->addOption('mode', 'm', InputOption::VALUE_REQUIRED, "mode for generating uncropped previews, 'cover' or 'fill'", IPreview::MODE_FILL);
}
protected function execute(InputInterface $input, OutputInterface $output): int {
- $fileInput = $input->getArgument("file");
- $sizes = $input->getOption("size");
- $sizes = array_map(function (string $size) use ($output, &$error) {
+ $fileInput = $input->getArgument('file');
+ $sizes = $input->getOption('size');
+ $sizes = array_map(function (string $size) use ($output) {
if (str_contains($size, 'x')) {
$sizeParts = explode('x', $size, 2);
} else {
$sizeParts = [$size, $size];
}
- if (!is_numeric($sizeParts[0]) || !is_numeric($sizeParts[1])) {
+ if (!is_numeric($sizeParts[0]) || !is_numeric($sizeParts[1] ?? null)) {
$output->writeln("<error>Invalid size $size</error>");
return null;
}
- return array_map("intval", $sizeParts);
+ return array_map('intval', $sizeParts);
}, $sizes);
if (in_array(null, $sizes)) {
return 1;
}
- $mode = $input->getOption("mode");
+ $mode = $input->getOption('mode');
if ($mode !== IPreview::MODE_FILL && $mode !== IPreview::MODE_COVER) {
$output->writeln("<error>Invalid mode $mode</error>");
return 1;
}
- $crop = $input->getOption("crop");
+ $crop = $input->getOption('crop');
$file = $this->getFile($fileInput);
if (!$file) {
$output->writeln("<error>File $fileInput not found</error>");
@@ -91,7 +76,7 @@ class Generate extends Command {
}
if (!$this->previewManager->isAvailable($file)) {
- $output->writeln("<error>No preview generator available for file of type" . $file->getMimetype() . "</error>");
+ $output->writeln('<error>No preview generator available for file of type' . $file->getMimetype() . '</error>');
return 1;
}
@@ -106,9 +91,9 @@ class Generate extends Command {
$this->previewManager->generatePreviews($file, $specifications);
if (count($specifications) > 1) {
- $output->writeln("generated <info>" . count($specifications) . "</info> previews");
+ $output->writeln('generated <info>' . count($specifications) . '</info> previews');
} else {
- $output->writeln("preview generated");
+ $output->writeln('preview generated');
}
return 0;
}
@@ -121,11 +106,7 @@ class Generate extends Command {
}
$mount = $mounts[0];
$userFolder = $this->rootFolder->getUserFolder($mount->getUser()->getUID());
- $nodes = $userFolder->getById((int)$fileInput);
- if (!$nodes) {
- return null;
- }
- return $nodes[0];
+ return $userFolder->getFirstNodeById((int)$fileInput);
} else {
try {
return $this->rootFolder->get($fileInput);
diff --git a/core/Command/Preview/Repair.php b/core/Command/Preview/Repair.php
index fcdea9c20c8..a92a4cf8ed0 100644
--- a/core/Command/Preview/Repair.php
+++ b/core/Command/Preview/Repair.php
@@ -3,26 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2020, Morris Jobke <hey@morrisjobke.de>
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Morris Jobke <hey@morrisjobke.de>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\Preview;
@@ -37,6 +19,7 @@ use OCP\Lock\LockedException;
use Psr\Log\LoggerInterface;
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\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
@@ -77,11 +60,11 @@ class Repair extends Command {
$thresholdInMiB = round($this->memoryTreshold / 1024 / 1024, 1);
$output->writeln("Memory limit is $limitInMiB MiB");
$output->writeln("Memory threshold is $thresholdInMiB MiB");
- $output->writeln("");
+ $output->writeln('');
$memoryCheckEnabled = true;
} else {
- $output->writeln("No memory limit in place - disabled memory check. Set a PHP memory limit to automatically stop the execution of this migration script once memory consumption is close to this limit.");
- $output->writeln("");
+ $output->writeln('No memory limit in place - disabled memory check. Set a PHP memory limit to automatically stop the execution of this migration script once memory consumption is close to this limit.');
+ $output->writeln('');
$memoryCheckEnabled = false;
}
@@ -90,20 +73,20 @@ class Repair extends Command {
if ($dryMode) {
- $output->writeln("INFO: The migration is run in dry mode and will not modify anything.");
- $output->writeln("");
+ $output->writeln('INFO: The migration is run in dry mode and will not modify anything.');
+ $output->writeln('');
} elseif ($deleteMode) {
- $output->writeln("WARN: The migration will _DELETE_ old previews.");
- $output->writeln("");
+ $output->writeln('WARN: The migration will _DELETE_ old previews.');
+ $output->writeln('');
}
$instanceId = $this->config->getSystemValueString('instanceid');
- $output->writeln("This will migrate all previews from the old preview location to the new one.");
+ $output->writeln('This will migrate all previews from the old preview location to the new one.');
$output->writeln('');
$output->writeln('Fetching previews that need to be migrated …');
- /** @var \OCP\Files\Folder $currentPreviewFolder */
+ /** @var Folder $currentPreviewFolder */
$currentPreviewFolder = $this->rootFolder->get("appdata_$instanceId/preview");
$directoryListing = $currentPreviewFolder->getDirectoryListing();
@@ -140,17 +123,18 @@ class Repair extends Command {
}
if ($total === 0) {
- $output->writeln("All previews are already migrated.");
+ $output->writeln('All previews are already migrated.');
return 0;
}
$output->writeln("A total of $total preview files need to be migrated.");
- $output->writeln("");
- $output->writeln("The migration will always migrate all previews of a single file in a batch. After each batch the process can be canceled by pressing CTRL-C. This will finish the current batch and then stop the migration. This migration can then just be started and it will continue.");
+ $output->writeln('');
+ $output->writeln('The migration will always migrate all previews of a single file in a batch. After each batch the process can be canceled by pressing CTRL-C. This will finish the current batch and then stop the migration. This migration can then just be started and it will continue.');
if ($input->getOption('batch')) {
$output->writeln('Batch mode active: migration is started right away.');
} else {
+ /** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('<info>Should the migration be started? (y/[n]) </info>', false);
@@ -162,12 +146,12 @@ class Repair extends Command {
// register the SIGINT listener late in here to be able to exit in the early process of this command
pcntl_signal(SIGINT, [$this, 'sigIntHandler']);
- $output->writeln("");
- $output->writeln("");
+ $output->writeln('');
+ $output->writeln('');
$section1 = $output->section();
$section2 = $output->section();
$progressBar = new ProgressBar($section2, $total);
- $progressBar->setFormat("%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% Used Memory: %memory:6s%");
+ $progressBar->setFormat('%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% Used Memory: %memory:6s%');
$time = (new \DateTime())->format('H:i:s');
$progressBar->setMessage("$time Starting …");
$progressBar->maxSecondsBetweenRedraws(0.2);
@@ -209,10 +193,10 @@ class Repair extends Command {
$memoryUsage = memory_get_usage();
if ($memoryCheckEnabled && $memoryUsage > $this->memoryTreshold) {
- $section1->writeln("");
- $section1->writeln("");
- $section1->writeln("");
- $section1->writeln(" Stopped process 25 MB before reaching the memory limit to avoid a hard crash.");
+ $section1->writeln('');
+ $section1->writeln('');
+ $section1->writeln('');
+ $section1->writeln(' Stopped process 25 MB before reaching the memory limit to avoid a hard crash.');
$time = (new \DateTime())->format('H:i:s');
$section1->writeln("$time Reached memory limit and stopped to avoid hard crash.");
return 1;
@@ -223,7 +207,7 @@ class Repair extends Command {
$section1->writeln(" Locking \"$lockName\" …", OutputInterface::VERBOSITY_VERBOSE);
$this->lockingProvider->acquireLock($lockName, ILockingProvider::LOCK_EXCLUSIVE);
} catch (LockedException $e) {
- $section1->writeln(" Skipping because it is locked - another process seems to work on this …");
+ $section1->writeln(' Skipping because it is locked - another process seems to work on this …');
continue;
}
@@ -291,14 +275,14 @@ class Repair extends Command {
}
$this->lockingProvider->releaseLock($lockName, ILockingProvider::LOCK_EXCLUSIVE);
- $section1->writeln(" Unlocked", OutputInterface::VERBOSITY_VERBOSE);
+ $section1->writeln(' Unlocked', OutputInterface::VERBOSITY_VERBOSE);
$section1->writeln(" Finished migrating previews of file with fileId $name …");
$progressBar->advance();
}
$progressBar->finish();
- $output->writeln("");
+ $output->writeln('');
return 0;
}
diff --git a/core/Command/Preview/ResetRenderedTexts.php b/core/Command/Preview/ResetRenderedTexts.php
index ec57f632ac9..4cae315e48b 100644
--- a/core/Command/Preview/ResetRenderedTexts.php
+++ b/core/Command/Preview/ResetRenderedTexts.php
@@ -3,25 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2021, Daniel Calviño Sánchez <danxuliu@gmail.com>
- *
- * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Core\Command\Preview;
@@ -137,7 +120,7 @@ class ResetRenderedTexts extends Command {
$qb->select('path', 'mimetype')
->from('filecache')
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($this->previewFolder->getId())));
- $cursor = $qb->execute();
+ $cursor = $qb->executeQuery();
$data = $cursor->fetch();
$cursor->closeCursor();
@@ -170,7 +153,7 @@ class ResetRenderedTexts extends Command {
)
);
- $cursor = $qb->execute();
+ $cursor = $qb->executeQuery();
while ($row = $cursor->fetch()) {
yield $row;