You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

432 lines
13 KiB

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