Skip to content

Commit 25bc18c

Browse files
authored
Merge pull request #52221 from nextcloud/feat/no-issue/add-logging-preview-generation
feat: add logging to preview generation
2 parents 9f6b2b8 + 6b89838 commit 25bc18c

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

lib/private/Preview/Generator.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
45
* SPDX-License-Identifier: AGPL-3.0-or-later
@@ -20,34 +21,20 @@
2021
use OCP\Preview\BeforePreviewFetchedEvent;
2122
use OCP\Preview\IProviderV2;
2223
use OCP\Preview\IVersionedPreviewFile;
24+
use Psr\Log\LoggerInterface;
2325

2426
class Generator {
2527
public const SEMAPHORE_ID_ALL = 0x0a11;
2628
public const SEMAPHORE_ID_NEW = 0x07ea;
2729

28-
/** @var IPreview */
29-
private $previewManager;
30-
/** @var IConfig */
31-
private $config;
32-
/** @var IAppData */
33-
private $appData;
34-
/** @var GeneratorHelper */
35-
private $helper;
36-
/** @var IEventDispatcher */
37-
private $eventDispatcher;
38-
3930
public function __construct(
40-
IConfig $config,
41-
IPreview $previewManager,
42-
IAppData $appData,
43-
GeneratorHelper $helper,
44-
IEventDispatcher $eventDispatcher,
31+
private IConfig $config,
32+
private IPreview $previewManager,
33+
private IAppData $appData,
34+
private GeneratorHelper $helper,
35+
private IEventDispatcher $eventDispatcher,
36+
private LoggerInterface $logger,
4537
) {
46-
$this->config = $config;
47-
$this->previewManager = $previewManager;
48-
$this->appData = $appData;
49-
$this->helper = $helper;
50-
$this->eventDispatcher = $eventDispatcher;
5138
}
5239

5340
/**
@@ -83,6 +70,16 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
8370
$mimeType,
8471
));
8572

73+
$this->logger->debug('Requesting preview for {path} with width={width}, height={height}, crop={crop}, mode={mode}, mimeType={mimeType}', [
74+
'path' => $file->getPath(),
75+
'width' => $width,
76+
'height' => $height,
77+
'crop' => $crop,
78+
'mode' => $mode,
79+
'mimeType' => $mimeType,
80+
]);
81+
82+
8683
// since we only ask for one preview, and the generate method return the last one it created, it returns the one we want
8784
return $this->generatePreviews($file, [$specification], $mimeType);
8885
}
@@ -100,6 +97,7 @@ public function getPreview(File $file, $width = -1, $height = -1, $crop = false,
10097
public function generatePreviews(File $file, array $specifications, $mimeType = null) {
10198
//Make sure that we can read the file
10299
if (!$file->isReadable()) {
100+
$this->logger->warning('Cannot read file: {path}, skipping preview generation.', ['path' => $file->getPath()]);
103101
throw new NotFoundException('Cannot read file');
104102
}
105103

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

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

169+
$this->logger->warning('Cached preview not found for file {path}, generating a new preview.', ['path' => $file->getPath()]);
170170
$preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion);
171171
// New file, augment our array
172172
$previewFiles[] = $preview;
@@ -335,6 +335,11 @@ private function generateProviderPreview(ISimpleFolder $previewFolder, File $fil
335335
$previewConcurrency = $this->getNumConcurrentPreviews('preview_concurrency_new');
336336
$sem = self::guardWithSemaphore(self::SEMAPHORE_ID_NEW, $previewConcurrency);
337337
try {
338+
$this->logger->debug('Calling preview provider for {mimeType} with width={width}, height={height}', [
339+
'mimeType' => $mimeType,
340+
'width' => $width,
341+
'height' => $height,
342+
]);
338343
$preview = $this->helper->getThumbnail($provider, $file, $width, $height);
339344
} finally {
340345
self::unguardWithSemaphore($sem);
@@ -558,6 +563,7 @@ private function getCachedPreview($files, $width, $height, $crop, $mimeType, $pr
558563
$path = $this->generatePath($width, $height, $crop, false, $mimeType, $prefix);
559564
foreach ($files as $file) {
560565
if ($file->getName() === $path) {
566+
$this->logger->debug('Found cached preview: {path}', ['path' => $path]);
561567
return $file;
562568
}
563569
}

lib/private/PreviewManager.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
use OCP\IBinaryFinder;
2222
use OCP\IConfig;
2323
use OCP\IPreview;
24-
use OCP\IServerContainer;
2524
use OCP\Preview\IProviderV2;
25+
use Psr\Container\ContainerInterface;
26+
use Psr\Log\LoggerInterface;
27+
2628
use function array_key_exists;
2729

2830
class PreviewManager implements IPreview {
@@ -47,7 +49,7 @@ class PreviewManager implements IPreview {
4749
* @psalm-var array<string, null>
4850
*/
4951
private array $loadedBootstrapProviders = [];
50-
private IServerContainer $container;
52+
private ContainerInterface $container;
5153
private IBinaryFinder $binaryFinder;
5254
private IMagickSupport $imagickSupport;
5355
private bool $enablePreviews;
@@ -60,7 +62,7 @@ public function __construct(
6062
GeneratorHelper $helper,
6163
?string $userId,
6264
Coordinator $bootstrapCoordinator,
63-
IServerContainer $container,
65+
ContainerInterface $container,
6466
IBinaryFinder $binaryFinder,
6567
IMagickSupport $imagickSupport,
6668
) {
@@ -136,7 +138,8 @@ private function getGenerator(): Generator {
136138
$this->rootFolder,
137139
$this->config
138140
),
139-
$this->eventDispatcher
141+
$this->eventDispatcher,
142+
$this->container->get(LoggerInterface::class),
140143
);
141144
}
142145
return $this->generator;

tests/lib/Preview/GeneratorTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OCP\IPreview;
2020
use OCP\Preview\BeforePreviewFetchedEvent;
2121
use OCP\Preview\IProviderV2;
22+
use Psr\Log\LoggerInterface;
2223

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

43+
private LoggerInterface|\PHPUnit\Framework\MockObject\MockObject $logger;
44+
4245
protected function setUp(): void {
4346
parent::setUp();
4447

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

5155
$this->generator = new Generator(
5256
$this->config,
5357
$this->previewManager,
5458
$this->appData,
5559
$this->helper,
56-
$this->eventDispatcher
60+
$this->eventDispatcher,
61+
$this->logger,
5762
);
5863
}
5964

0 commit comments

Comments
 (0)