PreviewManager.php 10.4 KB
Newer Older
1
2
<?php
/**
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
3
4
 * @author Joas Schilling <nickvergessen@owncloud.com>
 * @author Morris Jobke <hey@morrisjobke.de>
5
 * @author Olivier Paroz <github@oparoz.com>
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
6
7
8
 * @author Robin Appelman <icewind@owncloud.com>
 * @author Thomas Müller <thomas.mueller@tmit.eu>
 *
Thomas Müller's avatar
Thomas Müller committed
9
 * @copyright Copyright (c) 2016, ownCloud, Inc.
Jenkins for ownCloud's avatar
Jenkins for ownCloud committed
10
11
12
13
14
15
16
17
18
19
20
21
22
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
24
25
26
27
 *
 */
namespace OC;

use OCP\IPreview;
28
use OCP\Preview\IProvider;
29
30

class PreviewManager implements IPreview {
Joas Schilling's avatar
Joas Schilling committed
31
32
33
	/** @var \OCP\IConfig */
	protected $config;

34
	/** @var bool */
35
36
	protected $providerListDirty = false;

37
38
39
	/** @var bool */
	protected $registeredCoreProviders = false;

40
41
42
	/** @var array */
	protected $providers = [];

43
44
45
	/** @var array mime type => support status */
	protected $mimeTypeSupportMap = [];

Joas Schilling's avatar
Joas Schilling committed
46
47
48
	/** @var array */
	protected $defaultProviders;

49
50
	/**
	 * Constructor
Joas Schilling's avatar
Joas Schilling committed
51
52
	 *
	 * @param \OCP\IConfig $config
53
	 */
Joas Schilling's avatar
Joas Schilling committed
54
55
	public function __construct(\OCP\IConfig $config) {
		$this->config = $config;
56
57
	}

58
59
60
61
	/**
	 * In order to improve lazy loading a closure can be registered which will be
	 * called in case preview providers are actually requested
	 *
62
	 * $callable has to return an instance of \OCP\Preview\IProvider
63
64
65
66
67
68
	 *
	 * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider
	 * @param \Closure $callable
	 * @return void
	 */
	public function registerProvider($mimeTypeRegex, \Closure $callable) {
Joas Schilling's avatar
Joas Schilling committed
69
		if (!$this->config->getSystemValue('enable_previews', true)) {
70
71
72
			return;
		}

73
74
75
76
		if (!isset($this->providers[$mimeTypeRegex])) {
			$this->providers[$mimeTypeRegex] = [];
		}
		$this->providers[$mimeTypeRegex][] = $callable;
77
		$this->providerListDirty = true;
78
79
	}

80
81
82
83
84
	/**
	 * Get all providers
	 * @return array
	 */
	public function getProviders() {
85
86
87
88
		if (!$this->config->getSystemValue('enable_previews', true)) {
			return [];
		}

89
		$this->registerCoreProviders();
90
91
92
93
94
		if ($this->providerListDirty) {
			$keys = array_map('strlen', array_keys($this->providers));
			array_multisort($keys, SORT_DESC, $this->providers);
			$this->providerListDirty = false;
		}
95
96
97
98
99
100
101
102
103

		return $this->providers;
	}

	/**
	 * Does the manager have any providers
	 * @return bool
	 */
	public function hasProviders() {
104
		$this->registerCoreProviders();
105
106
107
		return !empty($this->providers);
	}

108
	/**
109
	 * return a preview of a file
110
	 *
111
112
113
114
	 * @param string $file The path to the file where you want a thumbnail from
	 * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image
	 * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image
	 * @param boolean $scaleUp Scale smaller images up to the thumbnail size or not. Might look ugly
115
	 * @return \OCP\IImage
116
	 */
Joas Schilling's avatar
Joas Schilling committed
117
	public function createPreview($file, $maxX = 100, $maxY = 75, $scaleUp = false) {
118
119
120
121
122
		$preview = new \OC\Preview('', '/', $file, $maxX, $maxY, $scaleUp);
		return $preview->getPreview();
	}

