Completed
Push — master ( e27817...bb2336 )
by Morris
17:59
created
lib/private/PreviewManager.php 1 patch
Indentation   +386 added lines, -386 removed lines patch added patch discarded remove patch
@@ -39,390 +39,390 @@
 block discarded – undo
39 39
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
40 40
 
41 41
 class PreviewManager implements IPreview {
42
-	/** @var IConfig */
43
-	protected $config;
44
-
45
-	/** @var IRootFolder */
46
-	protected $rootFolder;
47
-
48
-	/** @var IAppData */
49
-	protected $appData;
50
-
51
-	/** @var EventDispatcherInterface */
52
-	protected $eventDispatcher;
53
-
54
-	/** @var Generator */
55
-	private $generator;
56
-
57
-	/** @var bool */
58
-	protected $providerListDirty = false;
59
-
60
-	/** @var bool */
61
-	protected $registeredCoreProviders = false;
62
-
63
-	/** @var array */
64
-	protected $providers = [];
65
-
66
-	/** @var array mime type => support status */
67
-	protected $mimeTypeSupportMap = [];
68
-
69
-	/** @var array */
70
-	protected $defaultProviders;
71
-
72
-	/** @var string */
73
-	protected $userId;
74
-
75
-	/**
76
-	 * PreviewManager constructor.
77
-	 *
78
-	 * @param IConfig $config
79
-	 * @param IRootFolder $rootFolder
80
-	 * @param IAppData $appData
81
-	 * @param EventDispatcherInterface $eventDispatcher
82
-	 * @param string $userId
83
-	 */
84
-	public function __construct(IConfig $config,
85
-								IRootFolder $rootFolder,
86
-								IAppData $appData,
87
-								EventDispatcherInterface $eventDispatcher,
88
-								$userId) {
89
-		$this->config = $config;
90
-		$this->rootFolder = $rootFolder;
91
-		$this->appData = $appData;
92
-		$this->eventDispatcher = $eventDispatcher;
93
-		$this->userId = $userId;
94
-	}
95
-
96
-	/**
97
-	 * In order to improve lazy loading a closure can be registered which will be
98
-	 * called in case preview providers are actually requested
99
-	 *
100
-	 * $callable has to return an instance of \OCP\Preview\IProvider
101
-	 *
102
-	 * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider
103
-	 * @param \Closure $callable
104
-	 * @return void
105
-	 */
106
-	public function registerProvider($mimeTypeRegex, \Closure $callable) {
107
-		if (!$this->config->getSystemValue('enable_previews', true)) {
108
-			return;
109
-		}
110
-
111
-		if (!isset($this->providers[$mimeTypeRegex])) {
112
-			$this->providers[$mimeTypeRegex] = [];
113
-		}
114
-		$this->providers[$mimeTypeRegex][] = $callable;
115
-		$this->providerListDirty = true;
116
-	}
117
-
118
-	/**
119
-	 * Get all providers
120
-	 * @return array
121
-	 */
122
-	public function getProviders() {
123
-		if (!$this->config->getSystemValue('enable_previews', true)) {
124
-			return [];
125
-		}
126
-
127
-		$this->registerCoreProviders();
128
-		if ($this->providerListDirty) {
129
-			$keys = array_map('strlen', array_keys($this->providers));
130
-			array_multisort($keys, SORT_DESC, $this->providers);
131
-			$this->providerListDirty = false;
132
-		}
133
-
134
-		return $this->providers;
135
-	}
136
-
137
-	/**
138
-	 * Does the manager have any providers
139
-	 * @return bool
140
-	 */
141
-	public function hasProviders() {
142
-		$this->registerCoreProviders();
143
-		return !empty($this->providers);
144
-	}
145
-
146
-	/**
147
-	 * return a preview of a file
148
-	 *
149
-	 * @param string $file The path to the file where you want a thumbnail from
150
-	 * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
151
-	 * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
152
-	 * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly
153
-	 * @return \OCP\IImage
154
-	 * @deprecated 11 Use getPreview
155
-	 */
156
-	public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) {
157
-		try {
158
-			$userRoot = $this->rootFolder->getUserFolder($this->userId)->getParent();
159
-			$node = $userRoot->get($file);
160
-			if (!($file instanceof File)) {
161
-				throw new NotFoundException();
162
-			}
163
-
164
-			$preview = $this->getPreview($node, $maxX, $maxY);
165
-		} catch (\Exception $e) {
166
-			return new \OC_Image();
167
-		}
168
-
169
-		$previewImage = new \OC_Image();
170
-		$previewImage->loadFromData($preview->getContent());
171
-		return $previewImage;
172
-	}
173
-
174
-	/**
175
-	 * Returns a preview of a file
176
-	 *
177
-	 * The cache is searched first and if nothing usable was found then a preview is
178
-	 * generated by one of the providers
179
-	 *
180
-	 * @param File $file
181
-	 * @param int $width
182
-	 * @param int $height
183
-	 * @param bool $crop
184
-	 * @param string $mode
185
-	 * @param string $mimeType
186
-	 * @return ISimpleFile
187
-	 * @throws NotFoundException
188
-	 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
189
-	 * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0
190
-	 */
191
-	public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
192
-		if ($this->generator === null) {
193
-			$this->generator = new Generator(
194
-				$this->config,
195
-				$this,
196
-				$this->appData,
197
-				new GeneratorHelper(
198
-					$this->rootFolder
199
-				),
200
-				$this->eventDispatcher
201
-			);
202
-		}
203
-
204
-		return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType);
205
-	}
206
-
207
-	/**
208
-	 * returns true if the passed mime type is supported
209
-	 *
210
-	 * @param string $mimeType
211
-	 * @return boolean
212
-	 */
213
-	public function isMimeSupported($mimeType = '*') {
214
-		if (!$this->config->getSystemValue('enable_previews', true)) {
215
-			return false;
216
-		}
217
-
218
-		if (isset($this->mimeTypeSupportMap[$mimeType])) {
219
-			return $this->mimeTypeSupportMap[$mimeType];
220
-		}
221
-
222
-		$this->registerCoreProviders();
223
-		$providerMimeTypes = array_keys($this->providers);
224
-		foreach ($providerMimeTypes as $supportedMimeType) {
225
-			if (preg_match($supportedMimeType, $mimeType)) {
226
-				$this->mimeTypeSupportMap[$mimeType] = true;
227
-				return true;
228
-			}
229
-		}
230
-		$this->mimeTypeSupportMap[$mimeType] = false;
231
-		return false;
232
-	}
233
-
234
-	/**
235
-	 * Check if a preview can be generated for a file
236
-	 *
237
-	 * @param \OCP\Files\FileInfo $file
238
-	 * @return bool
239
-	 */
240
-	public function isAvailable(\OCP\Files\FileInfo $file) {
241
-		if (!$this->config->getSystemValue('enable_previews', true)) {
242
-			return false;
243
-		}
244
-
245
-		$this->registerCoreProviders();
246
-		if (!$this->isMimeSupported($file->getMimetype())) {
247
-			return false;
248
-		}
249
-
250
-		$mount = $file->getMountPoint();
251
-		if ($mount and !$mount->getOption('previews', true)){
252
-			return false;
253
-		}
254
-
255
-		foreach ($this->providers as $supportedMimeType => $providers) {
256
-			if (preg_match($supportedMimeType, $file->getMimetype())) {
257
-				foreach ($providers as $closure) {
258
-					$provider = $closure();
259
-					if (!($provider instanceof IProvider)) {
260
-						continue;
261
-					}
262
-
263
-					/** @var $provider IProvider */
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
-		];
317
-
318
-		$this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([
319
-			Preview\MarkDown::class,
320
-			Preview\MP3::class,
321
-			Preview\TXT::class,
322
-		], $imageProviders));
323
-
324
-		if (in_array(Preview\Image::class, $this->defaultProviders)) {
325
-			$this->defaultProviders = array_merge($this->defaultProviders, $imageProviders);
326
-		}
327
-		$this->defaultProviders = array_unique($this->defaultProviders);
328
-		return $this->defaultProviders;
329
-	}
330
-
331
-	/**
332
-	 * Register the default providers (if enabled)
333
-	 *
334
-	 * @param string $class
335
-	 * @param string $mimeType
336
-	 */
337
-	protected function registerCoreProvider($class, $mimeType, $options = []) {
338
-		if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
339
-			$this->registerProvider($mimeType, function () use ($class, $options) {
340
-				return new $class($options);
341
-			});
342
-		}
343
-	}
344
-
345
-	/**
346
-	 * Register the default providers (if enabled)
347
-	 */
348
-	protected function registerCoreProviders() {
349
-		if ($this->registeredCoreProviders) {
350
-			return;
351
-		}
352
-		$this->registeredCoreProviders = true;
353
-
354
-		$this->registerCoreProvider(Preview\TXT::class, '/text\/plain/');
355
-		$this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/');
356
-		$this->registerCoreProvider(Preview\PNG::class, '/image\/png/');
357
-		$this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/');
358
-		$this->registerCoreProvider(Preview\GIF::class, '/image\/gif/');
359
-		$this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/');
360
-		$this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/');
361
-		$this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/');
362
-
363
-		// SVG, Office and Bitmap require imagick
364
-		if (extension_loaded('imagick')) {
365
-			$checkImagick = new \Imagick();
366
-
367
-			$imagickProviders = [
368
-				'SVG'	=> ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class],
369
-				'TIFF'	=> ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class],
370
-				'PDF'	=> ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class],
371
-				'AI'	=> ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class],
372
-				'PSD'	=> ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class],
373
-				'EPS'	=> ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class],
374
-				'TTF'	=> ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class],
375
-				'HEIC'  => ['mimetype' => '/image\/hei(f|c)/', 'class' => Preview\HEIC::class],
376
-			];
377
-
378
-			foreach ($imagickProviders as $queryFormat => $provider) {
379
-				$class = $provider['class'];
380
-				if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
381
-					continue;
382
-				}
383
-
384
-				if (count($checkImagick->queryFormats($queryFormat)) === 1) {
385
-					$this->registerCoreProvider($class, $provider['mimetype']);
386
-				}
387
-			}
388
-
389
-			if (count($checkImagick->queryFormats('PDF')) === 1) {
390
-				if (\OC_Helper::is_function_enabled('shell_exec')) {
391
-					$officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null));
392
-
393
-					if (!$officeFound) {
394
-						//let's see if there is libreoffice or openoffice on this machine
395
-						$whichLibreOffice = shell_exec('command -v libreoffice');
396
-						$officeFound = !empty($whichLibreOffice);
397
-						if (!$officeFound) {
398
-							$whichOpenOffice = shell_exec('command -v openoffice');
399
-							$officeFound = !empty($whichOpenOffice);
400
-						}
401
-					}
402
-
403
-					if ($officeFound) {
404
-						$this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/');
405
-						$this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/');
406
-						$this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/');
407
-						$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
408
-						$this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/');
409
-					}
410
-				}
411
-			}
412
-		}
413
-
414
-		// Video requires avconv or ffmpeg
415
-		if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) {
416
-			$avconvBinary = \OC_Helper::findBinaryPath('avconv');
417
-			$ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg');
418
-
419
-			if ($avconvBinary || $ffmpegBinary) {
420
-				// FIXME // a bit hacky but didn't want to use subclasses
421
-				\OC\Preview\Movie::$avconvBinary = $avconvBinary;
422
-				\OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
423
-
424
-				$this->registerCoreProvider(Preview\Movie::class, '/video\/.*/');
425
-			}
426
-		}
427
-	}
42
+    /** @var IConfig */
43
+    protected $config;
44
+
45
+    /** @var IRootFolder */
46
+    protected $rootFolder;
47
+
48
+    /** @var IAppData */
49
+    protected $appData;
50
+
51
+    /** @var EventDispatcherInterface */
52
+    protected $eventDispatcher;
53
+
54
+    /** @var Generator */
55
+    private $generator;
56
+
57
+    /** @var bool */
58
+    protected $providerListDirty = false;
59
+
60
+    /** @var bool */
61
+    protected $registeredCoreProviders = false;
62
+
63
+    /** @var array */
64
+    protected $providers = [];
65
+
66
+    /** @var array mime type => support status */
67
+    protected $mimeTypeSupportMap = [];
68
+
69
+    /** @var array */
70
+    protected $defaultProviders;
71
+
72
+    /** @var string */
73
+    protected $userId;
74
+
75
+    /**
76
+     * PreviewManager constructor.
77
+     *
78
+     * @param IConfig $config
79
+     * @param IRootFolder $rootFolder
80
+     * @param IAppData $appData
81
+     * @param EventDispatcherInterface $eventDispatcher
82
+     * @param string $userId
83
+     */
84
+    public function __construct(IConfig $config,
85
+                                IRootFolder $rootFolder,
86
+                                IAppData $appData,
87
+                                EventDispatcherInterface $eventDispatcher,
88
+                                $userId) {
89
+        $this->config = $config;
90
+        $this->rootFolder = $rootFolder;
91
+        $this->appData = $appData;
92
+        $this->eventDispatcher = $eventDispatcher;
93
+        $this->userId = $userId;
94
+    }
95
+
96
+    /**
97
+     * In order to improve lazy loading a closure can be registered which will be
98
+     * called in case preview providers are actually requested
99
+     *
100
+     * $callable has to return an instance of \OCP\Preview\IProvider
101
+     *
102
+     * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider
103
+     * @param \Closure $callable
104
+     * @return void
105
+     */
106
+    public function registerProvider($mimeTypeRegex, \Closure $callable) {
107
+        if (!$this->config->getSystemValue('enable_previews', true)) {
108
+            return;
109
+        }
110
+
111
+        if (!isset($this->providers[$mimeTypeRegex])) {
112
+            $this->providers[$mimeTypeRegex] = [];
113
+        }
114
+        $this->providers[$mimeTypeRegex][] = $callable;
115
+        $this->providerListDirty = true;
116
+    }
117
+
118
+    /**
119
+     * Get all providers
120
+     * @return array
121
+     */
122
+    public function getProviders() {
123
+        if (!$this->config->getSystemValue('enable_previews', true)) {
124
+            return [];
125
+        }
126
+
127
+        $this->registerCoreProviders();
128
+        if ($this->providerListDirty) {
129
+            $keys = array_map('strlen', array_keys($this->providers));
130
+            array_multisort($keys, SORT_DESC, $this->providers);
131
+            $this->providerListDirty = false;
132
+        }
133
+
134
+        return $this->providers;
135
+    }
136
+
137
+    /**
138
+     * Does the manager have any providers
139
+     * @return bool
140
+     */
141
+    public function hasProviders() {
142
+        $this->registerCoreProviders();
143
+        return !empty($this->providers);
144
+    }
145
+
146
+    /**
147
+     * return a preview of a file
148
+     *
149
+     * @param string $file The path to the file where you want a thumbnail from
150
+     * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
151
+     * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
152
+     * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly
153
+     * @return \OCP\IImage
154
+     * @deprecated 11 Use getPreview
155
+     */
156
+    public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) {
157
+        try {
158
+            $userRoot = $this->rootFolder->getUserFolder($this->userId)->getParent();
159
+            $node = $userRoot->get($file);
160
+            if (!($file instanceof File)) {
161
+                throw new NotFoundException();
162
+            }
163
+
164
+            $preview = $this->getPreview($node, $maxX, $maxY);
165
+        } catch (\Exception $e) {
166
+            return new \OC_Image();
167
+        }
168
+
169
+        $previewImage = new \OC_Image();
170
+        $previewImage->loadFromData($preview->getContent());
171
+        return $previewImage;
172
+    }
173
+
174
+    /**
175
+     * Returns a preview of a file
176
+     *
177
+     * The cache is searched first and if nothing usable was found then a preview is
178
+     * generated by one of the providers
179
+     *
180
+     * @param File $file
181
+     * @param int $width
182
+     * @param int $height
183
+     * @param bool $crop
184
+     * @param string $mode
185
+     * @param string $mimeType
186
+     * @return ISimpleFile
187
+     * @throws NotFoundException
188
+     * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
189
+     * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0
190
+     */
191
+    public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
192
+        if ($this->generator === null) {
193
+            $this->generator = new Generator(
194
+                $this->config,
195
+                $this,
196
+                $this->appData,
197
+                new GeneratorHelper(
198
+                    $this->rootFolder
199
+                ),
200
+                $this->eventDispatcher
201
+            );
202
+        }
203
+
204
+        return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType);
205
+    }
206
+
207
+    /**
208
+     * returns true if the passed mime type is supported
209
+     *
210
+     * @param string $mimeType
211
+     * @return boolean
212
+     */
213
+    public function isMimeSupported($mimeType = '*') {
214
+        if (!$this->config->getSystemValue('enable_previews', true)) {
215
+            return false;
216
+        }
217
+
218
+        if (isset($this->mimeTypeSupportMap[$mimeType])) {
219
+            return $this->mimeTypeSupportMap[$mimeType];
220
+        }
221
+
222
+        $this->registerCoreProviders();
223
+        $providerMimeTypes = array_keys($this->providers);
224
+        foreach ($providerMimeTypes as $supportedMimeType) {
225
+            if (preg_match($supportedMimeType, $mimeType)) {
226
+                $this->mimeTypeSupportMap[$mimeType] = true;
227
+                return true;
228
+            }
229
+        }
230
+        $this->mimeTypeSupportMap[$mimeType] = false;
231
+        return false;
232
+    }
233
+
234
+    /**
235
+     * Check if a preview can be generated for a file
236
+     *
237
+     * @param \OCP\Files\FileInfo $file
238
+     * @return bool
239
+     */
240
+    public function isAvailable(\OCP\Files\FileInfo $file) {
241
+        if (!$this->config->getSystemValue('enable_previews', true)) {
242
+            return false;
243
+        }
244
+
245
+        $this->registerCoreProviders();
246
+        if (!$this->isMimeSupported($file->getMimetype())) {
247
+            return false;
248
+        }
249
+
250
+        $mount = $file->getMountPoint();
251
+        if ($mount and !$mount->getOption('previews', true)){
252
+            return false;
253
+        }
254
+
255
+        foreach ($this->providers as $supportedMimeType => $providers) {
256
+            if (preg_match($supportedMimeType, $file->getMimetype())) {
257
+                foreach ($providers as $closure) {
258
+                    $provider = $closure();
259
+                    if (!($provider instanceof IProvider)) {
260
+                        continue;
261
+                    }
262
+
263
+                    /** @var $provider IProvider */
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
+        ];
317
+
318
+        $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([
319
+            Preview\MarkDown::class,
320
+            Preview\MP3::class,
321
+            Preview\TXT::class,
322
+        ], $imageProviders));
323
+
324
+        if (in_array(Preview\Image::class, $this->defaultProviders)) {
325
+            $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders);
326
+        }
327
+        $this->defaultProviders = array_unique($this->defaultProviders);
328
+        return $this->defaultProviders;
329
+    }
330
+
331
+    /**
332
+     * Register the default providers (if enabled)
333
+     *
334
+     * @param string $class
335
+     * @param string $mimeType
336
+     */
337
+    protected function registerCoreProvider($class, $mimeType, $options = []) {
338
+        if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
339
+            $this->registerProvider($mimeType, function () use ($class, $options) {
340
+                return new $class($options);
341
+            });
342
+        }
343
+    }
344
+
345
+    /**
346
+     * Register the default providers (if enabled)
347
+     */
348
+    protected function registerCoreProviders() {
349
+        if ($this->registeredCoreProviders) {
350
+            return;
351
+        }
352
+        $this->registeredCoreProviders = true;
353
+
354
+        $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/');
355
+        $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/');
356
+        $this->registerCoreProvider(Preview\PNG::class, '/image\/png/');
357
+        $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/');
358
+        $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/');
359
+        $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/');
360
+        $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/');
361
+        $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/');
362
+
363
+        // SVG, Office and Bitmap require imagick
364
+        if (extension_loaded('imagick')) {
365
+            $checkImagick = new \Imagick();
366
+
367
+            $imagickProviders = [
368
+                'SVG'	=> ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class],
369
+                'TIFF'	=> ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class],
370
+                'PDF'	=> ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class],
371
+                'AI'	=> ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class],
372
+                'PSD'	=> ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class],
373
+                'EPS'	=> ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class],
374
+                'TTF'	=> ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class],
375
+                'HEIC'  => ['mimetype' => '/image\/hei(f|c)/', 'class' => Preview\HEIC::class],
376
+            ];
377
+
378
+            foreach ($imagickProviders as $queryFormat => $provider) {
379
+                $class = $provider['class'];
380
+                if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
381
+                    continue;
382
+                }
383
+
384
+                if (count($checkImagick->queryFormats($queryFormat)) === 1) {
385
+                    $this->registerCoreProvider($class, $provider['mimetype']);
386
+                }
387
+            }
388
+
389
+            if (count($checkImagick->queryFormats('PDF')) === 1) {
390
+                if (\OC_Helper::is_function_enabled('shell_exec')) {
391
+                    $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null));
392
+
393
+                    if (!$officeFound) {
394
+                        //let's see if there is libreoffice or openoffice on this machine
395
+                        $whichLibreOffice = shell_exec('command -v libreoffice');
396
+                        $officeFound = !empty($whichLibreOffice);
397
+                        if (!$officeFound) {
398
+                            $whichOpenOffice = shell_exec('command -v openoffice');
399
+                            $officeFound = !empty($whichOpenOffice);
400
+                        }
401
+                    }
402
+
403
+                    if ($officeFound) {
404
+                        $this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/');
405
+                        $this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/');
406
+                        $this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/');
407
+                        $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
408
+                        $this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/');
409
+                    }
410
+                }
411
+            }
412
+        }
413
+
414
+        // Video requires avconv or ffmpeg
415
+        if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) {
416
+            $avconvBinary = \OC_Helper::findBinaryPath('avconv');
417
+            $ffmpegBinary = $avconvBinary ? null : \OC_Helper::findBinaryPath('ffmpeg');
418
+
419
+            if ($avconvBinary || $ffmpegBinary) {
420
+                // FIXME // a bit hacky but didn't want to use subclasses
421
+                \OC\Preview\Movie::$avconvBinary = $avconvBinary;
422
+                \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
423
+
424
+                $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/');
425
+            }
426
+        }
427
+    }
428 428
 }
