PreviewManager.php 13 KB

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