Completed
Push — stable13 ( e93106...048f99 )
by Morris
30:41 queued 15:45
created
lib/private/Preview/GeneratorHelper.php 1 patch
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -36,54 +36,54 @@
 block discarded – undo
36 36
  */
37 37
 class GeneratorHelper {
38 38
 
39
-	/** @var IRootFolder */
40
-	private $rootFolder;
39
+    /** @var IRootFolder */
40
+    private $rootFolder;
41 41
 
42
-	/** @var IConfig */
43
-	private $config;
42
+    /** @var IConfig */
43
+    private $config;
44 44
 
45
-	public function __construct(IRootFolder $rootFolder, IConfig $config) {
46
-		$this->rootFolder = $rootFolder;
47
-		$this->config = $config;
48
-	}
45
+    public function __construct(IRootFolder $rootFolder, IConfig $config) {
46
+        $this->rootFolder = $rootFolder;
47
+        $this->config = $config;
48
+    }
49 49
 
50
-	/**
51
-	 * @param IProvider $provider
52
-	 * @param File $file
53
-	 * @param int $maxWidth
54
-	 * @param int $maxHeight
55
-	 * @return bool|IImage
56
-	 */
57
-	public function getThumbnail(IProvider $provider, File $file, $maxWidth, $maxHeight) {
58
-		list($view, $path) = $this->getViewAndPath($file);
59
-		return $provider->getThumbnail($path, $maxWidth, $maxHeight, false, $view);
60
-	}
50
+    /**
51
+     * @param IProvider $provider
52
+     * @param File $file
53
+     * @param int $maxWidth
54
+     * @param int $maxHeight
55
+     * @return bool|IImage
56
+     */
57
+    public function getThumbnail(IProvider $provider, File $file, $maxWidth, $maxHeight) {
58
+        list($view, $path) = $this->getViewAndPath($file);
59
+        return $provider->getThumbnail($path, $maxWidth, $maxHeight, false, $view);
60
+    }
61 61
 
62
-	/**
63
-	 * @param File $file
64
-	 * @return array
65
-	 * This is required to create the old view and path
66
-	 */
67
-	private function getViewAndPath(File $file) {
68
-		$view = new View($file->getParent()->getPath());
69
-		$path = $file->getName();
62
+    /**
63
+     * @param File $file
64
+     * @return array
65
+     * This is required to create the old view and path
66
+     */
67
+    private function getViewAndPath(File $file) {
68
+        $view = new View($file->getParent()->getPath());
69
+        $path = $file->getName();
70 70
 
71
-		return [$view, $path];
72
-	}
71
+        return [$view, $path];
72
+    }
73 73
 
74
-	/**
75
-	 * @param ISimpleFile $maxPreview
76
-	 * @return IImage
77
-	 */
78
-	public function getImage(ISimpleFile $maxPreview) {
79
-		return new img($maxPreview->getContent());
80
-	}
74
+    /**
75
+     * @param ISimpleFile $maxPreview
76
+     * @return IImage
77
+     */
78
+    public function getImage(ISimpleFile $maxPreview) {
79
+        return new img($maxPreview->getContent());
80
+    }
81 81
 
82
-	/**
83
-	 * @param $provider
84
-	 * @return IProvider
85
-	 */
86
-	public function getProvider($provider) {
87
-		return $provider();
88
-	}
82
+    /**
83
+     * @param $provider
84
+     * @return IProvider
85
+     */
86
+    public function getProvider($provider) {
87
+        return $provider();
88
+    }
89 89
 }
Please login to merge, or discard this patch.
lib/private/PreviewManager.php 1 patch
Indentation   +382 added lines, -382 removed lines patch added patch discarded remove patch
@@ -39,386 +39,386 @@
 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
