PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/private/previewmanager.php

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