2013-09-05 01:45:11 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-26 13:37:37 +03:00
|
|
|
* Copyright (c) 2013 Thomas Müller thomas.mueller@tmit.eu
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
2013-09-05 01:45:11 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OC;
|
|
|
|
|
|
|
|
use OCP\image;
|
|
|
|
use OCP\IPreview;
|
|
|
|
|
|
|
|
class PreviewManager implements IPreview {
|
2015-03-11 18:43:00 +03:00
|
|
|
/** @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;
|
|
|
|
}
|
|
|
|
|
2013-09-05 01:45:11 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* return a preview of a file
|
2014-07-30 18:29:18 +04:00
|
|
|
*
|
2013-09-05 01:45:11 +04:00
|
|
|
* @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
|
|
|
|
*/
|
2014-07-30 18:29:18 +04:00
|
|
|
function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) {
|
2013-09-05 01:45:11 +04:00
|
|
|
$preview = new \OC\Preview('', '/', $file, $maxX, $maxY, $scaleUp);
|
|
|
|
return $preview->getPreview();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* returns true if the passed mime type is supported
|
2014-07-30 18:29:18 +04:00
|
|
|
*
|
2013-09-05 01:45:11 +04:00
|
|
|
* @param string $mimeType
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-07-30 18:29:18 +04:00
|
|
|
function isMimeSupported($mimeType = '*') {
|
2015-03-11 18:43:00 +03:00
|
|
|
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;
|
2013-09-05 01:45:11 +04:00
|
|
|
}
|
2014-07-30 18:29:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a preview can be generated for a file
|
|
|
|
*
|
2015-03-11 18:43:00 +03:00
|
|
|
* @param \OCP\Files\FileInfo $file
|
2014-07-30 18:29:18 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function isAvailable($file) {
|
2015-03-11 18:43:00 +03:00
|
|
|
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;
|
2014-07-30 18:29:18 +04:00
|
|
|
}
|
2013-09-05 01:45:11 +04:00
|
|
|
}
|