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
49 changes: 19 additions & 30 deletions lib/private/Preview/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,24 @@
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OC\Preview;

use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\IConfig;
use OCP\IImage;
use OCP\ITempManager;
use OCP\Server;
use Psr\Log\LoggerInterface;

class Movie extends ProviderV2 {
private IConfig $config;

/**
* @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2
* @var string
*/
public static $avconvBinary;

/**
* @deprecated 23.0.0 pass option to \OCP\Preview\ProviderV2
* @var string
*/
public static $ffmpegBinary;

/** @var string */
private $binary;
private ?string $binary = null;

public function __construct(array $config) {
parent::__construct($config);
public function __construct(array $options = []) {
parent::__construct($options);
$this->config = Server::get(IConfig::class);
}

Expand All @@ -45,14 +34,9 @@ public function getMimeType(): string {
* {@inheritDoc}
*/
public function isAvailable(FileInfo $file): bool {
// TODO: remove when avconv is dropped
if (is_null($this->binary)) {
if (isset($this->options['movieBinary'])) {
$this->binary = $this->options['movieBinary'];
} elseif (is_string(self::$avconvBinary)) {
$this->binary = self::$avconvBinary;
} elseif (is_string(self::$ffmpegBinary)) {
$this->binary = self::$ffmpegBinary;
}
}
return is_string($this->binary);
Expand Down Expand Up @@ -89,14 +73,11 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
return null;
}

$result = null;
if (is_string($absPath)) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
if ($result === null) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
if ($result === null) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
if ($result === null) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
}
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
}
}

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

private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $second): ?IImage {
$tmpPath = \OC::$server->getTempManager()->getTemporaryFile();
$tmpPath = Server::get(ITempManager::class)->getTemporaryFile();

if ($tmpPath === false) {
Server::get(LoggerInterface::class)->error(
'Failed to get local file to generate thumbnail for: ' . $absPath,
['app' => 'core']
);
return null;
}

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

Expand Down Expand Up @@ -190,7 +179,7 @@ private function generateThumbNail(int $maxX, int $maxY, string $absPath, int $s
}

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

Expand Down
37 changes: 20 additions & 17 deletions lib/private/Preview/ProviderV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,23 @@
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OC\Preview;

use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\IImage;
use OCP\ITempManager;
use OCP\Preview\IProviderV2;
use OCP\Server;
use Psr\Log\LoggerInterface;

abstract class ProviderV2 implements IProviderV2 {
/** @var array */
protected $options;

/** @var array */
protected $tmpFiles = [];
protected array $tmpFiles = [];

/**
* Constructor
*
* @param array $options
*/
public function __construct(array $options = []) {
$this->options = $options;
public function __construct(
protected array $options = [],
) {
}

/**
Expand All @@ -50,7 +46,7 @@ public function isAvailable(FileInfo $file): bool {
* @param File $file
* @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
* @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
* @return null|\OCP\IImage false if no preview was generated
* @return null|\OCP\IImage null if no preview was generated
* @since 17.0.0
*/
abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage;
Expand All @@ -63,12 +59,19 @@ protected function useTempFile(File $file): bool {
* Get a path to either the local file or temporary file
*
* @param File $file
* @param int $maxSize maximum size for temporary files
* @return string|false
* @param ?int $maxSize maximum size for temporary files
*/
protected function getLocalFile(File $file, ?int $maxSize = null) {
protected function getLocalFile(File $file, ?int $maxSize = null): string|false {
if ($this->useTempFile($file)) {
$absPath = \OC::$server->getTempManager()->getTemporaryFile();
$absPath = Server::get(ITempManager::class)->getTemporaryFile();

if ($absPath === false) {
Server::get(LoggerInterface::class)->error(
'Failed to get local file to generate thumbnail for: ' . $file->getPath(),
['app' => 'core']
);
return false;
}

$content = $file->fopen('r');
if ($content === false) {
Expand Down
17 changes: 10 additions & 7 deletions tests/lib/Preview/MovieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace Test\Preview;

use OCP\IBinaryFinder;
use OCP\Server;

/**
* Class MovieTest
*
Expand All @@ -16,20 +19,20 @@
*/
class MovieTest extends Provider {
protected function setUp(): void {
$avconvBinary = \OC_Helper::findBinaryPath('avconv');
$ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg');
$binaryFinder = Server::get(IBinaryFinder::class);
$movieBinary = $binaryFinder->findBinaryPath('avconv');
if (!is_string($movieBinary)) {
$movieBinary = $binaryFinder->findBinaryPath('ffmpeg');
}

if ($avconvBinary || $ffmpegBinary) {
if (is_string($movieBinary)) {
parent::setUp();

\OC\Preview\Movie::$avconvBinary = $avconvBinary;
\OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;

$fileName = 'testimage.mp4';
$this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName);
$this->width = 560;
$this->height = 320;
$this->provider = new \OC\Preview\Movie;
$this->provider = new \OC\Preview\Movie(['movieBinary' => $movieBinary]);
} else {
$this->markTestSkipped('No Movie provider present');
}
Expand Down
Loading