Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/Command/PreGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,35 @@

use OCA\PreviewGenerator\Exceptions\EncryptionEnabledException;
use OCA\PreviewGenerator\Service\PreGenerateService;
use OCA\PreviewGenerator\Support\LoggerInterfaceToOutputAdapter;
use OCP\IConfig;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class PreGenerate extends Command {
public function __construct(
private readonly PreGenerateService $preGenerateService,
private readonly IConfig $config,
) {
parent::__construct();
}

protected function configure(): void {
$this
->setName('preview:pre-generate')
->setDescription('Pre generate only images that have been added or changed since the last regular run');
->setDescription('Pre-generate only images that have been added or changed since the last regular run');
}

protected function execute(InputInterface $input, OutputInterface $output): int {
// Set timestamp output
$formatter = new TimestampFormatter($this->config, $output->getFormatter());
$output->setFormatter($formatter);

$this->preGenerateService->setLogger(new LoggerInterfaceToOutputAdapter($output));

try {
$this->preGenerateService->preGenerate($output);
$this->preGenerateService->preGenerate();
} catch (EncryptionEnabledException $e) {
$output->writeln('<error>Encryption is enabled. Aborted.</error>');
return 1;
Expand Down
32 changes: 11 additions & 21 deletions lib/Service/PreGenerateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
namespace OCA\PreviewGenerator\Service;

use OC\DB\Exceptions\DbalException;
use OCA\PreviewGenerator\Command\TimestampFormatter;
use OCA\PreviewGenerator\Exceptions\EncryptionEnabledException;
use OCA\PreviewGenerator\SizeHelper;
use OCA\PreviewGenerator\Support\OutputInterfaceLoggerAdapter;
use OCA\PreviewGenerator\Support\PreviewLimiter\PreviewLimiter;
use OCP\AppFramework\Db\TTransactional;
use OCP\AppFramework\Utility\ITimeFactory;
Expand All @@ -27,15 +25,15 @@
use OCP\IDBConnection;
use OCP\IPreview;
use OCP\IUserManager;
use Symfony\Component\Console\Output\OutputInterface;
use Psr\Log\LoggerInterface;

class PreGenerateService {
use TTransactional;

/* @return array{width: int, height: int, crop: bool} */
private array $specifications;

private ?OutputInterface $output = null;
private ?LoggerInterface $logger = null;
private ?PreviewLimiter $limiter = null;

public function __construct(
Expand All @@ -51,34 +49,28 @@ public function __construct(
) {
}

public function setLogger(LoggerInterface $logger): void {
$this->logger = $logger;
}

public function setLimiter(PreviewLimiter $limiter): void {
$this->limiter = $limiter;
}

/**
* @throws EncryptionEnabledException If encryption is enabled.
*/
public function preGenerate(OutputInterface $output): void {
public function preGenerate(): void {
if ($this->encryptionManager->isEnabled()) {
throw new EncryptionEnabledException();
}

// Set timestamp output
if (!($output instanceof OutputInterfaceLoggerAdapter)) {
$formatter = new TimestampFormatter($this->config, $output->getFormatter());
$output->setFormatter($formatter);
}

$this->output = $output;

if ($this->limiter) {
$output->writeln('Using limiter: ' . get_class($this->limiter));
$this->logger?->debug('Using limiter: ' . get_class($this->limiter));
}

$this->specifications = $this->sizeHelper->generateSpecifications();
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_VERY_VERBOSE) {
$output->writeln('Specifications: ' . json_encode($this->specifications));
}
$this->logger?->debug('Specifications: ' . json_encode($this->specifications));
$this->startProcessing();
}

Expand Down Expand Up @@ -163,9 +155,7 @@ private function processFile(File $file): void {
}

if ($this->previewGenerator->isMimeSupported($file->getMimeType())) {
if ($this->output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
$this->output->writeln('Generating previews for ' . $file->getPath());
}
$this->logger?->debug('Generating previews for ' . $file->getPath());

try {
$this->previewGenerator->generatePreviews($file, $this->specifications);
Expand All @@ -174,7 +164,7 @@ private function processFile(File $file): void {
} catch (\InvalidArgumentException|GenericFileException $e) {
$class = $e::class;
$error = $e->getMessage();
$this->output->writeln("<error>{$class}: {$error}</error>");
$this->logger?->error("{$class}: {$error}");
} catch (DbalException $e) {
// Since the introduction of the oc_previews table, preview duplication caused by
// duplicated specifications will cause a UniqueConstraintViolationException. We can
Expand Down
101 changes: 101 additions & 0 deletions lib/Support/LoggerInterfaceToOutputAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\PreviewGenerator\Support;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Stringable;
use Symfony\Component\Console\Output\OutputInterface;

class LoggerInterfaceToOutputAdapter implements LoggerInterface {
public function __construct(
private readonly OutputInterface $output,
) {
var_dump($output->getVerbosity());
}

public function emergency(string|Stringable $message, array $context = []): void {
$this->writeToOutput('error', $message, $context);
}

public function alert(string|Stringable $message, array $context = []): void {
$this->writeToOutput('error', $message, $context);
}

public function critical(string|Stringable $message, array $context = []): void {
$this->writeToOutput('error', $message, $context);
}

public function error(string|Stringable $message, array $context = []): void {
$this->writeToOutput('error', $message, $context);
}

public function warning(string|Stringable $message, array $context = []): void {
if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_NORMAL) {
return;
}

$this->writeToOutput(null, $message, $context);
}

public function notice(string|Stringable $message, array $context = []): void {
if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) {
return;
}

$this->writeToOutput(null, $message, $context);
}

public function info(string|Stringable $message, array $context = []): void {
if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_VERY_VERBOSE) {
return;
}

$this->writeToOutput(null, $message, $context);
}

public function debug(string|Stringable $message, array $context = []): void {
if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_DEBUG) {
return;
}

$this->writeToOutput(null, $message, $context);
}

public function log($level, $message, array $context = []): void {
match ($level) {
LogLevel::EMERGENCY => $this->emergency($message, $context),
LogLevel::ALERT => $this->alert($message, $context),
LogLevel::CRITICAL => $this->critical($message, $context),
LogLevel::ERROR => $this->error($message, $context),
LogLevel::WARNING => $this->warning($message, $context),
LogLevel::NOTICE => $this->notice($message, $context),
LogLevel::INFO => $this->info($message, $context),
LogLevel::DEBUG => $this->debug($message, $context),
};
}

private function writeToOutput(
?string $decorator,
string|Stringable $message,
array $context = [],
): void {
$message = (string)$message;
if (!empty($context)) {
$message .= ' ' . json_encode($context);
}

if ($decorator) {
$this->output->writeln("<$decorator>$message</$decorator>");
} else {
$this->output->writeln($message);
}
}
}
99 changes: 0 additions & 99 deletions lib/Support/OutputInterfaceLoggerAdapter.php

This file was deleted.

Loading