1
0

PreviewManager.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. $this->config
  177. ),
  178. $this->eventDispatcher
  179. );
  180. }
  181. return $this->generator->getPreview($file, $width, $height, $crop, $mode, $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 $closure) {
  228. $provider = $closure();
  229. if (!($provider instanceof IProvider)) {
  230. continue;
  231. }
  232. /** @var $provider IProvider */
  233. if ($provider->isAvailable($file)) {
  234. return true;
  235. }
  236. }
  237. }
  238. }
  239. return false;
  240. }
  241. /**
  242. * List of enabled default providers
  243. *
  244. * The following providers are enabled by default:
  245. * - OC\Preview\PNG
  246. * - OC\Preview\JPEG
  247. * - OC\Preview\GIF
  248. * - OC\Preview\BMP
  249. * - OC\Preview\HEIC
  250. * - OC\Preview\XBitmap
  251. * - OC\Preview\MarkDown
  252. * - OC\Preview\MP3
  253. * - OC\Preview\TXT
  254. *
  255. * The following providers are disabled by default due to performance or privacy concerns:
  256. * - OC\Preview\Font
  257. * - OC\Preview\Illustrator
  258. * - OC\Preview\Movie
  259. * - OC\Preview\MSOfficeDoc
  260. * - OC\Preview\MSOffice2003
  261. * - OC\Preview\MSOffice2007
  262. * - OC\Preview\OpenDocument
  263. * - OC\Preview\PDF
  264. * - OC\Preview\Photoshop
  265. * - OC\Preview\Postscript
  266. * - OC\Preview\StarOffice
  267. * - OC\Preview\SVG
  268. * - OC\Preview\TIFF
  269. *
  270. * @return array
  271. */
  272. protected function getEnabledDefaultProvider() {
  273. if ($this->defaultProviders !== null) {
  274. return $this->defaultProviders;
  275. }
  276. $imageProviders = [
  277. Preview\PNG::class,
  278. Preview\JPEG::class,
  279. Preview\GIF::class,
  280. Preview\BMP::class,
  281. Preview\HEIC::class,
  282. Preview\XBitmap::class
  283. ];
  284. $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([
  285. Preview\MarkDown::class,
  286. Preview\MP3::class,
  287. Preview\TXT::class,
  288. ], $imageProviders));
  289. if (in_array(Preview\Image::class, $this->defaultProviders)) {
  290. $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders);
  291. }
  292. $this->defaultProviders = array_unique($this->defaultProviders);
  293. return $this->defaultProviders;
  294. }
  295. /**
  296. * Register the default providers (if enabled)
  297. *
  298. * @param string $class
  299. * @param string $mimeType
  300. */
  301. protected function registerCoreProvider($class, $mimeType, $options = []) {
  302. if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
  303. $this->registerProvider($mimeType, function () use ($class, $options) {
  304. return new $class($options);
  305. });
  306. }
  307. }
  308. /**
  309. * Register the default providers (if enabled)
  310. */
  311. protected function registerCoreProviders() {
  312. if ($this->registeredCoreProviders) {
  313. return;
  314. }
  315. $this->registeredCoreProviders = true;
  316. $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/');
  317. $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/');
  318. $this->registerCoreProvider(Preview\PNG::class, '/image\/png/');
  319. $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/');
  320. $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/');
  321. $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/');
  322. $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/');
  323. $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/');
  324. // SVG, Office and Bitmap require imagick
  325. if (extension_loaded('imagick')) {
  326. $checkImagick = new \Imagick();
  327. $imagickProviders = [
  328. 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class],
  329. 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class],
  330. 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class],
  331. 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class],
  332. 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class],
  333. 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class],
  334. 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class],
  335. 'HEIC' => ['mimetype' => '/image\/hei(f|c)/', 'class' => Preview\HEIC::class],
  336. ];
  337. foreach ($imagickProviders as $queryFormat => $provider) {
  338. $class = $provider['class'];
  339. if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
  340. continue;
  341. }
  342. if (count($checkImagick->queryFormats($queryFormat)) === 1) {
  343. $this->registerCoreProvider($class, $provider['mimetype']);
  344. }
  345. }
  346. if (count($checkImagick->queryFormats('PDF')) === 1) {
  347. if (\OC_Helper::is_function_enabled('shell_exec')) {
  348. $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null));
  349. if (!$officeFound) {
  350. //let's see if there is libreoffice or openoffice on this machine
  351. $whichLibreOffice = shell_exec('command -v libreoffice');
  352. $officeFound = !empty($whichLibreOffice);
  353. if (!$officeFound) {
  354. $whichOpenOffice = shell_exec('command -v openoffice');
  355. $officeFound = !empty($whichOpenOffice);
  356. }
  357. }
  358. if ($officeFound) {
  359. $this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/');
  360. $this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/');
  361. $this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/');
  362. $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
  363. $this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/');
  364. }
  365. }
  366. }
  367. }
  368. // Video requires avconv or ffmpeg
  369. if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) {
  370. $avconvBinary = \OC_Helper::findBinaryPath('avconv');
  371. $ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg');
  372. if ($avconvBinary || $ffmpegBinary) {
  373. // FIXME // a bit hacky but didn't want to use subclasses
  374. \OC\Preview\Movie::$avconvBinary = $avconvBinary;
  375. \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
  376. $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/');
  377. }
  378. }
  379. }
  380. }