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.

393 lines
12 KiB

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