diff --git a/apps/files/lib/helper.php b/apps/files/lib/helper.php index be0992b198..50b4e8338a 100644 --- a/apps/files/lib/helper.php +++ b/apps/files/lib/helper.php @@ -31,7 +31,7 @@ class Helper /** * Determine icon for a given file * - * @param \OC\Files\FileInfo $file file info + * @param \OCP\Files\FileInfo $file file info * @return string icon URL */ public static function determineIcon($file) { @@ -111,7 +111,7 @@ class Helper $entry['mtime'] = $i['mtime'] * 1000; // only pick out the needed attributes $entry['icon'] = \OCA\Files\Helper::determineIcon($i); - if (\OC::$server->getPreviewManager()->isMimeSupported($i['mimetype'])) { + if (\OC::$server->getPreviewManager()->isAvailable($i)) { $entry['isPreviewAvailable'] = true; } $entry['name'] = $i->getName(); diff --git a/core/ajax/preview.php b/core/ajax/preview.php index edbd41d2db..d2811f39dd 100644 --- a/core/ajax/preview.php +++ b/core/ajax/preview.php @@ -30,7 +30,8 @@ if ($maxX === 0 || $maxY === 0) { try { $preview = new \OC\Preview(\OC_User::getUser(), 'files'); - if (!$always and !$preview->isMimeSupported(\OC\Files\Filesystem::getMimeType($file))) { + $info = \OC\Files\Filesystem::getFileInfo($file); + if (!$always and !$preview->isAvailable($info)) { \OC_Response::setStatus(404); } else { $preview->setFile($file); diff --git a/lib/private/preview.php b/lib/private/preview.php index 6172519c7d..aeb9806904 100755 --- a/lib/private/preview.php +++ b/lib/private/preview.php @@ -13,6 +13,7 @@ */ namespace OC; +use OC\Files\Filesystem; use OC\Preview\Provider; require_once 'preview/image.php'; @@ -731,6 +732,35 @@ class Preview { $preview->deleteAllPreviews(); } + /** + * Check if a preview can be generated for a file + * + * @param \OC\Files\FileInfo $file + * @return bool + */ + public static function isAvailable($file) { + if (!\OC_Config::getValue('enable_previews', true)) { + return false; + } + + //check if there are preview backends + if (empty(self::$providers)) { + self::initProviders(); + } + + //remove last element because it has the mimetype * + $providers = array_slice(self::$providers, 0, -1); + foreach ($providers as $supportedMimeType => $provider) { + /** + * @var \OC\Preview\Provider $provider + */ + if (preg_match($supportedMimeType, $file->getMimetype())) { + return $provider->isAvailable($file); + } + } + return false; + } + /** * @param string $mimeType * @return bool diff --git a/lib/private/preview/provider.php b/lib/private/preview/provider.php index f769823f6e..f544c2c4b1 100644 --- a/lib/private/preview/provider.php +++ b/lib/private/preview/provider.php @@ -10,6 +10,16 @@ abstract class Provider { abstract public function getMimeType(); + /** + * Check if a preview can be generated for $path + * + * @param string $path + * @return bool + */ + public function isAvailable($path) { + return true; + } + /** * get thumbnail for file at path $path * @param string $path Path of file diff --git a/lib/private/preview/txt.php b/lib/private/preview/txt.php index 063543c627..b81436baa0 100644 --- a/lib/private/preview/txt.php +++ b/lib/private/preview/txt.php @@ -13,6 +13,16 @@ class TXT extends Provider { return '/text\/plain/'; } + /** + * Check if a preview can be generated for $path + * + * @param \OC\Files\FileInfo $file + * @return bool + */ + public function isAvailable($file) { + return $file->getSize() > 5; + } + /** * @param string $path * @param int $maxX diff --git a/lib/private/previewmanager.php b/lib/private/previewmanager.php index 23dbee13c7..85bf609743 100755 --- a/lib/private/previewmanager.php +++ b/lib/private/previewmanager.php @@ -14,25 +14,35 @@ use OCP\IPreview; class PreviewManager implements IPreview { /** * return a preview of a file + * * @param string $file The path to the file where you want a thumbnail from * @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 * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly * @return \OCP\Image */ - function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) - { + function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) { $preview = new \OC\Preview('', '/', $file, $maxX, $maxY, $scaleUp); return $preview->getPreview(); } /** * returns true if the passed mime type is supported + * * @param string $mimeType * @return boolean */ - function isMimeSupported($mimeType = '*') - { + function isMimeSupported($mimeType = '*') { return \OC\Preview::isMimeSupported($mimeType); } + + /** + * Check if a preview can be generated for a file + * + * @param \OC\Files\FileInfo $file + * @return bool + */ + function isAvailable($file) { + return \OC\Preview::isAvailable($file); + } } diff --git a/lib/public/ipreview.php b/lib/public/ipreview.php index f74472ad36..cc756ef80d 100644 --- a/lib/public/ipreview.php +++ b/lib/public/ipreview.php @@ -57,4 +57,11 @@ interface IPreview */ function isMimeSupported($mimeType = '*'); + /** + * Check if a preview can be generated for a file + * + * @param \OCP\Files\FileInfo $file + * @return bool + */ + function isAvailable($file); }