-		return new \OC_Image($preview->getContent());
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
-		if ($this->generator === null) {
191
-			$this->generator = new Generator(
192
-				$this->config,
193
-				$this,
194
-				$this->appData,
195
-				new GeneratorHelper(
196
-					$this->rootFolder,
197
-					$this->config
198
-				),
199
-				$this->eventDispatcher
200
-			);
201
-		}
202
-
203
-		return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType);
204
-	}
205
-
206
-	/**
207
-	 * returns true if the passed mime type is supported
208
-	 *
209
-	 * @param string $mimeType
210
-	 * @return boolean
211
-	 */
212
-	public function isMimeSupported($mimeType = '*') {
213
-		if (!$this->config->getSystemValue('enable_previews', true)) {
214
-			return false;
215
-		}
216
-
217
-		if (isset($this->mimeTypeSupportMap[$mimeType])) {
218
-			return $this->mimeTypeSupportMap[$mimeType];
219
-		}
220
-
221
-		$this->registerCoreProviders();
222
-		$providerMimeTypes = array_keys($this->providers);
223
-		foreach ($providerMimeTypes as $supportedMimeType) {
224
-			if (preg_match($supportedMimeType, $mimeType)) {
225
-				$this->mimeTypeSupportMap[$mimeType] = true;
226
-				return true;
227
-			}
228
-		}
229
-		$this->mimeTypeSupportMap[$mimeType] = false;
230
-		return false;
231
-	}
232
-
233
-	/**
234
-	 * Check if a preview can be generated for a file
235
-	 *
236
-	 * @param \OCP\Files\FileInfo $file
237
-	 * @return bool
238
-	 */
239
-	public function isAvailable(\OCP\Files\FileInfo $file) {
240
-		if (!$this->config->getSystemValue('enable_previews', true)) {
241
-			return false;
242
-		}
243
-
244
-		$this->registerCoreProviders();
245
-		if (!$this->isMimeSupported($file->getMimetype())) {
246
-			return false;
247
-		}
248
-
249
-		$mount = $file->getMountPoint();
250
-		if ($mount and !$mount->getOption('previews', true)){
251
-			return false;
252
-		}
253
-
254
-		foreach ($this->providers as $supportedMimeType => $providers) {
255
-			if (preg_match($supportedMimeType, $file->getMimetype())) {
256
-				foreach ($providers as $closure) {
257
-					$provider = $closure();
258
-					if (!($provider instanceof IProvider)) {
259
-						continue;
260
-					}
261
-
262
-					/** @var $provider IProvider */
263
-					if ($provider->isAvailable($file)) {
264
-						return true;
265
-					}
266
-				}
267
-			}
268
-		}
269
-		return false;
270
-	}
271
-
272
-	/**
273
-	 * List of enabled default providers
274
-	 *
275
-	 * The following providers are enabled by default:
276
-	 *  - OC\Preview\PNG
277
-	 *  - OC\Preview\JPEG
278
-	 *  - OC\Preview\GIF
279
-	 *  - OC\Preview\BMP
280
-	 *  - OC\Preview\XBitmap
281
-	 *  - OC\Preview\MarkDown
282
-	 *  - OC\Preview\MP3
283
-	 *  - OC\Preview\TXT
284
-	 *
285
-	 * The following providers are disabled by default due to performance or privacy concerns:
286
-	 *  - OC\Preview\Font
287
-	 *  - OC\Preview\Illustrator
288
-	 *  - OC\Preview\Movie
289
-	 *  - OC\Preview\MSOfficeDoc
290
-	 *  - OC\Preview\MSOffice2003
291
-	 *  - OC\Preview\MSOffice2007
292
-	 *  - OC\Preview\OpenDocument
293
-	 *  - OC\Preview\PDF
294
-	 *  - OC\Preview\Photoshop
295
-	 *  - OC\Preview\Postscript
296
-	 *  - OC\Preview\StarOffice
297
-	 *  - OC\Preview\SVG
298
-	 *  - OC\Preview\TIFF
299
-	 *
300
-	 * @return array
301
-	 */
302
-	protected function getEnabledDefaultProvider() {
303
-		if ($this->defaultProviders !== null) {
304
-			return $this->defaultProviders;
305
-		}
306
-
307
-		$imageProviders = [
308
-			'OC\Preview\PNG',
309
-			'OC\Preview\JPEG',
310
-			'OC\Preview\GIF',
311
-			'OC\Preview\BMP',
312
-			'OC\Preview\XBitmap'
313
-		];
314
-
315
-		$this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([
316
-			'OC\Preview\MarkDown',
317
-			'OC\Preview\MP3',
318
-			'OC\Preview\TXT',
319
-		], $imageProviders));
320
-
321
-		if (in_array('OC\Preview\Image', $this->defaultProviders)) {
322
-			$this->defaultProviders = array_merge($this->defaultProviders, $imageProviders);
323
-		}
324
-		$this->defaultProviders = array_unique($this->defaultProviders);
325
-		return $this->defaultProviders;
326
-	}
327
-
328
-	/**
329
-	 * Register the default providers (if enabled)
330
-	 *
331
-	 * @param string $class
332
-	 * @param string $mimeType
333
-	 */
334
-	protected function registerCoreProvider($class, $mimeType, $options = []) {
335
-		if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
336
-			$this->registerProvider($mimeType, function () use ($class, $options) {
337
-				return new $class($options);
338
-			});
339
-		}
340
-	}
341
-
342
-	/**
343
-	 * Register the default providers (if enabled)
344
-	 */
345
-	protected function registerCoreProviders() {
346
-		if ($this->registeredCoreProviders) {
347
-			return;
348
-		}
349
-		$this->registeredCoreProviders = true;
350
-
351
-		$this->registerCoreProvider('OC\Preview\TXT', '/text\/plain/');
352
-		$this->registerCoreProvider('OC\Preview\MarkDown', '/text\/(x-)?markdown/');
353
-		$this->registerCoreProvider('OC\Preview\PNG', '/image\/png/');
354
-		$this->registerCoreProvider('OC\Preview\JPEG', '/image\/jpeg/');
355
-		$this->registerCoreProvider('OC\Preview\GIF', '/image\/gif/');
356
-		$this->registerCoreProvider('OC\Preview\BMP', '/image\/bmp/');
357
-		$this->registerCoreProvider('OC\Preview\XBitmap', '/image\/x-xbitmap/');
358
-		$this->registerCoreProvider('OC\Preview\MP3', '/audio\/mpeg/');
359
-
360
-		// SVG, Office and Bitmap require imagick
361
-		if (extension_loaded('imagick')) {
362
-			$checkImagick = new \Imagick();
363
-
364
-			$imagickProviders = [
365
-				'SVG'	=> ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'],
366
-				'TIFF'	=> ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'],
367
-				'PDF'	=> ['mimetype' => '/application\/pdf/', 'class' => '\OC\Preview\PDF'],
368
-				'AI'	=> ['mimetype' => '/application\/illustrator/', 'class' => '\OC\Preview\Illustrator'],
369
-				'PSD'	=> ['mimetype' => '/application\/x-photoshop/', 'class' => '\OC\Preview\Photoshop'],
370
-				'EPS'	=> ['mimetype' => '/application\/postscript/', 'class' => '\OC\Preview\Postscript'],
371
-				'TTF'	=> ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => '\OC\Preview\Font'],
372
-			];
373
-
374
-			foreach ($imagickProviders as $queryFormat => $provider) {
375
-				$class = $provider['class'];
376
-				if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
377
-					continue;
378
-				}
379
-
380
-				if (count($checkImagick->queryFormats($queryFormat)) === 1) {
381
-					$this->registerCoreProvider($class, $provider['mimetype']);
382
-				}
383
-			}
384
-
385
-			if (count($checkImagick->queryFormats('PDF')) === 1) {
386
-				if (\OC_Helper::is_function_enabled('shell_exec')) {
387
-					$officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null));
388
-
389
-					if (!$officeFound) {
390
-						//let's see if there is libreoffice or openoffice on this machine
391
-						$whichLibreOffice = shell_exec('command -v libreoffice');
392
-						$officeFound = !empty($whichLibreOffice);
393
-						if (!$officeFound) {
394
-							$whichOpenOffice = shell_exec('command -v openoffice');
395
-							$officeFound = !empty($whichOpenOffice);
396
-						}
397
-					}
398
-
399
-					if ($officeFound) {
400
-						$this->registerCoreProvider('\OC\Preview\MSOfficeDoc', '/application\/msword/');
401
-						$this->registerCoreProvider('\OC\Preview\MSOffice2003', '/application\/vnd.ms-.*/');
402
-						$this->registerCoreProvider('\OC\Preview\MSOffice2007', '/application\/vnd.openxmlformats-officedocument.*/');
403
-						$this->registerCoreProvider('\OC\Preview\OpenDocument', '/application\/vnd.oasis.opendocument.*/');
404
-						$this->registerCoreProvider('\OC\Preview\StarOffice', '/application\/vnd.sun.xml.*/');
405
-					}
406
-				}
407
-			}
408
-		}
409
-
410
-		// Video requires avconv or ffmpeg
411
-		if (in_array('OC\Preview\Movie', $this->getEnabledDefaultProvider())) {
412
-			$avconvBinary = \OC_Helper::findBinaryPath('avconv');
413
-			$ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg');
414
-
415
-			if ($avconvBinary || $ffmpegBinary) {
416
-				// FIXME // a bit hacky but didn't want to use subclasses
417
-				\OC\Preview\Movie::$avconvBinary = $avconvBinary;
418
-				\OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
419
-
420
-				$this->registerCoreProvider('\OC\Preview\Movie', '/video\/.*/');
421
-			}
422
-		}
423
-	}
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
+        return new \OC_Image($preview->getContent());
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
+        if ($this->generator === null) {
191
+            $this->generator = new Generator(
192
+                $this->config,
193
+                $this,
194
+                $this->appData,
195
+                new GeneratorHelper(
196
+                    $this->rootFolder,
197
+                    $this->config
198
+                ),
199
+                $this->eventDispatcher
200
+            );
201
+        }
202
+
203
+        return $this->generator->getPreview($file, $width, $height, $crop, $mode, $mimeType);
204
+    }
205
+
206
+    /**
207
+     * returns true if the passed mime type is supported
208
+     *
209
+     * @param string $mimeType
210
+     * @return boolean
211
+     */
212
+    public function isMimeSupported($mimeType = '*') {
213
+        if (!$this->config->getSystemValue('enable_previews', true)) {
214
+            return false;
215
+        }
216
+
217
+        if (isset($this->mimeTypeSupportMap[$mimeType])) {
218
+            return $this->mimeTypeSupportMap[$mimeType];
219
+        }
220
+
221
+        $this->registerCoreProviders();
222
+        $providerMimeTypes = array_keys($this->providers);
223
+        foreach ($providerMimeTypes as $supportedMimeType) {
224
+            if (preg_match($supportedMimeType, $mimeType)) {
225
+                $this->mimeTypeSupportMap[$mimeType] = true;
226
+                return true;
227
+            }
228
+        }
229
+        $this->mimeTypeSupportMap[$mimeType] = false;
230
+        return false;
231
+    }
232
+
233
+    /**
234
+     * Check if a preview can be generated for a file
235
+     *
236
+     * @param \OCP\Files\FileInfo $file
237
+     * @return bool
238
+     */
239
+    public function isAvailable(\OCP\Files\FileInfo $file) {
240
+        if (!$this->config->getSystemValue('enable_previews', true)) {
241
+            return false;
242
+        }
243
+
244
+        $this->registerCoreProviders();
245
+        if (!$this->isMimeSupported($file->getMimetype())) {
246
+            return false;
247
+        }
248
+
249
+        $mount = $file->getMountPoint();
250
+        if ($mount and !$mount->getOption('previews', true)){
251
+            return false;
252
+        }
253
+
254
+        foreach ($this->providers as $supportedMimeType => $providers) {
255
+            if (preg_match($supportedMimeType, $file->getMimetype())) {
256
+                foreach ($providers as $closure) {
257
+                    $provider = $closure();
258
+                    if (!($provider instanceof IProvider)) {
259
+                        continue;
260
+                    }
261
+
262
+                    /** @var $provider IProvider */
263
+                    if ($provider->isAvailable($file)) {
264
+                        return true;
265
+                    }
266
+                }
267
+            }
268
+        }
269
+        return false;
270
+    }
271
+
272
+    /**
273
+     * List of enabled default providers
274
+     *
275
+     * The following providers are enabled by default:
276
+     *  - OC\Preview\PNG
277
+     *  - OC\Preview\JPEG
278
+     *  - OC\Preview\GIF
279
+     *  - OC\Preview\BMP
280
+     *  - OC\Preview\XBitmap
281
+     *  - OC\Preview\MarkDown
282
+     *  - OC\Preview\MP3
283
+     *  - OC\Preview\TXT
284
+     *
285
+     * The following providers are disabled by default due to performance or privacy concerns:
286
+     *  - OC\Preview\Font
287
+     *  - OC\Preview\Illustrator
288
+     *  - OC\Preview\Movie
289
+     *  - OC\Preview\MSOfficeDoc
290
+     *  - OC\Preview\MSOffice2003
291
+     *  - OC\Preview\MSOffice2007
292
+     *  - OC\Preview\OpenDocument
293
+     *  - OC\Preview\PDF
294
+     *  - OC\Preview\Photoshop
295
+     *  - OC\Preview\Postscript
296
+     *  - OC\Preview\StarOffice
297
+     *  - OC\Preview\SVG
298
+     *  - OC\Preview\TIFF
299
+     *
300
+     * @return array
301
+     */
302
+    protected function getEnabledDefaultProvider() {
303
+        if ($this->defaultProviders !== null) {
304
+            return $this->defaultProviders;
305
+        }
306
+
307
+        $imageProviders = [
308
+            'OC\Preview\PNG',
309
+            'OC\Preview\JPEG',
310
+            'OC\Preview\GIF',
311
+            'OC\Preview\BMP',
312
+            'OC\Preview\XBitmap'
313
+        ];
314
+
315
+        $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([
316
+            'OC\Preview\MarkDown',
317
+            'OC\Preview\MP3',
318
+            'OC\Preview\TXT',
319
+        ], $imageProviders));
320
+
321
+        if (in_array('OC\Preview\Image', $this->defaultProviders)) {
322
+            $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders);
323
+        }
324
+        $this->defaultProviders = array_unique($this->defaultProviders);
325
+        return $this->defaultProviders;
326
+    }
327
+
328
+    /**
329
+     * Register the default providers (if enabled)
330
+     *
331
+     * @param string $class
332
+     * @param string $mimeType
333
+     */
334
+    protected function registerCoreProvider($class, $mimeType, $options = []) {
335
+        if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
336
+            $this->registerProvider($mimeType, function () use ($class, $options) {
337
+                return new $class($options);
338
+            });
339
+        }
340
+    }
341
+
342
+    /**
343
+     * Register the default providers (if enabled)
344
+     */
345
+    protected function registerCoreProviders() {
346
+        if ($this->registeredCoreProviders) {
347
+            return;
348
+        }
349
+        $this->registeredCoreProviders = true;
350
+
351
+        $this->registerCoreProvider('OC\Preview\TXT', '/text\/plain/');
352
+        $this->registerCoreProvider('OC\Preview\MarkDown', '/text\/(x-)?markdown/');
353
+        $this->registerCoreProvider('OC\Preview\PNG', '/image\/png/');
354
+        $this->registerCoreProvider('OC\Preview\JPEG', '/image\/jpeg/');
355
+        $this->registerCoreProvider('OC\Preview\GIF', '/image\/gif/');
356
+        $this->registerCoreProvider('OC\Preview\BMP', '/image\/bmp/');
357
+        $this->registerCoreProvider('OC\Preview\XBitmap', '/image\/x-xbitmap/');
358
+        $this->registerCoreProvider('OC\Preview\MP3', '/audio\/mpeg/');
359
+
360
+        // SVG, Office and Bitmap require imagick
361
+        if (extension_loaded('imagick')) {
362
+            $checkImagick = new \Imagick();
363
+
364
+            $imagickProviders = [
365
+                'SVG'	=> ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'],
366
+                'TIFF'	=> ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'],
367
+                'PDF'	=> ['mimetype' => '/application\/pdf/', 'class' => '\OC\Preview\PDF'],
368
+                'AI'	=> ['mimetype' => '/application\/illustrator/', 'class' => '\OC\Preview\Illustrator'],
369
+                'PSD'	=> ['mimetype' => '/application\/x-photoshop/', 'class' => '\OC\Preview\Photoshop'],
370
+                'EPS'	=> ['mimetype' => '/application\/postscript/', 'class' => '\OC\Preview\Postscript'],
371
+                'TTF'	=> ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => '\OC\Preview\Font'],
372
+            ];
373
+
374
+            foreach ($imagickProviders as $queryFormat => $provider) {
375
+                $class = $provider['class'];
376
+                if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
377
+                    continue;
378
+                }
379
+
380
+                if (count($checkImagick->queryFormats($queryFormat)) === 1) {
381
+                    $this->registerCoreProvider($class, $provider['mimetype']);
382
+                }
383
+            }
384
+
385
+            if (count($checkImagick->queryFormats('PDF')) === 1) {
386
+                if (\OC_Helper::is_function_enabled('shell_exec')) {
387
+                    $officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null));
388
+
389
+                    if (!$officeFound) {
390
+                        //let's see if there is libreoffice or openoffice on this machine
391
+                        $whichLibreOffice = shell_exec('command -v libreoffice');
392
+                        $officeFound = !empty($whichLibreOffice);
393
+                        if (!$officeFound) {
394
+                            $whichOpenOffice = shell_exec('command -v openoffice');
395
+                            $officeFound = !empty($whichOpenOffice);
396
+                        }
397
+                    }
398
+
399
+                    if ($officeFound) {
400
+                        $this->registerCoreProvider('\OC\Preview\MSOfficeDoc', '/application\/msword/');
401
+                        $this->registerCoreProvider('\OC\Preview\MSOffice2003', '/application\/vnd.ms-.*/');
402
+                        $this->registerCoreProvider('\OC\Preview\MSOffice2007', '/application\/vnd.openxmlformats-officedocument.*/');
403
+                        $this->registerCoreProvider('\OC\Preview\OpenDocument', '/application\/vnd.oasis.opendocument.*/');
404
+                        $this->registerCoreProvider('\OC\Preview\StarOffice', '/application\/vnd.sun.xml.*/');
405
+                    }
406
+                }
407
+            }
408
+        }
409
+
410
+        // Video requires avconv or ffmpeg
411
+        if (in_array('OC\Preview\Movie', $this->getEnabledDefaultProvider())) {
412
+            $avconvBinary = \OC_Helper::findBinaryPath('avconv');
413
+            $ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg');
414
+
415
+            if ($avconvBinary || $ffmpegBinary) {
416
+                // FIXME // a bit hacky but didn't want to use subclasses
417
+                \OC\Preview\Movie::$avconvBinary = $avconvBinary;
418
+                \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;
419
+
420
+                $this->registerCoreProvider('\OC\Preview\Movie', '/video\/.*/');
421
+            }
422
+        }
423
+    }
424 424
 }
Please login to merge, or discard this patch.