	/**
123
	 * returns true if the passed mime type is supported
124
	 *
125
126
127
	 * @param string $mimeType
	 * @return boolean
	 */
Joas Schilling's avatar
Joas Schilling committed
128
	public function isMimeSupported($mimeType = '*') {
Joas Schilling's avatar
Joas Schilling committed
129
		if (!$this->config->getSystemValue('enable_previews', true)) {
130
131
132
			return false;
		}

133
134
135
136
		if (isset($this->mimeTypeSupportMap[$mimeType])) {
			return $this->mimeTypeSupportMap[$mimeType];
		}

137
		$this->registerCoreProviders();
138
139
140
		$providerMimeTypes = array_keys($this->providers);
		foreach ($providerMimeTypes as $supportedMimeType) {
			if (preg_match($supportedMimeType, $mimeType)) {
141
				$this->mimeTypeSupportMap[$mimeType] = true;
142
143
144
				return true;
			}
		}
145
		$this->mimeTypeSupportMap[$mimeType] = false;
146
		return false;
147
	}
148
149
150
151

	/**
	 * Check if a preview can be generated for a file
	 *
152
	 * @param \OCP\Files\FileInfo $file
153
154
	 * @return bool
	 */
Joas Schilling's avatar
Joas Schilling committed
155
	public function isAvailable(\OCP\Files\FileInfo $file) {
Joas Schilling's avatar
Joas Schilling committed
156
		if (!$this->config->getSystemValue('enable_previews', true)) {
157
158
159
			return false;
		}

160
		$this->registerCoreProviders();
161
162
163
164
		if (!$this->isMimeSupported($file->getMimetype())) {
			return false;
		}

165
166
167
168
169
170
171
		$mount = $file->getMountPoint();
		if ($mount and !$mount->getOption('previews', true)){
			return false;
		}

		foreach ($this->providers as $supportedMimeType => $providers) {
			if (preg_match($supportedMimeType, $file->getMimetype())) {
172
173
				foreach ($providers as $closure) {
					$provider = $closure();
174
					if (!($provider instanceof IProvider)) {
175
176
177
						continue;
					}

178
					/** @var $provider IProvider */
179
180
181
182
183
184
185
					if ($provider->isAvailable($file)) {
						return true;
					}
				}
			}
		}
		return false;
186
	}
187
188
189
190
191

	/**
	 * List of enabled default providers
	 *
	 * The following providers are enabled by default:
192
193
194
195
196
	 *  - OC\Preview\PNG
	 *  - OC\Preview\JPEG
	 *  - OC\Preview\GIF
	 *  - OC\Preview\BMP
	 *  - OC\Preview\XBitmap
197
	 *  - OC\Preview\MarkDown
198
199
200
201
	 *  - OC\Preview\MP3
	 *  - OC\Preview\TXT
	 *
	 * The following providers are disabled by default due to performance or privacy concerns:
202
203
204
	 *  - OC\Preview\Font
	 *  - OC\Preview\Illustrator
	 *  - OC\Preview\Movie
205
206
207
208
	 *  - OC\Preview\MSOfficeDoc
	 *  - OC\Preview\MSOffice2003
	 *  - OC\Preview\MSOffice2007
	 *  - OC\Preview\OpenDocument
209
210
211
	 *  - OC\Preview\PDF
	 *  - OC\Preview\Photoshop
	 *  - OC\Preview\Postscript
212
213
214
215
216
217
218
219
220
221
222
	 *  - OC\Preview\StarOffice
	 *  - OC\Preview\SVG
	 *  - OC\Preview\TIFF
	 *
	 * @return array
	 */
	protected function getEnabledDefaultProvider() {
		if ($this->defaultProviders !== null) {
			return $this->defaultProviders;
		}

223
224
225
226
227
228
229
230
231
		$imageProviders = [
			'OC\Preview\PNG',
			'OC\Preview\JPEG',
			'OC\Preview\GIF',
			'OC\Preview\BMP',
			'OC\Preview\XBitmap'
		];

		$this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([
232
			'OC\Preview\MarkDown',
233
234
			'OC\Preview\MP3',
			'OC\Preview\TXT',
235
236
237
238
239
240
		], $imageProviders));

		if (in_array('OC\Preview\Image', $this->defaultProviders)) {
			$this->defaultProviders = array_merge($this->defaultProviders, $imageProviders);
		}
		$this->defaultProviders = array_unique($this->defaultProviders);
241
242
243
244
245
246
247
248
249
		return $this->defaultProviders;
	}

	/**
	 * Register the default providers (if enabled)
	 *
	 * @param string $class
	 * @param string $mimeType
	 */
