@@ -28,436 +28,436 @@ |
||
28 | 28 | use function array_key_exists; |
29 | 29 | |
30 | 30 | class PreviewManager implements IPreview { |
31 | - protected IConfig $config; |
|
32 | - protected IRootFolder $rootFolder; |
|
33 | - protected IAppData $appData; |
|
34 | - protected IEventDispatcher $eventDispatcher; |
|
35 | - private ?Generator $generator = null; |
|
36 | - private GeneratorHelper $helper; |
|
37 | - protected bool $providerListDirty = false; |
|
38 | - protected bool $registeredCoreProviders = false; |
|
39 | - protected array $providers = []; |
|
40 | - |
|
41 | - /** @var array mime type => support status */ |
|
42 | - protected array $mimeTypeSupportMap = []; |
|
43 | - protected ?array $defaultProviders = null; |
|
44 | - protected ?string $userId; |
|
45 | - private Coordinator $bootstrapCoordinator; |
|
46 | - |
|
47 | - /** |
|
48 | - * Hash map (without value) of loaded bootstrap providers |
|
49 | - * @psalm-var array<string, null> |
|
50 | - */ |
|
51 | - private array $loadedBootstrapProviders = []; |
|
52 | - private ContainerInterface $container; |
|
53 | - private IBinaryFinder $binaryFinder; |
|
54 | - private IMagickSupport $imagickSupport; |
|
55 | - private bool $enablePreviews; |
|
56 | - |
|
57 | - public function __construct( |
|
58 | - IConfig $config, |
|
59 | - IRootFolder $rootFolder, |
|
60 | - IAppData $appData, |
|
61 | - IEventDispatcher $eventDispatcher, |
|
62 | - GeneratorHelper $helper, |
|
63 | - ?string $userId, |
|
64 | - Coordinator $bootstrapCoordinator, |
|
65 | - ContainerInterface $container, |
|
66 | - IBinaryFinder $binaryFinder, |
|
67 | - IMagickSupport $imagickSupport, |
|
68 | - ) { |
|
69 | - $this->config = $config; |
|
70 | - $this->rootFolder = $rootFolder; |
|
71 | - $this->appData = $appData; |
|
72 | - $this->eventDispatcher = $eventDispatcher; |
|
73 | - $this->helper = $helper; |
|
74 | - $this->userId = $userId; |
|
75 | - $this->bootstrapCoordinator = $bootstrapCoordinator; |
|
76 | - $this->container = $container; |
|
77 | - $this->binaryFinder = $binaryFinder; |
|
78 | - $this->imagickSupport = $imagickSupport; |
|
79 | - $this->enablePreviews = $config->getSystemValueBool('enable_previews', true); |
|
80 | - } |
|
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 or \OCP\Preview\IProviderV2 |
|
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): void { |
|
93 | - if (!$this->enablePreviews) { |
|
94 | - return; |
|
95 | - } |
|
96 | - |
|
97 | - if (!isset($this->providers[$mimeTypeRegex])) { |
|
98 | - $this->providers[$mimeTypeRegex] = []; |
|
99 | - } |
|
100 | - $this->providers[$mimeTypeRegex][] = $callable; |
|
101 | - $this->providerListDirty = true; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Get all providers |
|
106 | - */ |
|
107 | - public function getProviders(): array { |
|
108 | - if (!$this->enablePreviews) { |
|
109 | - return []; |
|
110 | - } |
|
111 | - |
|
112 | - $this->registerCoreProviders(); |
|
113 | - $this->registerBootstrapProviders(); |
|
114 | - if ($this->providerListDirty) { |
|
115 | - $keys = array_map('strlen', array_keys($this->providers)); |
|
116 | - array_multisort($keys, SORT_DESC, $this->providers); |
|
117 | - $this->providerListDirty = false; |
|
118 | - } |
|
119 | - |
|
120 | - return $this->providers; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Does the manager have any providers |
|
125 | - */ |
|
126 | - public function hasProviders(): bool { |
|
127 | - $this->registerCoreProviders(); |
|
128 | - return !empty($this->providers); |
|
129 | - } |
|
130 | - |
|
131 | - private function getGenerator(): Generator { |
|
132 | - if ($this->generator === null) { |
|
133 | - $this->generator = new Generator( |
|
134 | - $this->config, |
|
135 | - $this, |
|
136 | - $this->appData, |
|
137 | - new GeneratorHelper( |
|
138 | - $this->rootFolder, |
|
139 | - $this->config |
|
140 | - ), |
|
141 | - $this->eventDispatcher, |
|
142 | - $this->container->get(LoggerInterface::class), |
|
143 | - ); |
|
144 | - } |
|
145 | - return $this->generator; |
|
146 | - } |
|
147 | - |
|
148 | - public function getPreview( |
|
149 | - File $file, |
|
150 | - $width = -1, |
|
151 | - $height = -1, |
|
152 | - $crop = false, |
|
153 | - $mode = IPreview::MODE_FILL, |
|
154 | - $mimeType = null, |
|
155 | - bool $cacheResult = true, |
|
156 | - ): ISimpleFile { |
|
157 | - $this->throwIfPreviewsDisabled($file); |
|
158 | - $previewConcurrency = $this->getGenerator()->getNumConcurrentPreviews('preview_concurrency_all'); |
|
159 | - $sem = Generator::guardWithSemaphore(Generator::SEMAPHORE_ID_ALL, $previewConcurrency); |
|
160 | - try { |
|
161 | - $preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType, $cacheResult); |
|
162 | - } finally { |
|
163 | - Generator::unguardWithSemaphore($sem); |
|
164 | - } |
|
165 | - |
|
166 | - return $preview; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Generates previews of a file |
|
171 | - * |
|
172 | - * @param File $file |
|
173 | - * @param array $specifications |
|
174 | - * @param string $mimeType |
|
175 | - * @return ISimpleFile the last preview that was generated |
|
176 | - * @throws NotFoundException |
|
177 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
178 | - * @since 19.0.0 |
|
179 | - */ |
|
180 | - public function generatePreviews(File $file, array $specifications, $mimeType = null) { |
|
181 | - $this->throwIfPreviewsDisabled($file); |
|
182 | - return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * returns true if the passed mime type is supported |
|
187 | - * |
|
188 | - * @param string $mimeType |
|
189 | - * @return boolean |
|
190 | - */ |
|
191 | - public function isMimeSupported($mimeType = '*') { |
|
192 | - if (!$this->enablePreviews) { |
|
193 | - return false; |
|
194 | - } |
|
195 | - |
|
196 | - if (isset($this->mimeTypeSupportMap[$mimeType])) { |
|
197 | - return $this->mimeTypeSupportMap[$mimeType]; |
|
198 | - } |
|
199 | - |
|
200 | - $this->registerCoreProviders(); |
|
201 | - $this->registerBootstrapProviders(); |
|
202 | - $providerMimeTypes = array_keys($this->providers); |
|
203 | - foreach ($providerMimeTypes as $supportedMimeType) { |
|
204 | - if (preg_match($supportedMimeType, $mimeType)) { |
|
205 | - $this->mimeTypeSupportMap[$mimeType] = true; |
|
206 | - return true; |
|
207 | - } |
|
208 | - } |
|
209 | - $this->mimeTypeSupportMap[$mimeType] = false; |
|
210 | - return false; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Check if a preview can be generated for a file |
|
215 | - */ |
|
216 | - public function isAvailable(\OCP\Files\FileInfo $file): bool { |
|
217 | - if (!$this->enablePreviews) { |
|
218 | - return false; |
|
219 | - } |
|
220 | - |
|
221 | - $this->registerCoreProviders(); |
|
222 | - if (!$this->isMimeSupported($file->getMimetype())) { |
|
223 | - return false; |
|
224 | - } |
|
225 | - |
|
226 | - $mount = $file->getMountPoint(); |
|
227 | - if ($mount and !$mount->getOption('previews', true)) { |
|
228 | - return false; |
|
229 | - } |
|
230 | - |
|
231 | - foreach ($this->providers as $supportedMimeType => $providers) { |
|
232 | - if (preg_match($supportedMimeType, $file->getMimetype())) { |
|
233 | - foreach ($providers as $providerClosure) { |
|
234 | - $provider = $this->helper->getProvider($providerClosure); |
|
235 | - if (!($provider instanceof IProviderV2)) { |
|
236 | - continue; |
|
237 | - } |
|
238 | - |
|
239 | - if ($provider->isAvailable($file)) { |
|
240 | - return true; |
|
241 | - } |
|
242 | - } |
|
243 | - } |
|
244 | - } |
|
245 | - return false; |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * List of enabled default providers |
|
250 | - * |
|
251 | - * The following providers are enabled by default: |
|
252 | - * - OC\Preview\PNG |
|
253 | - * - OC\Preview\JPEG |
|
254 | - * - OC\Preview\GIF |
|
255 | - * - OC\Preview\BMP |
|
256 | - * - OC\Preview\XBitmap |
|
257 | - * - OC\Preview\MarkDown |
|
258 | - * - OC\Preview\MP3 |
|
259 | - * - OC\Preview\TXT |
|
260 | - * |
|
261 | - * The following providers are disabled by default due to performance or privacy concerns: |
|
262 | - * - OC\Preview\Font |
|
263 | - * - OC\Preview\HEIC |
|
264 | - * - OC\Preview\Illustrator |
|
265 | - * - OC\Preview\Movie |
|
266 | - * - OC\Preview\MSOfficeDoc |
|
267 | - * - OC\Preview\MSOffice2003 |
|
268 | - * - OC\Preview\MSOffice2007 |
|
269 | - * - OC\Preview\OpenDocument |
|
270 | - * - OC\Preview\PDF |
|
271 | - * - OC\Preview\Photoshop |
|
272 | - * - OC\Preview\Postscript |
|
273 | - * - OC\Preview\StarOffice |
|
274 | - * - OC\Preview\SVG |
|
275 | - * - OC\Preview\TIFF |
|
276 | - * |
|
277 | - * @return array |
|
278 | - */ |
|
279 | - protected function getEnabledDefaultProvider() { |
|
280 | - if ($this->defaultProviders !== null) { |
|
281 | - return $this->defaultProviders; |
|
282 | - } |
|
283 | - |
|
284 | - $imageProviders = [ |
|
285 | - Preview\PNG::class, |
|
286 | - Preview\JPEG::class, |
|
287 | - Preview\GIF::class, |
|
288 | - Preview\BMP::class, |
|
289 | - Preview\XBitmap::class, |
|
290 | - Preview\Krita::class, |
|
291 | - Preview\WebP::class, |
|
292 | - ]; |
|
293 | - |
|
294 | - $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
295 | - Preview\MarkDown::class, |
|
296 | - Preview\MP3::class, |
|
297 | - Preview\TXT::class, |
|
298 | - Preview\OpenDocument::class, |
|
299 | - ], $imageProviders)); |
|
300 | - |
|
301 | - if (in_array(Preview\Image::class, $this->defaultProviders)) { |
|
302 | - $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
303 | - } |
|
304 | - $this->defaultProviders = array_unique($this->defaultProviders); |
|
305 | - return $this->defaultProviders; |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Register the default providers (if enabled) |
|
310 | - * |
|
311 | - * @param string $class |
|
312 | - * @param string $mimeType |
|
313 | - */ |
|
314 | - protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
315 | - if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
316 | - $this->registerProvider($mimeType, function () use ($class, $options) { |
|
317 | - return new $class($options); |
|
318 | - }); |
|
319 | - } |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Register the default providers (if enabled) |
|
324 | - */ |
|
325 | - protected function registerCoreProviders() { |
|
326 | - if ($this->registeredCoreProviders) { |
|
327 | - return; |
|
328 | - } |
|
329 | - $this->registeredCoreProviders = true; |
|
330 | - |
|
331 | - $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/'); |
|
332 | - $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/'); |
|
333 | - $this->registerCoreProvider(Preview\PNG::class, '/image\/png/'); |
|
334 | - $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/'); |
|
335 | - $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/'); |
|
336 | - $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/'); |
|
337 | - $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); |
|
338 | - $this->registerCoreProvider(Preview\WebP::class, '/image\/webp/'); |
|
339 | - $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); |
|
340 | - $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg$/'); |
|
341 | - $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
342 | - $this->registerCoreProvider(Preview\Imaginary::class, Preview\Imaginary::supportedMimeTypes()); |
|
343 | - $this->registerCoreProvider(Preview\ImaginaryPDF::class, Preview\ImaginaryPDF::supportedMimeTypes()); |
|
344 | - |
|
345 | - // SVG and Bitmap require imagick |
|
346 | - if ($this->imagickSupport->hasExtension()) { |
|
347 | - $imagickProviders = [ |
|
348 | - 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class], |
|
349 | - 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class], |
|
350 | - 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class], |
|
351 | - 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class], |
|
352 | - 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class], |
|
353 | - 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class], |
|
354 | - 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class], |
|
355 | - 'HEIC' => ['mimetype' => '/image\/(x-)?hei(f|c)/', 'class' => Preview\HEIC::class], |
|
356 | - 'TGA' => ['mimetype' => '/image\/(x-)?t(ar)?ga/', 'class' => Preview\TGA::class], |
|
357 | - 'SGI' => ['mimetype' => '/image\/(x-)?sgi/', 'class' => Preview\SGI::class], |
|
358 | - ]; |
|
359 | - |
|
360 | - foreach ($imagickProviders as $queryFormat => $provider) { |
|
361 | - $class = $provider['class']; |
|
362 | - if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
363 | - continue; |
|
364 | - } |
|
365 | - |
|
366 | - if ($this->imagickSupport->supportsFormat($queryFormat)) { |
|
367 | - $this->registerCoreProvider($class, $provider['mimetype']); |
|
368 | - } |
|
369 | - } |
|
370 | - } |
|
371 | - |
|
372 | - $this->registerCoreProvidersOffice(); |
|
373 | - |
|
374 | - // Video requires avconv or ffmpeg |
|
375 | - if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) { |
|
376 | - $movieBinary = $this->config->getSystemValue('preview_ffmpeg_path', null); |
|
377 | - if (!is_string($movieBinary)) { |
|
378 | - $movieBinary = $this->binaryFinder->findBinaryPath('avconv'); |
|
379 | - if (!is_string($movieBinary)) { |
|
380 | - $movieBinary = $this->binaryFinder->findBinaryPath('ffmpeg'); |
|
381 | - } |
|
382 | - } |
|
383 | - |
|
384 | - |
|
385 | - if (is_string($movieBinary)) { |
|
386 | - $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/', ['movieBinary' => $movieBinary]); |
|
387 | - } |
|
388 | - } |
|
389 | - } |
|
390 | - |
|
391 | - private function registerCoreProvidersOffice(): void { |
|
392 | - $officeProviders = [ |
|
393 | - ['mimetype' => '/application\/msword/', 'class' => Preview\MSOfficeDoc::class], |
|
394 | - ['mimetype' => '/application\/vnd.ms-.*/', 'class' => Preview\MSOffice2003::class], |
|
395 | - ['mimetype' => '/application\/vnd.openxmlformats-officedocument.*/', 'class' => Preview\MSOffice2007::class], |
|
396 | - ['mimetype' => '/application\/vnd.oasis.opendocument.*/', 'class' => Preview\OpenDocument::class], |
|
397 | - ['mimetype' => '/application\/vnd.sun.xml.*/', 'class' => Preview\StarOffice::class], |
|
398 | - ['mimetype' => '/image\/emf/', 'class' => Preview\EMF::class], |
|
399 | - ]; |
|
400 | - |
|
401 | - $findBinary = true; |
|
402 | - $officeBinary = false; |
|
403 | - |
|
404 | - foreach ($officeProviders as $provider) { |
|
405 | - $class = $provider['class']; |
|
406 | - if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
407 | - continue; |
|
408 | - } |
|
409 | - |
|
410 | - if ($findBinary) { |
|
411 | - // Office requires openoffice or libreoffice |
|
412 | - $officeBinary = $this->config->getSystemValue('preview_libreoffice_path', false); |
|
413 | - if ($officeBinary === false) { |
|
414 | - $officeBinary = $this->binaryFinder->findBinaryPath('libreoffice'); |
|
415 | - } |
|
416 | - if ($officeBinary === false) { |
|
417 | - $officeBinary = $this->binaryFinder->findBinaryPath('openoffice'); |
|
418 | - } |
|
419 | - $findBinary = false; |
|
420 | - } |
|
421 | - |
|
422 | - if ($officeBinary) { |
|
423 | - $this->registerCoreProvider($class, $provider['mimetype'], ['officeBinary' => $officeBinary]); |
|
424 | - } |
|
425 | - } |
|
426 | - } |
|
427 | - |
|
428 | - private function registerBootstrapProviders(): void { |
|
429 | - $context = $this->bootstrapCoordinator->getRegistrationContext(); |
|
430 | - |
|
431 | - if ($context === null) { |
|
432 | - // Just ignore for now |
|
433 | - return; |
|
434 | - } |
|
435 | - |
|
436 | - $providers = $context->getPreviewProviders(); |
|
437 | - foreach ($providers as $provider) { |
|
438 | - $key = $provider->getMimeTypeRegex() . '-' . $provider->getService(); |
|
439 | - if (array_key_exists($key, $this->loadedBootstrapProviders)) { |
|
440 | - // Do not load the provider more than once |
|
441 | - continue; |
|
442 | - } |
|
443 | - $this->loadedBootstrapProviders[$key] = null; |
|
444 | - |
|
445 | - $this->registerProvider($provider->getMimeTypeRegex(), function () use ($provider) { |
|
446 | - try { |
|
447 | - return $this->container->get($provider->getService()); |
|
448 | - } catch (QueryException $e) { |
|
449 | - return null; |
|
450 | - } |
|
451 | - }); |
|
452 | - } |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * @throws NotFoundException if preview generation is disabled |
|
457 | - */ |
|
458 | - private function throwIfPreviewsDisabled(File $file): void { |
|
459 | - if (!$this->isAvailable($file)) { |
|
460 | - throw new NotFoundException('Previews disabled'); |
|
461 | - } |
|
462 | - } |
|
31 | + protected IConfig $config; |
|
32 | + protected IRootFolder $rootFolder; |
|
33 | + protected IAppData $appData; |
|
34 | + protected IEventDispatcher $eventDispatcher; |
|
35 | + private ?Generator $generator = null; |
|
36 | + private GeneratorHelper $helper; |
|
37 | + protected bool $providerListDirty = false; |
|
38 | + protected bool $registeredCoreProviders = false; |
|
39 | + protected array $providers = []; |
|
40 | + |
|
41 | + /** @var array mime type => support status */ |
|
42 | + protected array $mimeTypeSupportMap = []; |
|
43 | + protected ?array $defaultProviders = null; |
|
44 | + protected ?string $userId; |
|
45 | + private Coordinator $bootstrapCoordinator; |
|
46 | + |
|
47 | + /** |
|
48 | + * Hash map (without value) of loaded bootstrap providers |
|
49 | + * @psalm-var array<string, null> |
|
50 | + */ |
|
51 | + private array $loadedBootstrapProviders = []; |
|
52 | + private ContainerInterface $container; |
|
53 | + private IBinaryFinder $binaryFinder; |
|
54 | + private IMagickSupport $imagickSupport; |
|
55 | + private bool $enablePreviews; |
|
56 | + |
|
57 | + public function __construct( |
|
58 | + IConfig $config, |
|
59 | + IRootFolder $rootFolder, |
|
60 | + IAppData $appData, |
|
61 | + IEventDispatcher $eventDispatcher, |
|
62 | + GeneratorHelper $helper, |
|
63 | + ?string $userId, |
|
64 | + Coordinator $bootstrapCoordinator, |
|
65 | + ContainerInterface $container, |
|
66 | + IBinaryFinder $binaryFinder, |
|
67 | + IMagickSupport $imagickSupport, |
|
68 | + ) { |
|
69 | + $this->config = $config; |
|
70 | + $this->rootFolder = $rootFolder; |
|
71 | + $this->appData = $appData; |
|
72 | + $this->eventDispatcher = $eventDispatcher; |
|
73 | + $this->helper = $helper; |
|
74 | + $this->userId = $userId; |
|
75 | + $this->bootstrapCoordinator = $bootstrapCoordinator; |
|
76 | + $this->container = $container; |
|
77 | + $this->binaryFinder = $binaryFinder; |
|
78 | + $this->imagickSupport = $imagickSupport; |
|
79 | + $this->enablePreviews = $config->getSystemValueBool('enable_previews', true); |
|
80 | + } |
|
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 or \OCP\Preview\IProviderV2 |
|
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): void { |
|
93 | + if (!$this->enablePreviews) { |
|
94 | + return; |
|
95 | + } |
|
96 | + |
|
97 | + if (!isset($this->providers[$mimeTypeRegex])) { |
|
98 | + $this->providers[$mimeTypeRegex] = []; |
|
99 | + } |
|
100 | + $this->providers[$mimeTypeRegex][] = $callable; |
|
101 | + $this->providerListDirty = true; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Get all providers |
|
106 | + */ |
|
107 | + public function getProviders(): array { |
|
108 | + if (!$this->enablePreviews) { |
|
109 | + return []; |
|
110 | + } |
|
111 | + |
|
112 | + $this->registerCoreProviders(); |
|
113 | + $this->registerBootstrapProviders(); |
|
114 | + if ($this->providerListDirty) { |
|
115 | + $keys = array_map('strlen', array_keys($this->providers)); |
|
116 | + array_multisort($keys, SORT_DESC, $this->providers); |
|
117 | + $this->providerListDirty = false; |
|
118 | + } |
|
119 | + |
|
120 | + return $this->providers; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Does the manager have any providers |
|
125 | + */ |
|
126 | + public function hasProviders(): bool { |
|
127 | + $this->registerCoreProviders(); |
|
128 | + return !empty($this->providers); |
|
129 | + } |
|
130 | + |
|
131 | + private function getGenerator(): Generator { |
|
132 | + if ($this->generator === null) { |
|
133 | + $this->generator = new Generator( |
|
134 | + $this->config, |
|
135 | + $this, |
|
136 | + $this->appData, |
|
137 | + new GeneratorHelper( |
|
138 | + $this->rootFolder, |
|
139 | + $this->config |
|
140 | + ), |
|
141 | + $this->eventDispatcher, |
|
142 | + $this->container->get(LoggerInterface::class), |
|
143 | + ); |
|
144 | + } |
|
145 | + return $this->generator; |
|
146 | + } |
|
147 | + |
|
148 | + public function getPreview( |
|
149 | + File $file, |
|
150 | + $width = -1, |
|
151 | + $height = -1, |
|
152 | + $crop = false, |
|
153 | + $mode = IPreview::MODE_FILL, |
|
154 | + $mimeType = null, |
|
155 | + bool $cacheResult = true, |
|
156 | + ): ISimpleFile { |
|
157 | + $this->throwIfPreviewsDisabled($file); |
|
158 | + $previewConcurrency = $this->getGenerator()->getNumConcurrentPreviews('preview_concurrency_all'); |
|
159 | + $sem = Generator::guardWithSemaphore(Generator::SEMAPHORE_ID_ALL, $previewConcurrency); |
|
160 | + try { |
|
161 | + $preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType, $cacheResult); |
|
162 | + } finally { |
|
163 | + Generator::unguardWithSemaphore($sem); |
|
164 | + } |
|
165 | + |
|
166 | + return $preview; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Generates previews of a file |
|
171 | + * |
|
172 | + * @param File $file |
|
173 | + * @param array $specifications |
|
174 | + * @param string $mimeType |
|
175 | + * @return ISimpleFile the last preview that was generated |
|
176 | + * @throws NotFoundException |
|
177 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
178 | + * @since 19.0.0 |
|
179 | + */ |
|
180 | + public function generatePreviews(File $file, array $specifications, $mimeType = null) { |
|
181 | + $this->throwIfPreviewsDisabled($file); |
|
182 | + return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * returns true if the passed mime type is supported |
|
187 | + * |
|
188 | + * @param string $mimeType |
|
189 | + * @return boolean |
|
190 | + */ |
|
191 | + public function isMimeSupported($mimeType = '*') { |
|
192 | + if (!$this->enablePreviews) { |
|
193 | + return false; |
|
194 | + } |
|
195 | + |
|
196 | + if (isset($this->mimeTypeSupportMap[$mimeType])) { |
|
197 | + return $this->mimeTypeSupportMap[$mimeType]; |
|
198 | + } |
|
199 | + |
|
200 | + $this->registerCoreProviders(); |
|
201 | + $this->registerBootstrapProviders(); |
|
202 | + $providerMimeTypes = array_keys($this->providers); |
|
203 | + foreach ($providerMimeTypes as $supportedMimeType) { |
|
204 | + if (preg_match($supportedMimeType, $mimeType)) { |
|
205 | + $this->mimeTypeSupportMap[$mimeType] = true; |
|
206 | + return true; |
|
207 | + } |
|
208 | + } |
|
209 | + $this->mimeTypeSupportMap[$mimeType] = false; |
|
210 | + return false; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Check if a preview can be generated for a file |
|
215 | + */ |
|
216 | + public function isAvailable(\OCP\Files\FileInfo $file): bool { |
|
217 | + if (!$this->enablePreviews) { |
|
218 | + return false; |
|
219 | + } |
|
220 | + |
|
221 | + $this->registerCoreProviders(); |
|
222 | + if (!$this->isMimeSupported($file->getMimetype())) { |
|
223 | + return false; |
|
224 | + } |
|
225 | + |
|
226 | + $mount = $file->getMountPoint(); |
|
227 | + if ($mount and !$mount->getOption('previews', true)) { |
|
228 | + return false; |
|
229 | + } |
|
230 | + |
|
231 | + foreach ($this->providers as $supportedMimeType => $providers) { |
|
232 | + if (preg_match($supportedMimeType, $file->getMimetype())) { |
|
233 | + foreach ($providers as $providerClosure) { |
|
234 | + $provider = $this->helper->getProvider($providerClosure); |
|
235 | + if (!($provider instanceof IProviderV2)) { |
|
236 | + continue; |
|
237 | + } |
|
238 | + |
|
239 | + if ($provider->isAvailable($file)) { |
|
240 | + return true; |
|
241 | + } |
|
242 | + } |
|
243 | + } |
|
244 | + } |
|
245 | + return false; |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * List of enabled default providers |
|
250 | + * |
|
251 | + * The following providers are enabled by default: |
|
252 | + * - OC\Preview\PNG |
|
253 | + * - OC\Preview\JPEG |
|
254 | + * - OC\Preview\GIF |
|
255 | + * - OC\Preview\BMP |
|
256 | + * - OC\Preview\XBitmap |
|
257 | + * - OC\Preview\MarkDown |
|
258 | + * - OC\Preview\MP3 |
|
259 | + * - OC\Preview\TXT |
|
260 | + * |
|
261 | + * The following providers are disabled by default due to performance or privacy concerns: |
|
262 | + * - OC\Preview\Font |
|
263 | + * - OC\Preview\HEIC |
|
264 | + * - OC\Preview\Illustrator |
|
265 | + * - OC\Preview\Movie |
|
266 | + * - OC\Preview\MSOfficeDoc |
|
267 | + * - OC\Preview\MSOffice2003 |
|
268 | + * - OC\Preview\MSOffice2007 |
|
269 | + * - OC\Preview\OpenDocument |
|
270 | + * - OC\Preview\PDF |
|
271 | + * - OC\Preview\Photoshop |
|
272 | + * - OC\Preview\Postscript |
|
273 | + * - OC\Preview\StarOffice |
|
274 | + * - OC\Preview\SVG |
|
275 | + * - OC\Preview\TIFF |
|
276 | + * |
|
277 | + * @return array |
|
278 | + */ |
|
279 | + protected function getEnabledDefaultProvider() { |
|
280 | + if ($this->defaultProviders !== null) { |
|
281 | + return $this->defaultProviders; |
|
282 | + } |
|
283 | + |
|
284 | + $imageProviders = [ |
|
285 | + Preview\PNG::class, |
|
286 | + Preview\JPEG::class, |
|
287 | + Preview\GIF::class, |
|
288 | + Preview\BMP::class, |
|
289 | + Preview\XBitmap::class, |
|
290 | + Preview\Krita::class, |
|
291 | + Preview\WebP::class, |
|
292 | + ]; |
|
293 | + |
|
294 | + $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ |
|
295 | + Preview\MarkDown::class, |
|
296 | + Preview\MP3::class, |
|
297 | + Preview\TXT::class, |
|
298 | + Preview\OpenDocument::class, |
|
299 | + ], $imageProviders)); |
|
300 | + |
|
301 | + if (in_array(Preview\Image::class, $this->defaultProviders)) { |
|
302 | + $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); |
|
303 | + } |
|
304 | + $this->defaultProviders = array_unique($this->defaultProviders); |
|
305 | + return $this->defaultProviders; |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Register the default providers (if enabled) |
|
310 | + * |
|
311 | + * @param string $class |
|
312 | + * @param string $mimeType |
|
313 | + */ |
|
314 | + protected function registerCoreProvider($class, $mimeType, $options = []) { |
|
315 | + if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
316 | + $this->registerProvider($mimeType, function () use ($class, $options) { |
|
317 | + return new $class($options); |
|
318 | + }); |
|
319 | + } |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Register the default providers (if enabled) |
|
324 | + */ |
|
325 | + protected function registerCoreProviders() { |
|
326 | + if ($this->registeredCoreProviders) { |
|
327 | + return; |
|
328 | + } |
|
329 | + $this->registeredCoreProviders = true; |
|
330 | + |
|
331 | + $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/'); |
|
332 | + $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/'); |
|
333 | + $this->registerCoreProvider(Preview\PNG::class, '/image\/png/'); |
|
334 | + $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/'); |
|
335 | + $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/'); |
|
336 | + $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/'); |
|
337 | + $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); |
|
338 | + $this->registerCoreProvider(Preview\WebP::class, '/image\/webp/'); |
|
339 | + $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); |
|
340 | + $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg$/'); |
|
341 | + $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); |
|
342 | + $this->registerCoreProvider(Preview\Imaginary::class, Preview\Imaginary::supportedMimeTypes()); |
|
343 | + $this->registerCoreProvider(Preview\ImaginaryPDF::class, Preview\ImaginaryPDF::supportedMimeTypes()); |
|
344 | + |
|
345 | + // SVG and Bitmap require imagick |
|
346 | + if ($this->imagickSupport->hasExtension()) { |
|
347 | + $imagickProviders = [ |
|
348 | + 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class], |
|
349 | + 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class], |
|
350 | + 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class], |
|
351 | + 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class], |
|
352 | + 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class], |
|
353 | + 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class], |
|
354 | + 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class], |
|
355 | + 'HEIC' => ['mimetype' => '/image\/(x-)?hei(f|c)/', 'class' => Preview\HEIC::class], |
|
356 | + 'TGA' => ['mimetype' => '/image\/(x-)?t(ar)?ga/', 'class' => Preview\TGA::class], |
|
357 | + 'SGI' => ['mimetype' => '/image\/(x-)?sgi/', 'class' => Preview\SGI::class], |
|
358 | + ]; |
|
359 | + |
|
360 | + foreach ($imagickProviders as $queryFormat => $provider) { |
|
361 | + $class = $provider['class']; |
|
362 | + if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
363 | + continue; |
|
364 | + } |
|
365 | + |
|
366 | + if ($this->imagickSupport->supportsFormat($queryFormat)) { |
|
367 | + $this->registerCoreProvider($class, $provider['mimetype']); |
|
368 | + } |
|
369 | + } |
|
370 | + } |
|
371 | + |
|
372 | + $this->registerCoreProvidersOffice(); |
|
373 | + |
|
374 | + // Video requires avconv or ffmpeg |
|
375 | + if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) { |
|
376 | + $movieBinary = $this->config->getSystemValue('preview_ffmpeg_path', null); |
|
377 | + if (!is_string($movieBinary)) { |
|
378 | + $movieBinary = $this->binaryFinder->findBinaryPath('avconv'); |
|
379 | + if (!is_string($movieBinary)) { |
|
380 | + $movieBinary = $this->binaryFinder->findBinaryPath('ffmpeg'); |
|
381 | + } |
|
382 | + } |
|
383 | + |
|
384 | + |
|
385 | + if (is_string($movieBinary)) { |
|
386 | + $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/', ['movieBinary' => $movieBinary]); |
|
387 | + } |
|
388 | + } |
|
389 | + } |
|
390 | + |
|
391 | + private function registerCoreProvidersOffice(): void { |
|
392 | + $officeProviders = [ |
|
393 | + ['mimetype' => '/application\/msword/', 'class' => Preview\MSOfficeDoc::class], |
|
394 | + ['mimetype' => '/application\/vnd.ms-.*/', 'class' => Preview\MSOffice2003::class], |
|
395 | + ['mimetype' => '/application\/vnd.openxmlformats-officedocument.*/', 'class' => Preview\MSOffice2007::class], |
|
396 | + ['mimetype' => '/application\/vnd.oasis.opendocument.*/', 'class' => Preview\OpenDocument::class], |
|
397 | + ['mimetype' => '/application\/vnd.sun.xml.*/', 'class' => Preview\StarOffice::class], |
|
398 | + ['mimetype' => '/image\/emf/', 'class' => Preview\EMF::class], |
|
399 | + ]; |
|
400 | + |
|
401 | + $findBinary = true; |
|
402 | + $officeBinary = false; |
|
403 | + |
|
404 | + foreach ($officeProviders as $provider) { |
|
405 | + $class = $provider['class']; |
|
406 | + if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { |
|
407 | + continue; |
|
408 | + } |
|
409 | + |
|
410 | + if ($findBinary) { |
|
411 | + // Office requires openoffice or libreoffice |
|
412 | + $officeBinary = $this->config->getSystemValue('preview_libreoffice_path', false); |
|
413 | + if ($officeBinary === false) { |
|
414 | + $officeBinary = $this->binaryFinder->findBinaryPath('libreoffice'); |
|
415 | + } |
|
416 | + if ($officeBinary === false) { |
|
417 | + $officeBinary = $this->binaryFinder->findBinaryPath('openoffice'); |
|
418 | + } |
|
419 | + $findBinary = false; |
|
420 | + } |
|
421 | + |
|
422 | + if ($officeBinary) { |
|
423 | + $this->registerCoreProvider($class, $provider['mimetype'], ['officeBinary' => $officeBinary]); |
|
424 | + } |
|
425 | + } |
|
426 | + } |
|
427 | + |
|
428 | + private function registerBootstrapProviders(): void { |
|
429 | + $context = $this->bootstrapCoordinator->getRegistrationContext(); |
|
430 | + |
|
431 | + if ($context === null) { |
|
432 | + // Just ignore for now |
|
433 | + return; |
|
434 | + } |
|
435 | + |
|
436 | + $providers = $context->getPreviewProviders(); |
|
437 | + foreach ($providers as $provider) { |
|
438 | + $key = $provider->getMimeTypeRegex() . '-' . $provider->getService(); |
|
439 | + if (array_key_exists($key, $this->loadedBootstrapProviders)) { |
|
440 | + // Do not load the provider more than once |
|
441 | + continue; |
|
442 | + } |
|
443 | + $this->loadedBootstrapProviders[$key] = null; |
|
444 | + |
|
445 | + $this->registerProvider($provider->getMimeTypeRegex(), function () use ($provider) { |
|
446 | + try { |
|
447 | + return $this->container->get($provider->getService()); |
|
448 | + } catch (QueryException $e) { |
|
449 | + return null; |
|
450 | + } |
|
451 | + }); |
|
452 | + } |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * @throws NotFoundException if preview generation is disabled |
|
457 | + */ |
|
458 | + private function throwIfPreviewsDisabled(File $file): void { |
|
459 | + if (!$this->isAvailable($file)) { |
|
460 | + throw new NotFoundException('Previews disabled'); |
|
461 | + } |
|
462 | + } |
|
463 | 463 | } |