Fix typing in OC\Preview

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2022-02-24 14:42:55 +01:00
parent 6da8a6d62a
commit ebe731f014
No known key found for this signature in database
GPG key ID: A3E2F658B28C760A
7 changed files with 51 additions and 25 deletions

View file

@ -29,7 +29,7 @@ namespace OC\Preview;
use Imagick;
use OCP\Files\File;
use OCP\IImage;
use OCP\ILogger;
use Psr\Log\LoggerInterface;
/**
* Creates a PNG preview using ImageMagick via the PECL extension
@ -43,16 +43,25 @@ abstract class Bitmap extends ProviderV2 {
*/
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
$tmpPath = $this->getLocalFile($file);
if ($tmpPath === false) {
\OC::$server->get(LoggerInterface::class)->error(
'Failed to get thumbnail for: ' . $file->getPath(),
['app' => 'core']
);
return null;
}
// Creates \Imagick object from bitmap or vector file
try {
$bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
} catch (\Exception $e) {
\OC::$server->getLogger()->logException($e, [
'message' => 'File: ' . $file->getPath() . ' Imagick says:',
'level' => ILogger::ERROR,
'app' => 'core',
]);
\OC::$server->get(LoggerInterface::class)->error(
'File: ' . $file->getPath() . ' Imagick says:',
[
'exception' => $e,
'app' => 'core',
]
);
return null;
}

View file

@ -30,7 +30,7 @@ use OCP\IImage;
* Extracts a preview from files that embed them in an ZIP archive
*/
abstract class Bundled extends ProviderV2 {
protected function extractThumbnail(File $file, $path): ?IImage {
protected function extractThumbnail(File $file, string $path): ?IImage {
$sourceTmp = \OC::$server->getTempManager()->getTemporaryFile();
$targetTmp = \OC::$server->getTempManager()->getTemporaryFile();

View file

@ -32,7 +32,7 @@ namespace OC\Preview;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\IImage;
use OCP\ILogger;
use Psr\Log\LoggerInterface;
/**
* Creates a JPG preview using ImageMagick via the PECL extension
@ -63,17 +63,26 @@ class HEIC extends ProviderV2 {
}
$tmpPath = $this->getLocalFile($file);
if ($tmpPath === false) {
\OC::$server->get(LoggerInterface::class)->error(
'Failed to get thumbnail for: ' . $file->getPath(),
['app' => 'core']
);
return null;
}
// Creates \Imagick object from the heic file
try {
$bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
$bp->setFormat('jpg');
} catch (\Exception $e) {
\OC::$server->getLogger()->logException($e, [
'message' => 'File: ' . $file->getPath() . ' Imagick says:',
'level' => ILogger::ERROR,
'app' => 'core',
]);
\OC::$server->get(LoggerInterface::class)->error(
'File: ' . $file->getPath() . ' Imagick says:',
[
'exception' => $e,
'app' => 'core',
]
);
return null;
}
@ -109,7 +118,7 @@ class HEIC extends ProviderV2 {
$bp->setImageFormat('jpg');
$bp = $this->resize($bp, $maxX, $maxY);
return $bp;
}

View file

@ -39,11 +39,11 @@ class Krita extends Bundled {
*/
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
$image = $this->extractThumbnail($file, 'mergedimage.png');
if ($image->valid()) {
if (($image !== null) && $image->valid()) {
return $image;
}
$image = $this->extractThumbnail($file, 'preview.png');
if ($image->valid()) {
if (($image !== null) && $image->valid()) {
return $image;
}
return null;

View file

@ -99,11 +99,14 @@ class Movie extends ProviderV2 {
foreach ($sizeAttempts as $size) {
$absPath = $this->getLocalFile($file, $size);
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
if ($result === null) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
$result = null;
if (is_string($absPath)) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 5);
if ($result === null) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 1);
if ($result === null) {
$result = $this->generateThumbNail($maxX, $maxY, $absPath, 0);
}
}
}

View file

@ -42,7 +42,7 @@ class OpenDocument extends Bundled {
*/
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
$image = $this->extractThumbnail($file, 'Thumbnails/thumbnail.png');
if ($image->valid()) {
if (($image !== null) && $image->valid()) {
return $image;
}
return null;

View file

@ -72,7 +72,7 @@ abstract class ProviderV2 implements IProviderV2 {
*/
abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage;
protected function useTempFile(File $file) {
protected function useTempFile(File $file): bool {
return $file->isEncrypted() || !$file->getStorage()->isLocal();
}
@ -81,9 +81,9 @@ abstract class ProviderV2 implements IProviderV2 {
*
* @param File $file
* @param int $maxSize maximum size for temporary files
* @return string
* @return string|false
*/
protected function getLocalFile(File $file, int $maxSize = null): string {
protected function getLocalFile(File $file, int $maxSize = null) {
if ($this->useTempFile($file)) {
$absPath = \OC::$server->getTempManager()->getTemporaryFile();
@ -97,7 +97,12 @@ abstract class ProviderV2 implements IProviderV2 {
$this->tmpFiles[] = $absPath;
return $absPath;
} else {
return $file->getStorage()->getLocalFile($file->getInternalPath());
$path = $file->getStorage()->getLocalFile($file->getInternalPath());
if (is_string($path)) {
return $path;
} else {
return false;
}
}
}