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
48 changes: 27 additions & 21 deletions lib/private/Preview/Generator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
Expand All @@ -20,34 +21,20 @@
use OCP\Preview\BeforePreviewFetchedEvent;
use OCP\Preview\IProviderV2;
use OCP\Preview\IVersionedPreviewFile;
use Psr\Log\LoggerInterface;

class Generator {
public const SEMAPHORE_ID_ALL = 0x0a11;
public const SEMAPHORE_ID_NEW = 0x07ea;

/** @var IPreview */
private $previewManager;
/** @var IConfig */
private $config;
/** @var IAppData */
private $appData;
/** @var GeneratorHelper */
private $helper;
/** @var IEventDispatcher */
private $eventDispatcher;

public function __construct(
IConfig $config,
IPreview $previewManager,
IAppData $appData,
GeneratorHelper $helper,
IEventDispatcher $eventDispatcher,
private IConfig $config,
private IPreview $previewManager,
private IAppData $appData,
private GeneratorHelper $helper,
private IEventDispatcher $eventDispatcher,
private LoggerInterface $logger,
) {
$this->config = $config;
$this->previewManager = $previewManager;
$this->appData = $appData;
$this->helper = $helper;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand Down Expand Up @@ -83,6 +70,16 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
$mimeType,
));

$this->logger->debug('Requesting preview for {path} with width={width}, height={height}, crop={crop}, mode={mode}, mimeType={mimeType}', [
'path' => $file->getPath(),
'width' => $width,
'height' => $height,
'crop' => $crop,
'mode' => $mode,
'mimeType' => $mimeType,
]);


// since we only ask for one preview, and the generate method return the last one it created, it returns the one we want
return $this->generatePreviews($file, [$specification], $mimeType);
}
Expand All @@ -100,6 +97,7 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
public function generatePreviews(File $file, array $specifications, $mimeType = null) {
//Make sure that we can read the file
if (!$file->isReadable()) {
$this->logger->warning('Cannot read file: {path}, skipping preview generation.', ['path' => $file->getPath()]);
throw new NotFoundException('Cannot read file');
}

Expand All @@ -121,6 +119,7 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
$maxPreviewImage = null; // only load the image when we need it
if ($maxPreview->getSize() === 0) {
$maxPreview->delete();
$this->logger->error("Max preview generated for file {$file->getPath()} has size 0, deleting and throwing exception.");
throw new NotFoundException('Max preview size 0, invalid!');
}

Expand Down Expand Up @@ -167,6 +166,7 @@ public function generatePreviews(File $file, array $specifications, $mimeType =
$maxPreviewImage = $this->helper->getImage($maxPreview);
}

$this->logger->warning('Cached preview not found for file {path}, generating a new preview.', ['path' => $file->getPath()]);
$preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion);
// New file, augment our array
$previewFiles[] = $preview;
Expand Down Expand Up @@ -335,6 +335,11 @@ private function generateProviderPreview(ISimpleFolder $previewFolder, File $fil
$previewConcurrency = $this->getNumConcurrentPreviews('preview_concurrency_new');
$sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency);
try {
$this->logger->debug('Calling preview provider for {mimeType} with width={width}, height={height}', [
'mimeType' => $mimeType,
'width' => $width,
'height' => $height,
]);
$preview = $this->helper->getThumbnail($provider, $file, $width, $height);
} finally {
self::unguardWithSemaphore($sem);
Expand Down Expand Up @@ -558,6 +563,7 @@ private function getCachedPreview($files, $width, $height, $crop, $mimeType, $pr
$path = $this->generatePath($width, $height, $crop, false, $mimeType, $prefix);
foreach ($files as $file) {
if ($file->getName() === $path) {
$this->logger->debug('Found cached preview: {path}', ['path' => $path]);
return $file;
}
}
Expand Down
11 changes: 7 additions & 4 deletions lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
use OCP\IBinaryFinder;
use OCP\IConfig;
use OCP\IPreview;
use OCP\IServerContainer;
use OCP\Preview\IProviderV2;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

use function array_key_exists;

class PreviewManager implements IPreview {
Expand All @@ -47,7 +49,7 @@ class PreviewManager implements IPreview {
* @psalm-var array<string, null>
*/
private array $loadedBootstrapProviders = [];
private IServerContainer $container;
private ContainerInterface $container;
private IBinaryFinder $binaryFinder;
private IMagickSupport $imagickSupport;
private bool $enablePreviews;
Expand All @@ -60,7 +62,7 @@ public function __construct(
GeneratorHelper $helper,
?string $userId,
Coordinator $bootstrapCoordinator,
IServerContainer $container,
ContainerInterface $container,
IBinaryFinder $binaryFinder,
IMagickSupport $imagickSupport,
) {
Expand Down Expand Up @@ -136,7 +138,8 @@ private function getGenerator(): Generator {
$this->rootFolder,
$this->config
),
$this->eventDispatcher
$this->eventDispatcher,
$this->container->get(LoggerInterface::class),
);
}
return $this->generator;
Expand Down
7 changes: 6 additions & 1 deletion tests/lib/Preview/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCP\IPreview;
use OCP\Preview\BeforePreviewFetchedEvent;
use OCP\Preview\IProviderV2;
use Psr\Log\LoggerInterface;

class GeneratorTest extends \Test\TestCase {
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
Expand All @@ -39,6 +40,8 @@ class GeneratorTest extends \Test\TestCase {
/** @var Generator */
private $generator;

private LoggerInterface|\PHPUnit\Framework\MockObject\MockObject $logger;

protected function setUp(): void {
parent::setUp();

Expand All @@ -47,13 +50,15 @@ protected function setUp(): void {
$this->appData = $this->createMock(IAppData::class);
$this->helper = $this->createMock(GeneratorHelper::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->logger = $this->createMock(LoggerInterface::class);

$this->generator = new Generator(
$this->config,
$this->previewManager,
$this->appData,
$this->helper,
$this->eventDispatcher
$this->eventDispatcher,
$this->logger,
);
}

Expand Down
Loading