Please login to merge, or discard this patch.
lib/private/Preview/HEIC.php 2 patches
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -23,18 +23,18 @@
 block discarded – undo
23 23
 namespace OC\Preview;
24 24
 
25 25
 class HEIC extends Bitmap {
26
-	/**
27
-	 * {@inheritDoc}
28
-	 */
29
-	public function getMimeType() {
30
-		return '/image\/hei(f|c)/';
31
-	}
26
+    /**
27
+     * {@inheritDoc}
28
+     */
29
+    public function getMimeType() {
30
+        return '/image\/hei(f|c)/';
31
+    }
32 32
 
33
-	/**
34
-	 * {@inheritDoc}
35
-	 */
36
-	public function isAvailable(\OCP\Files\FileInfo $file) {
37
-		return in_array("HEIC", \Imagick::queryFonts("HEI*") );
38
-	}
33
+    /**
34
+     * {@inheritDoc}
35
+     */
36
+    public function isAvailable(\OCP\Files\FileInfo $file) {
37
+        return in_array("HEIC", \Imagick::queryFonts("HEI*") );
38
+    }
39 39
 
40 40
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@
 block discarded – undo
34 34
 	 * {@inheritDoc}
35 35
 	 */
36 36
 	public function isAvailable(\OCP\Files\FileInfo $file) {
37
-		return in_array("HEIC", \Imagick::queryFonts("HEI*") );
37
+		return in_array("HEIC", \Imagick::queryFonts("HEI*"));
38 38
 	}
39 39
 
40 40
 }
Please login to merge, or discard this patch.