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