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
32 changes: 28 additions & 4 deletions lib/private/Preview/Bitmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ abstract class Bitmap extends ProviderV2 {
abstract protected function getAllowedMimeTypes(): string;

/**
* {@inheritDoc}
* @return list<string>
*/
abstract protected function getMagicStrings(): array;

abstract protected function getImagickFormatHint(): string;

#[\Override]
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
$tmpPath = $this->getLocalFile($file);
Expand Down Expand Up @@ -66,7 +70,7 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
//new bitmap image object
$image = new Image();
$image->loadFromData((string)$bp);
//check if image object is valid
// Check if image object is valid
return $image->valid() ? $image : null;
}

Expand All @@ -89,15 +93,19 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
private function getResizedPreview($tmpPath, $maxX, $maxY) {
$bp = new Imagick();

if (!$this->isMagicStringSupported($tmpPath)) {
throw new \Exception('Invalid image type: magic string not recognized');
}

// Validate mime type
$bp->pingImage($tmpPath . '[0]');
$bp->pingImage($this->getImagickFormatHint() . ':' . $tmpPath . '[0]');
$mimeType = $bp->getImageMimeType();
if (!preg_match($this->getAllowedMimeTypes(), $mimeType)) {
throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
}

// Layer 0 contains either the bitmap or a flat representation of all vector layers
$bp->readImage($tmpPath . '[0]');
$bp->readImage($this->getImagickFormatHint() . ':' . $tmpPath . '[0]');

$bp = $this->resize($bp, $maxX, $maxY);

Expand All @@ -106,6 +114,22 @@ private function getResizedPreview($tmpPath, $maxX, $maxY) {
return $bp;
}

private function isMagicStringSupported(string $filepath): bool {
$signatures = $this->getMagicStrings();
if (empty($signatures)) {
return true;
}
$length = array_reduce($signatures, static fn (int $carry, string $signature) => max($carry, strlen($signature)), 0);
$firstBytes = file_get_contents($filepath, false, null, 0, $length);
foreach ($signatures as $signature) {
if (str_starts_with($firstBytes, $signature)) {
return true;
}
}

return false;
}

/**
* Returns a resized \Imagick object
*
Expand Down
19 changes: 13 additions & 6 deletions lib/private/Preview/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@

// .otf, .ttf and .pfb
class Font extends Bitmap {
/**
* {@inheritDoc}
*/
#[\Override]
public function getMimeType(): string {
return '/application\/(?:font-sfnt|x-font$)/';
}

/**
* {@inheritDoc}
*/
#[\Override]
protected function getAllowedMimeTypes(): string {
return '/(application|image)\/(?:font-sfnt|x-font|x-otf|x-ttf|x-pfb$)/';
}

#[\Override]
protected function getMagicStrings(): array {
return [
"\x00\x01\x00\x00\x00", // TTF
'OTTO', // OTF
];
}

#[\Override]
protected function getImagickFormatHint(): string {
return 'ttf';
}
}
4 changes: 2 additions & 2 deletions lib/private/Preview/HEIC.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ private function getResizedPreview($tmpPath, $maxX, $maxY) {

// Some HEIC files just contain (or at least are identified as) other formats
// like JPEG. We just need to check if the image is safe to process.
$bp->pingImage($tmpPath . '[0]');
$bp->pingImage('heic:' . $tmpPath . '[0]');
$mimeType = $bp->getImageMimeType();
if (!preg_match('/^image\/(x-)?(png|jpeg|gif|bmp|tiff|webp|hei(f|c)|avif)$/', $mimeType)) {
throw new \Exception('File mime type does not match the preview provider: ' . $mimeType);
}

// Layer 0 contains either the bitmap or a flat representation of all vector layers
$bp->readImage($tmpPath . '[0]');
$bp->readImage('heic:' . $tmpPath . '[0]');

// Fix orientation from EXIF
$bp->autoOrient();
Expand Down
2 changes: 0 additions & 2 deletions lib/private/Preview/IMagickSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ public function __construct(ICacheFactory $cacheFactory) {
}

public function hasExtension(): bool {
return false;
return !is_null($this->imagick);
}

public function supportsFormat(string $format): bool {
return false;
if (is_null($this->imagick)) {
return false;
}
Expand Down
16 changes: 10 additions & 6 deletions lib/private/Preview/Illustrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@

//.ai
class Illustrator extends Bitmap {
/**
* {@inheritDoc}
*/
#[\Override]
public function getMimeType(): string {
return '/application\/illustrator/';
}

/**
* {@inheritDoc}
*/
#[\Override]
protected function getAllowedMimeTypes(): string {
return '/application\/(illustrator|pdf)/';
}

#[\Override]
protected function getMagicStrings(): array {
return ["\x25\x50\x44\x46"];
}

#[\Override]
protected function getImagickFormatHint(): string {
return 'ai';
}
}
16 changes: 10 additions & 6 deletions lib/private/Preview/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@

//.pdf
class PDF extends Bitmap {
/**
* {@inheritDoc}
*/
#[\Override]
public function getMimeType(): string {
return '/application\/pdf/';
}

/**
* {@inheritDoc}
*/
#[\Override]
protected function getAllowedMimeTypes(): string {
return '/application\/pdf/';
}

#[\Override]
protected function getMagicStrings(): array {
return ['%PDF-'];
}

#[\Override]
protected function getImagickFormatHint(): string {
return 'pdf';
}
}
16 changes: 10 additions & 6 deletions lib/private/Preview/Photoshop.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@

//.psd
class Photoshop extends Bitmap {
/**
* {@inheritDoc}
*/
#[\Override]
public function getMimeType(): string {
return '/application\/x-photoshop/';
}

/**
* {@inheritDoc}
*/
#[\Override]
protected function getAllowedMimeTypes(): string {
return '/(application|image)\/(x-photoshop|x-psd)/';
}

#[\Override]
protected function getMagicStrings(): array {
return ['8BPS'];
}

#[\Override]
protected function getImagickFormatHint(): string {
return 'psd';
}
}
16 changes: 10 additions & 6 deletions lib/private/Preview/Postscript.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@

//.eps
class Postscript extends Bitmap {
/**
* {@inheritDoc}
*/
#[\Override]
public function getMimeType(): string {
return '/application\/postscript/';
}

/**
* {@inheritDoc}
*/
#[\Override]
protected function getAllowedMimeTypes(): string {
return '/(application\/postscript|image\/x-eps)/';
}

#[\Override]
protected function getMagicStrings(): array {
return ['%!PS'];
}

#[\Override]
protected function getImagickFormatHint(): string {
return 'ps';
}
}
16 changes: 10 additions & 6 deletions lib/private/Preview/SGI.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@

//.sgi
class SGI extends Bitmap {
/**
* {@inheritDoc}
*/
#[\Override]
public function getMimeType(): string {
return '/image\/(x-)?sgi/';
}

/**
* {@inheritDoc}
*/
#[\Override]
protected function getAllowedMimeTypes(): string {
return '/image\/(x-)?sgi/';
}

#[\Override]
protected function getMagicStrings(): array {
return ["\x01\xDA"];
}

#[\Override]
protected function getImagickFormatHint(): string {
return 'sgi';
}
}
16 changes: 10 additions & 6 deletions lib/private/Preview/TGA.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@

//.tga
class TGA extends Bitmap {
/**
* {@inheritDoc}
*/
#[\Override]
public function getMimeType(): string {
return '/image\/(x-)?t(ar)?ga/';
}

/**
* {@inheritDoc}
*/
#[\Override]
protected function getAllowedMimeTypes(): string {
return '/image\/(x-)?t(ar)?ga/';
}

#[\Override]
protected function getMagicStrings(): array {
return [];
}

#[\Override]
protected function getImagickFormatHint(): string {
return 'tga';
}
}
21 changes: 15 additions & 6 deletions lib/private/Preview/TIFF.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@

//.tiff
class TIFF extends Bitmap {
/**
* {@inheritDoc}
*/
#[\Override]
public function getMimeType(): string {
return '/image\/tiff/';
}

/**
* {@inheritDoc}
*/
#[\Override]
protected function getAllowedMimeTypes(): string {
return '/image\/tiff/';
}

#[\Override]
protected function getMagicStrings(): array {
return [
"II*\x00",
"MM\x00*",
"II+\x00",
"MM\x00+",
];
}

#[\Override]
protected function getImagickFormatHint(): string {
return 'tiff';
}
}
Loading