nextcloud/lib/private/PreviewManager.php

422 lines
12 KiB
PHP
Raw Normal View History

2013-09-05 01:45:11 +04:00
<?php
/**
2016-07-21 18:07:57 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <coding@schilljs.com>
2015-03-26 13:44:34 +03:00
* @author Morris Jobke <hey@morrisjobke.de>
2015-06-25 12:43:55 +03:00
* @author Olivier Paroz <github@oparoz.com>
2016-07-21 19:13:36 +03:00
* @author Robin Appelman <robin@icewind.nl>
2015-03-26 13:44:34 +03:00
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
2013-09-05 01:45:11 +04:00
*
*/
namespace OC;
use OC\Preview\Generator;
use OC\Preview\GeneratorHelper;
use OCP\Files\File;
use OCP\Files\IAppData;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
2013-09-05 01:45:11 +04:00
use OCP\IPreview;
use OCP\Preview\IProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2013-09-05 01:45:11 +04:00
class PreviewManager implements IPreview {
/** @var IConfig */
2015-03-12 14:08:31 +03:00
protected $config;
/** @var IRootFolder */
protected $rootFolder;
/** @var IAppData */
protected $appData;
/** @var EventDispatcherInterface */
protected $eventDispatcher;
/** @var Generator */
private $generator;
/** @var bool */
2015-03-13 11:55:13 +03:00
protected $providerListDirty = false;
/** @var bool */
protected $registeredCoreProviders = false;
/** @var array */
protected $providers = [];
2015-03-13 11:39:16 +03:00
/** @var array mime type => support status */
protected $mimeTypeSupportMap = [];
2015-03-12 13:57:56 +03:00
/** @var array */
protected $defaultProviders;
/** @var string */
protected $userId;
/**
* PreviewManager constructor.
2015-03-12 14:08:31 +03:00
*
* @param IConfig $config
* @param IRootFolder $rootFolder
* @param IAppData $appData
* @param EventDispatcherInterface $eventDispatcher
* @param string $userId
*/
public function __construct(IConfig $config,
IRootFolder $rootFolder,
IAppData $appData,
EventDispatcherInterface $eventDispatcher,
$userId) {
2015-03-12 14:08:31 +03:00
$this->config = $config;
$this->rootFolder = $rootFolder;
$this->appData = $appData;
$this->eventDispatcher = $eventDispatcher;
$this->userId = $userId;
}
/**
* 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 \OCP\Preview\IProvider
*
* @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) {
2015-03-12 14:08:31 +03:00
if (!$this->config->getSystemValue('enable_previews', true)) {
return;
}
if (!isset($this->providers[$mimeTypeRegex])) {
$this->providers[$mimeTypeRegex] = [];
}
$this->providers[$mimeTypeRegex][] = $callable;
2015-03-13 11:55:13 +03:00
$this->providerListDirty = true;
}
/**
* Get all providers
* @return array
*/
public function getProviders() {
2015-03-13 11:55:13 +03:00
if (!$this->config->getSystemValue('enable_previews', true)) {
return [];
}
$this->registerCoreProviders();
2015-03-13 11:55:13 +03:00
if ($this->providerListDirty) {
$keys = array_map('strlen', array_keys($this->providers));
array_multisort($keys, SORT_DESC, $this->providers);
$this->providerListDirty = false;
}
return $this->providers;
}
/**
* Does the manager have any providers
* @return bool
*/
public function hasProviders() {
$this->registerCoreProviders();
return !empty($this->providers);
}
2013-09-05 01:45:11 +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\IImage
* @deprecated 11 Use getPreview
2013-09-05 01:45:11 +04:00
*/
2015-03-12 13:57:56 +03:00
public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) {
try {
$userRoot = $this->rootFolder->getUserFolder($this->userId)->getParent();
$node = $userRoot->get($file);
if (!($file instanceof File)) {
throw new NotFoundException();
}
$preview = $this->getPreview($node, $maxX, $maxY);
} catch (\Exception $e) {
return new \OC_Image();
}
return new \OC_Image($preview->getContent());
2013-09-05 01:45:11 +04:00
}
/**
* Returns a preview of a file
*
* The cache is searched first and if nothing usable was found then a preview is
* generated by one of the providers
*
* @param File $file
* @param int $width
* @param int $height
* @param bool $crop
* @param string $mode
* @param string $mimeType
* @return ISimpleFile
* @throws NotFoundException
* @since 11.0.0
*/
public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
if ($this->generator === null) {
$this->generator = new Generator(
$this->config,
$this,
$this->appData,
new GeneratorHelper(
$this->rootFolder
),
$this->eventDispatcher
);
}
return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType);
}
2013-09-05 01:45:11 +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
*/
2015-03-12 13:57:56 +03:00
public function isMimeSupported($mimeType = '*') {
2015-03-12 14:08:31 +03:00
if (!$this->config->getSystemValue('enable_previews', true)) {
return false;
}
2015-03-13 11:39:16 +03:00
if (isset($this->mimeTypeSupportMap[$mimeType])) {
return $this->mimeTypeSupportMap[$mimeType];
}
$this->registerCoreProviders();
$providerMimeTypes = array_keys($this->providers);
foreach ($providerMimeTypes as $supportedMimeType) {
if (preg_match($supportedMimeType, $mimeType)) {
2015-03-13 11:39:16 +03:00
$this->mimeTypeSupportMap[$mimeType] = true;
return true;
}
}
2015-03-13 11:39:16 +03:00
$this->mimeTypeSupportMap[$mimeType] = false;
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
*
* @param \OCP\Files\FileInfo $file
2014-07-30 18:29:18 +04:00
* @return bool
*/
2015-03-12 13:57:56 +03:00
public function isAvailable(\OCP\Files\FileInfo $file) {
2015-03-12 14:08:31 +03:00
if (!$this->config->getSystemValue('enable_previews', true)) {
return false;
}
$this->registerCoreProviders();
2015-03-13 11:39:16 +03:00
if (!$this->isMimeSupported($file->getMimetype())) {
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 $closure) {
$provider = $closure();
if (!($provider instanceof IProvider)) {
continue;
}
/** @var $provider IProvider */
if ($provider->isAvailable($file)) {
return true;
}
}
}
}
return false;
2014-07-30 18:29:18 +04:00
}
/**
* List of enabled default providers
*
* The following providers are enabled by default:
* - OC\Preview\PNG
* - OC\Preview\JPEG
* - OC\Preview\GIF
* - OC\Preview\BMP
* - OC\Preview\XBitmap
2015-03-13 11:51:42 +03:00
* - OC\Preview\MarkDown
* - OC\Preview\MP3
* - OC\Preview\TXT
*
* The following providers are disabled by default due to performance or privacy concerns:
2015-03-13 11:51:42 +03:00
* - OC\Preview\Font
* - OC\Preview\Illustrator
* - OC\Preview\Movie
* - OC\Preview\MSOfficeDoc
* - OC\Preview\MSOffice2003
* - OC\Preview\MSOffice2007
* - OC\Preview\OpenDocument
2015-03-13 11:51:42 +03:00
* - OC\Preview\PDF
* - OC\Preview\Photoshop
* - OC\Preview\Postscript
* - OC\Preview\StarOffice
* - OC\Preview\SVG
* - OC\Preview\TIFF
*
* @return array
*/
protected function getEnabledDefaultProvider() {
if ($this->defaultProviders !== null) {
return $this->defaultProviders;
}
$imageProviders = [
'OC\Preview\PNG',
'OC\Preview\JPEG',
'OC\Preview\GIF',
'OC\Preview\BMP',
'OC\Preview\XBitmap'
];
$this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([
2015-03-13 11:51:42 +03:00
'OC\Preview\MarkDown',
'OC\Preview\MP3',
'OC\Preview\TXT',
], $imageProviders));
if (in_array('OC\Preview\Image', $this->defaultProviders)) {
$this->defaultProviders = array_merge($this->defaultProviders, $imageProviders);
}
$this->defaultProviders = array_unique($this->defaultProviders);
return $this->defaultProviders;
}
/**
* Register the default providers (if enabled)
*
* @param string $class
* @param string $mimeType
*/
2015-03-12 14:08:31 +03:00
protected function registerCoreProvider($class, $mimeType, $options = []) {
if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
2015-03-12 14:08:31 +03:00
$this->registerProvider($mimeType, function () use ($class, $options) {
return new $class($options);
});
}
}
/**
* Register the default providers (if enabled)
*/
protected function registerCoreProviders() {
if ($this->registeredCoreProviders) {
return;
}
$this->registeredCoreProviders = true;
$this->registerCoreProvider('OC\Preview\TXT', '/text\/plain/');
$this->registerCoreProvider('OC\Preview\MarkDown', '/text\/(x-)?markdown/');
$this->registerCoreProvider('OC\Preview\PNG', '/image\/png/');
$this->registerCoreProvider('OC\Preview\JPEG', '/image\/jpeg/');
$this->registerCoreProvider('OC\Preview\GIF', '/image\/gif/');
$this->registerCoreProvider('OC\Preview\BMP', '/image\/bmp/');
$this->registerCoreProvider('OC\Preview\XBitmap', '/image\/x-xbitmap/');
$this->registerCoreProvider('OC\Preview\MP3', '/audio\/mpeg/');
// SVG, Office and Bitmap require imagick
if (extension_loaded('imagick')) {
$checkImagick = new \Imagick();
$imagickProviders = [
'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'],
'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'],
'PDF' => ['mimetype' => '/application\/pdf/', 'class' => '\OC\Preview\PDF'],
'AI' => ['mimetype' => '/application\/illustrator/', 'class' => '\OC\Preview\Illustrator'],
'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => '\OC\Preview\Photoshop'],
'EPS' => ['mimetype' => '/application\/postscript/', 'class' => '\OC\Preview\Postscript'],
'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => '\OC\Preview\Font'],
];
foreach ($imagickProviders as $queryFormat => $provider) {
$class = $provider['class'];
if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
continue;
}
if (count($checkImagick->queryFormats($queryFormat)) === 1) {
$this->registerCoreProvider($class, $provider['mimetype']);
}
}
if (count($checkImagick->queryFormats('PDF')) === 1) {
if (\OC_Helper::is_function_enabled('shell_exec')) {
2015-03-12 14:08:31 +03:00
$officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null));
if (!$officeFound) {
//let's see if there is libreoffice or openoffice on this machine
$whichLibreOffice = shell_exec('command -v libreoffice');
$officeFound = !empty($whichLibreOffice);
if (!$officeFound) {
$whichOpenOffice = shell_exec('command -v openoffice');
$officeFound = !empty($whichOpenOffice);
}
}
if ($officeFound) {
$this->registerCoreProvider('\OC\Preview\MSOfficeDoc', '/application\/msword/');
$this->registerCoreProvider('\OC\Preview\MSOffice2003', '/application\/vnd.ms-.*/');
$this->registerCoreProvider('\OC\Preview\MSOffice2007', '/application\/vnd.openxmlformats-officedocument.*/');
$this->registerCoreProvider('\OC\Preview\OpenDocument', '/application\/vnd.oasis.opendocument.*/');
$this->registerCoreProvider('\OC\Preview\StarOffice', '/application\/vnd.sun.xml.*/');
}
}
}
}
// Video requires avconv or ffmpeg
if (in_array('OC\Preview\Movie', $this->getEnabledDefaultProvider())) {
$avconvBinary = \OC_Helper::findBinaryPath('avconv');
$ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg');
if ($avconvBinary || $ffmpegBinary) {
// FIXME // a bit hacky but didn't want to use subclasses
\OC\Preview\Movie::$avconvBinary = $avconvBinary;
\OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
$this->registerCoreProvider('\OC\Preview\Movie', '/video\/.*/');
}
}
}
2013-09-05 01:45:11 +04:00
}