previewmanager.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @copyright Copyright (c) 2015, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC;
  25. use OCP\IPreview;
  26. use OCP\Preview\IProvider;
  27. class PreviewManager implements IPreview {
  28. /** @var \OCP\IConfig */
  29. protected $config;
  30. /** @var bool */
  31. protected $providerListDirty = false;
  32. /** @var bool */
  33. protected $registeredCoreProviders = false;
  34. /** @var array */
  35. protected $providers = [];
  36. /** @var array mime type => support status */
  37. protected $mimeTypeSupportMap = [];
  38. /** @var array */
  39. protected $defaultProviders;
  40. /**
  41. * Constructor
  42. *
  43. * @param \OCP\IConfig $config
  44. */
  45. public function __construct(\OCP\IConfig $config) {
  46. $this->config = $config;
  47. }
  48. /**
  49. * In order to improve lazy loading a closure can be registered which will be
  50. * called in case preview providers are actually requested
  51. *
  52. * $callable has to return an instance of \OCP\Preview\IProvider
  53. *
  54. * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider
  55. * @param \Closure $callable
  56. * @return void
  57. */
  58. public function registerProvider($mimeTypeRegex, \Closure $callable) {
  59. if (!$this->config->getSystemValue('enable_previews', true)) {
  60. return;
  61. }
  62. if (!isset($this->providers[$mimeTypeRegex])) {
  63. $this->providers[$mimeTypeRegex] = [];
  64. }
  65. $this->providers[$mimeTypeRegex][] = $callable;
  66. $this->providerListDirty = true;
  67. }
  68. /**
  69. * Get all providers
  70. * @return array
  71. */
  72. public function getProviders() {
  73. if (!$this->config->getSystemValue('enable_previews', true)) {
  74. return [];
  75. }
  76. $this->registerCoreProviders();
  77. if ($this->providerListDirty) {
  78. $keys = array_map('strlen', array_keys($this->providers));
  79. array_multisort($keys, SORT_DESC, $this->providers);
  80. $this->providerListDirty = false;
  81. }
  82. return $this->providers;
  83. }
  84. /**
  85. * Does the manager have any providers
  86. * @return bool
  87. */
  88. public function hasProviders() {
  89. $this->registerCoreProviders();
  90. return !empty($this->providers);
  91. }
  92. /**
  93. * return a preview of a file
  94. *
  95. * @param string $file The path to the file where you want a thumbnail from
  96. * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
  97. * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
  98. * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly
  99. * @return \OCP\IImage
  100. */
  101. public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) {
  102. $preview = new \OC\Preview('', '/', $file, $maxX, $maxY, $scaleUp);
  103. return $preview->getPreview();
  104. }
  105. /**
  106. * returns true if the passed mime type is supported
  107. *
  108. * @param string $mimeType
  109. * @return boolean
  110. */
  111. public function isMimeSupported($mimeType = '*') {
  112. if (!$this->config->getSystemValue('enable_previews', true)) {
  113. return false;
  114. }
  115. if (isset($this->mimeTypeSupportMap[$mimeType])) {
  116. return $this->mimeTypeSupportMap[$mimeType];
  117. }
  118. $this->registerCoreProviders();
  119. $providerMimeTypes = array_keys($this->providers);
  120. foreach ($providerMimeTypes as $supportedMimeType) {
  121. if (preg_match($supportedMimeType, $mimeType)) {
  122. $this->mimeTypeSupportMap[$mimeType] = true;
  123. return true;
  124. }
  125. }
  126. $this->mimeTypeSupportMap[$mimeType] = false;
  127. return false;
  128. }
  129. /**
  130. * Check if a preview can be generated for a file
  131. *
  132. * @param \OCP\Files\FileInfo $file
  133. * @return bool
  134. */
  135. public function isAvailable(\OCP\Files\FileInfo $file) {
  136. if (!$this->config->getSystemValue('enable_previews', true)) {
  137. return false;
  138. }
  139. $this->registerCoreProviders();
  140. if (!$this->isMimeSupported($file->getMimetype())) {
  141. return false;
  142. }
  143. $mount = $file->getMountPoint();
  144. if ($mount and !$mount->getOption('previews', true)){
  145. return false;
  146. }
  147. foreach ($this->providers as $supportedMimeType => $providers) {
  148. if (preg_match($supportedMimeType, $file->getMimetype())) {
  149. foreach ($providers as $closure) {
  150. $provider = $closure();
  151. if (!($provider instanceof IProvider)) {
  152. continue;
  153. }
  154. /** @var $provider IProvider */
  155. if ($provider->isAvailable($file)) {
  156. return true;
  157. }
  158. }
  159. }
  160. }
  161. return false;
  162. }
  163. /**
  164. * List of enabled default providers
  165. *
  166. * The following providers are enabled by default:
  167. * - OC\Preview\PNG
  168. * - OC\Preview\JPEG
  169. * - OC\Preview\GIF
  170. * - OC\Preview\BMP
  171. * - OC\Preview\XBitmap
  172. * - OC\Preview\MarkDown
  173. * - OC\Preview\MP3
  174. * - OC\Preview\TXT
  175. *
  176. * The following providers are disabled by default due to performance or privacy concerns:
  177. * - OC\Preview\Font
  178. * - OC\Preview\Illustrator
  179. * - OC\Preview\Movie
  180. * - OC\Preview\MSOfficeDoc
  181. * - OC\Preview\MSOffice2003
  182. * - OC\Preview\MSOffice2007
  183. * - OC\Preview\OpenDocument
  184. * - OC\Preview\PDF
  185. * - OC\Preview\Photoshop
  186. * - OC\Preview\Postscript
  187. * - OC\Preview\StarOffice
  188. * - OC\Preview\SVG
  189. * - OC\Preview\TIFF
  190. *
  191. * @return array
  192. */
  193. protected function getEnabledDefaultProvider() {
  194. if ($this->defaultProviders !== null) {
  195. return $this->defaultProviders;
  196. }
  197. $imageProviders = [
  198. 'OC\Preview\PNG',
  199. 'OC\Preview\JPEG',
  200. 'OC\Preview\GIF',
  201. 'OC\Preview\BMP',
  202. 'OC\Preview\XBitmap'
  203. ];
  204. $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([
  205. 'OC\Preview\MarkDown',
  206. 'OC\Preview\MP3',
  207. 'OC\Preview\TXT',
  208. ], $imageProviders));
  209. if (in_array('OC\Preview\Image', $this->defaultProviders)) {
  210. $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders);
  211. }
  212. $this->defaultProviders = array_unique($this->defaultProviders);
  213. return $this->defaultProviders;
  214. }
  215. /**
  216. * Register the default providers (if enabled)
  217. *
  218. * @param string $class
  219. * @param string $mimeType
  220. */
  221. protected function registerCoreProvider($class, $mimeType, $options = []) {
  222. if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
  223. $this->registerProvider($mimeType, function () use ($class, $options) {
  224. return new $class($options);
  225. });
  226. }
  227. }
  228. /**
  229. * Register the default providers (if enabled)
  230. */
  231. protected function registerCoreProviders() {
  232. if ($this->registeredCoreProviders) {
  233. return;
  234. }
  235. $this->registeredCoreProviders = true;
  236. $this->registerCoreProvider('OC\Preview\TXT', '/text\/plain/');
  237. $this->registerCoreProvider('OC\Preview\MarkDown', '/text\/(x-)?markdown/');
  238. $this->registerCoreProvider('OC\Preview\PNG', '/image\/png/');
  239. $this->registerCoreProvider('OC\Preview\JPEG', '/image\/jpeg/');
  240. $this->registerCoreProvider('OC\Preview\GIF', '/image\/gif/');
  241. $this->registerCoreProvider('OC\Preview\BMP', '/image\/bmp/');
  242. $this->registerCoreProvider('OC\Preview\XBitmap', '/image\/x-xbitmap/');
  243. $this->registerCoreProvider('OC\Preview\MP3', '/audio\/mpeg/');
  244. // SVG, Office and Bitmap require imagick
  245. if (extension_loaded('imagick')) {
  246. $checkImagick = new \Imagick();
  247. $imagickProviders = [
  248. 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'],
  249. 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'],
  250. 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => '\OC\Preview\PDF'],
  251. 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => '\OC\Preview\Illustrator'],
  252. 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => '\OC\Preview\Photoshop'],
  253. 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => '\OC\Preview\Postscript'],
  254. 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => '\OC\Preview\Font'],
  255. ];
  256. foreach ($imagickProviders as $queryFormat => $provider) {
  257. $class = $provider['class'];
  258. if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
  259. continue;
  260. }
  261. if (count($checkImagick->queryFormats($queryFormat)) === 1) {
  262. $this->registerCoreProvider($class, $provider['mimetype']);
  263. }
  264. }
  265. if (count($checkImagick->queryFormats('PDF')) === 1) {
  266. // Office previews are currently not supported on Windows
  267. if (!\OC_Util::runningOnWindows() && \OC_Helper::is_function_enabled('shell_exec')) {
  268. $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null));
  269. if (!$officeFound) {
  270. //let's see if there is libreoffice or openoffice on this machine
  271. $whichLibreOffice = shell_exec('command -v libreoffice');
  272. $officeFound = !empty($whichLibreOffice);
  273. if (!$officeFound) {
  274. $whichOpenOffice = shell_exec('command -v openoffice');
  275. $officeFound = !empty($whichOpenOffice);
  276. }
  277. }
  278. if ($officeFound) {
  279. $this->registerCoreProvider('\OC\Preview\MSOfficeDoc', '/application\/msword/');
  280. $this->registerCoreProvider('\OC\Preview\MSOffice2003', '/application\/vnd.ms-.*/');
  281. $this->registerCoreProvider('\OC\Preview\MSOffice2007', '/application\/vnd.openxmlformats-officedocument.*/');
  282. $this->registerCoreProvider('\OC\Preview\OpenDocument', '/application\/vnd.oasis.opendocument.*/');
  283. $this->registerCoreProvider('\OC\Preview\StarOffice', '/application\/vnd.sun.xml.*/');
  284. }
  285. }
  286. }
  287. }
  288. // Video requires avconv or ffmpeg and is therefor
  289. // currently not supported on Windows.
  290. if (in_array('OC\Preview\Movie', $this->getEnabledDefaultProvider()) && !\OC_Util::runningOnWindows()) {
  291. $avconvBinary = \OC_Helper::findBinaryPath('avconv');
  292. $ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg');
  293. if ($avconvBinary || $ffmpegBinary) {
  294. // FIXME // a bit hacky but didn't want to use subclasses
  295. \OC\Preview\Movie::$avconvBinary = $avconvBinary;
  296. \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
  297. $this->registerCoreProvider('\OC\Preview\Movie', '/video\/.*/');
  298. }
  299. }
  300. }
  301. }