1
0

previewmanager.php 10 KB

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