Skip to content

Commit 065a976

Browse files
Merge pull request nextcloud#52014 from nextcloud/fix/fix-movie-preview-construct
fix(preview): Fix constructor parameter name and default value
2 parents ba2b778 + 3ca690a commit 065a976

3 files changed

Lines changed: 49 additions & 54 deletions

File tree

lib/private/Preview/Movie.php

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,24 @@
55
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
8+
89
namespace OC\Preview;
910

1011
use OCP\Files\File;
1112
use OCP\Files\FileInfo;
1213
use OCP\IConfig;
1314
use OCP\IImage;
15+
use OCP\ITempManager;
1416
use OCP\Server;
1517
use Psr\Log\LoggerInterface;
1618

1719
class Movie extends ProviderV2 {
1820
private IConfig $config;
1921

20-
/**
21-
* @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2
22-
* @var string
23-
*/
24-
public static $avconvBinary;
25-
26-
/**
27-
* @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2
28-
* @var string
29-
*/
30-
public static $ffmpegBinary;
31-
32-
/** @var string */
33-
private $binary;
22+
private ?string $binary = null;
3423

35-
public function __construct(array $config) {
36-
parent::__construct($config);
24+
public function __construct(array $options = []) {
25+
parent::__construct($options);
3726
$this->config = Server::get(IConfig::class);
3827
}
3928

@@ -45,14 +34,9 @@ public function getMimeType(): string {
4534
* {@inheritDoc}
4635
*/
4736
public function isAvailable(FileInfo $file): bool {
48-
// TODO: remove when avconv is dropped
4937
if (is_null($this->binary)) {
5038
if (isset($this->options['movieBinary'])) {
5139
$this->binary = $this->options['movieBinary'];
52-
} elseif (is_string(self::$avconvBinary)) {
53-
$this->binary = self::$avconvBinary;
54-
} elseif (is_string(self::$ffmpegBinary)) {
55-
$this->binary = self::$ffmpegBinary;
5640
}
5741
}
5842
return is_string($this->binary);
@@ -89,14 +73,11 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
8973
return null;
9074
}
9175

92-
$result = null;
93-
if (is_string($absPath)) {
94-
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
76+
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
77+
if ($result === null) {
78+
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
9579
if ($result === null) {
96-
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
97-
if ($result === null) {
98-
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
99-
}
80+
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
10081
}
10182
}
10283

@@ -137,7 +118,15 @@ private function useHdr(string $absPath): bool {
137118
}
138119

139120
private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $second): ?IImage {
140-
$tmpPath = \OC::$server->getTempManager()->getTemporaryFile();
121+
$tmpPath = Server::get(ITempManager::class)->getTemporaryFile();
122+
123+
if ($tmpPath === false) {
124+
Server::get(LoggerInterface::class)->error(
125+
'Failed to get local file to generate thumbnail for: ' . $absPath,
126+
['app' => 'core']
127+
);
128+
return null;
129+
}
141130

142131
$binaryType = substr(strrchr($this->binary, '/'), 1);
143132

@@ -190,7 +179,7 @@ private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $s
190179
}
191180

192181
if ($second === 0) {
193-
$logger = \OC::$server->get(LoggerInterface::class);
182+
$logger = Server::get(LoggerInterface::class);
194183
$logger->info('Movie preview generation failed Output: {output}', ['app' => 'core', 'output' => $output]);
195184
}
196185

lib/private/Preview/ProviderV2.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,23 @@
66
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
77
* SPDX-License-Identifier: AGPL-3.0-or-later
88
*/
9+
910
namespace OC\Preview;
1011

1112
use OCP\Files\File;
1213
use OCP\Files\FileInfo;
1314
use OCP\IImage;
15+
use OCP\ITempManager;
1416
use OCP\Preview\IProviderV2;
17+
use OCP\Server;
18+
use Psr\Log\LoggerInterface;
1519

1620
abstract class ProviderV2 implements IProviderV2 {
17-
/** @var array */
18-
protected $options;
19-
20-
/** @var array */
21-
protected $tmpFiles = [];
21+
protected array $tmpFiles = [];
2222

23-
/**
24-
* Constructor
25-
*
26-
* @param array $options
27-
*/
28-
public function __construct(array $options = []) {
29-
$this->options = $options;
23+
public function __construct(
24+
protected array $options = [],
25+
) {
3026
}
3127

3228
/**
@@ -50,7 +46,7 @@ public function isAvailable(FileInfo $file): bool {
5046
* @param File $file
5147
* @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
5248
* @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
53-
* @return null|\OCP\IImage false if no preview was generated
49+
* @return null|\OCP\IImage null if no preview was generated
5450
* @since 17.0.0
5551
*/
5652
abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage;
@@ -63,12 +59,19 @@ protected function useTempFile(File $file): bool {
6359
* Get a path to either the local file or temporary file
6460
*
6561
* @param File $file
66-
* @param int $maxSize maximum size for temporary files
67-
* @return string|false
62+
* @param ?int $maxSize maximum size for temporary files
6863
*/
69-
protected function getLocalFile(File $file, ?int $maxSize = null) {
64+
protected function getLocalFile(File $file, ?int $maxSize = null): string|false {
7065
if ($this->useTempFile($file)) {
71-
$absPath = \OC::$server->getTempManager()->getTemporaryFile();
66+
$absPath = Server::get(ITempManager::class)->getTemporaryFile();
67+
68+
if ($absPath === false) {
69+
Server::get(LoggerInterface::class)->error(
70+
'Failed to get local file to generate thumbnail for: ' . $file->getPath(),
71+
['app' => 'core']
72+
);
73+
return false;
74+
}
7275

7376
$content = $file->fopen('r');
7477
if ($content === false) {

tests/lib/Preview/MovieTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace Test\Preview;
99

10+
use OCP\IBinaryFinder;
11+
use OCP\Server;
12+
1013
/**
1114
* Class MovieTest
1215
*
@@ -16,20 +19,20 @@
1619
*/
1720
class MovieTest extends Provider {
1821
protected function setUp(): void {
19-
$avconvBinary = \OC_Helper::findBinaryPath('avconv');
20-
$ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg');
22+
$binaryFinder = Server::get(IBinaryFinder::class);
23+
$movieBinary = $binaryFinder->findBinaryPath('avconv');
24+
if (!is_string($movieBinary)) {
25+
$movieBinary = $binaryFinder->findBinaryPath('ffmpeg');
26+
}
2127

22-
if ($avconvBinary || $ffmpegBinary) {
28+
if (is_string($movieBinary)) {
2329
parent::setUp();
2430

25-
\OC\Preview\Movie::$avconvBinary = $avconvBinary;
26-
\OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
27-
2831
$fileName = 'testimage.mp4';
2932
$this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
3033
$this->width = 560;
3134
$this->height = 320;
32-
$this->provider = new \OC\Preview\Movie;
35+
$this->provider = new \OC\Preview\Movie(['movieBinary' => $movieBinary]);
3336
} else {
3437
$this->markTestSkipped('No Movie provider present');
3538
}

0 commit comments

Comments
 (0)