Register preview providers on the preview manager instead of OC\Preview

This commit is contained in:
Joas Schilling 2015-03-11 16:43:00 +01:00
parent 4c4c0fa120
commit cae8529359
4 changed files with 74 additions and 61 deletions

View File

@ -510,7 +510,7 @@ class Preview {
if (is_null($this->preview)) {
$preview = null;
foreach (self::$providers as $supportedMimeType => $provider) {
foreach (self::getProviders() as $supportedMimeType => $provider) {
if (!preg_match($supportedMimeType, $this->mimeType)) {
continue;
}
@ -757,6 +757,14 @@ class Preview {
array_multisort($keys, SORT_DESC, self::$providers);
}
protected static function getProviders() {
if (empty(self::$providers)) {
self::initProviders();
}
return self::$providers;
}
protected static function registerCoreProviders() {
self::registerProvider('OC\Preview\TXT');
self::registerProvider('OC\Preview\MarkDown');
@ -914,60 +922,6 @@ 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(\OC\Files\FileInfo $file) {
if (!\OC_Config::getValue('enable_previews', true)) {
return false;
}
$mount = $file->getMountPoint();
if ($mount and !$mount->getOption('previews', true)){
return false;
}
//check if there are preview backends
if (empty(self::$providers)) {
self::initProviders();
}
foreach (self::$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
*/
public static function isMimeSupported($mimeType) {
if (!\OC_Config::getValue('enable_previews', true)) {
return false;
}
//check if there are preview backends
if (empty(self::$providers)) {
self::initProviders();
}
foreach(self::$providers as $supportedMimetype => $provider) {
if(preg_match($supportedMimetype, $mimeType)) {
return true;
}
}
return false;
}
/**
* @param int $fileId
* @return string

View File

@ -16,7 +16,7 @@ abstract class Provider {
/**
* Check if a preview can be generated for $path
*
* @param \OC\Files\FileInfo $file
* @param \OCP\Files\FileInfo $file
* @return bool
*/
public function isAvailable($file) {

View File

@ -12,6 +12,26 @@ use OCP\image;
use OCP\IPreview;
class PreviewManager implements IPreview {
/** @var array */
protected $providers = [];
/**
* In order to improve lazy loading a closure can be registered which will be
* called in case preview providers are actually requested
*
* $callable has to return an instance of \OC\Preview\Provider
*
* @param string $mimeTypeRegex Regex with the mime types that are supported by this provider
* @param \Closure $callable
* @return void
*/
public function registerProvider($mimeTypeRegex, \Closure $callable) {
if (!isset($this->providers[$mimeTypeRegex])) {
$this->providers[$mimeTypeRegex] = [];
}
$this->providers[$mimeTypeRegex][] = $callable;
}
/**
* return a preview of a file
*
@ -33,16 +53,45 @@ class PreviewManager implements IPreview {
* @return boolean
*/
function isMimeSupported($mimeType = '*') {
return \OC\Preview::isMimeSupported($mimeType);
if (!\OC::$server->getConfig()->getSystemValue('enable_previews', true)) {
return false;
}
$providerMimeTypes = array_keys($this->providers);
foreach ($providerMimeTypes as $supportedMimeType) {
if (preg_match($supportedMimeType, $mimeType)) {
return true;
}
}
return false;
}
/**
* Check if a preview can be generated for a file
*
* @param \OC\Files\FileInfo $file
* @param \OCP\Files\FileInfo $file
* @return bool
*/
function isAvailable($file) {
return \OC\Preview::isAvailable($file);
if (!\OC::$server->getConfig()->getSystemValue('enable_previews', true)) {
return false;
}
$mount = $file->getMountPoint();
if ($mount and !$mount->getOption('previews', true)){
return false;
}
foreach ($this->providers as $supportedMimeType => $providers) {
if (preg_match($supportedMimeType, $file->getMimetype())) {
foreach ($providers as $provider) {
/** @var $provider \OC\Preview\Provider */
if ($provider->isAvailable($file)) {
return true;
}
}
}
}
return false;
}
}

View File

@ -36,8 +36,18 @@ namespace OCP;
/**
* This class provides functions to render and show thumbnails and previews of files
*/
interface IPreview
{
interface IPreview {
/**
* In order to improve lazy loading a closure can be registered which will be
* called in case preview providers are actually requested
*
* $callable has to return an instance of \OC\Preview\Provider
*
* @param string $mimeTypeRegex Regex with the mime types that are supported by this provider
* @param \Closure $callable
* @return void
*/
public function registerProvider($mimeTypeRegex, \Closure $callable);
/**
* Return a preview of a file