Joas Schilling's avatar
Joas Schilling committed
250
	protected function registerCoreProvider($class, $mimeType, $options = []) {
251
		if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
Joas Schilling's avatar
Joas Schilling committed
252
253
			$this->registerProvider($mimeType, function () use ($class, $options) {
				return new $class($options);
254
255
256
257
258
259
260
261
			});
		}
	}

	/**
	 * Register the default providers (if enabled)
	 */
	protected function registerCoreProviders() {
262
263
264
265
266
		if ($this->registeredCoreProviders) {
			return;
		}
		$this->registeredCoreProviders = true;

267
268
		$this->registerCoreProvider('OC\Preview\TXT', '/text\/plain/');
		$this->registerCoreProvider('OC\Preview\MarkDown', '/text\/(x-)?markdown/');
269
270
271
272
273
		$this->registerCoreProvider('OC\Preview\PNG', '/image\/png/');
		$this->registerCoreProvider('OC\Preview\JPEG', '/image\/jpeg/');
		$this->registerCoreProvider('OC\Preview\GIF', '/image\/gif/');
		$this->registerCoreProvider('OC\Preview\BMP', '/image\/bmp/');
		$this->registerCoreProvider('OC\Preview\XBitmap', '/image\/x-xbitmap/');
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
		$this->registerCoreProvider('OC\Preview\MP3', '/audio\/mpeg/');

		// SVG, Office and Bitmap require imagick
		if (extension_loaded('imagick')) {
			$checkImagick = new \Imagick();

			$imagickProviders = [
				'SVG'	=> ['mimetype' => '/image\/svg\+xml/', 'class' => '\OC\Preview\SVG'],
				'TIFF'	=> ['mimetype' => '/image\/tiff/', 'class' => '\OC\Preview\TIFF'],
				'PDF'	=> ['mimetype' => '/application\/pdf/', 'class' => '\OC\Preview\PDF'],
				'AI'	=> ['mimetype' => '/application\/illustrator/', 'class' => '\OC\Preview\Illustrator'],
				'PSD'	=> ['mimetype' => '/application\/x-photoshop/', 'class' => '\OC\Preview\Photoshop'],
				'EPS'	=> ['mimetype' => '/application\/postscript/', 'class' => '\OC\Preview\Postscript'],
				'TTF'	=> ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => '\OC\Preview\Font'],
			];

			foreach ($imagickProviders as $queryFormat => $provider) {
				$class = $provider['class'];
				if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
					continue;
				}

				if (count($checkImagick->queryFormats($queryFormat)) === 1) {
					$this->registerCoreProvider($class, $provider['mimetype']);
				}
			}

			if (count($checkImagick->queryFormats('PDF')) === 1) {
				// Office previews are currently not supported on Windows
				if (!\OC_Util::runningOnWindows() && \OC_Helper::is_function_enabled('shell_exec')) {
Joas Schilling's avatar
Joas Schilling committed
304
					$officeFound = is_string($this->config->getSystemValue('preview_libreoffice_path', null));
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

					if (!$officeFound) {
						//let's see if there is libreoffice or openoffice on this machine
						$whichLibreOffice = shell_exec('command -v libreoffice');
						$officeFound = !empty($whichLibreOffice);
						if (!$officeFound) {
							$whichOpenOffice = shell_exec('command -v openoffice');
							$officeFound = !empty($whichOpenOffice);
						}
					}

					if ($officeFound) {
						$this->registerCoreProvider('\OC\Preview\MSOfficeDoc', '/application\/msword/');
						$this->registerCoreProvider('\OC\Preview\MSOffice2003', '/application\/vnd.ms-.*/');
						$this->registerCoreProvider('\OC\Preview\MSOffice2007', '/application\/vnd.openxmlformats-officedocument.*/');
						$this->registerCoreProvider('\OC\Preview\OpenDocument', '/application\/vnd.oasis.opendocument.*/');
						$this->registerCoreProvider('\OC\Preview\StarOffice', '/application\/vnd.sun.xml.*/');
					}
				}
			}
		}

		// Video requires avconv or ffmpeg and is therefor
		// currently not supported on Windows.
		if (in_array('OC\Preview\Movie', $this->getEnabledDefaultProvider()) && !\OC_Util::runningOnWindows()) {
			$avconvBinary = \OC_Helper::findBinaryPath('avconv');
			$ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg');

			if ($avconvBinary || $ffmpegBinary) {
				// FIXME // a bit hacky but didn't want to use subclasses
				\OC\Preview\Movie::$avconvBinary = $avconvBinary;
				\OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary;

				$this->registerCoreProvider('\OC\Preview\Movie', '/video\/.*/');
			}
		}
	}
342
}