PreviewManager.php 12 KB

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