PreviewManager.php 12 KB

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