Completed
Push — master ( c260e4...4b92a0 )
by Blizzz
18:04
created

Generator::getCachedPreview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Morris Jobke <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OC\Preview;
27
28
use OCP\Files\File;
29
use OCP\Files\IAppData;
30
use OCP\Files\NotFoundException;
31
use OCP\Files\NotPermittedException;
32
use OCP\Files\SimpleFS\ISimpleFile;
33
use OCP\Files\SimpleFS\ISimpleFolder;
34
use OCP\IConfig;
35
use OCP\IImage;
36
use OCP\IPreview;
37
use OCP\Preview\IProvider;
38
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
39
use Symfony\Component\EventDispatcher\GenericEvent;
40
41
class Generator {
42
43
	/** @var IPreview */
44
	private $previewManager;
45
	/** @var IConfig */
46
	private $config;
47
	/** @var IAppData */
48
	private $appData;
49
	/** @var GeneratorHelper */
50
	private $helper;
51
	/** @var EventDispatcherInterface */
52
	private $eventDispatcher;
53
54
	/**
55
	 * @param IConfig $config
56
	 * @param IPreview $previewManager
57
	 * @param IAppData $appData
58
	 * @param GeneratorHelper $helper
59
	 * @param EventDispatcherInterface $eventDispatcher
60
	 */
61
	public function __construct(
62
		IConfig $config,
63
		IPreview $previewManager,
64
		IAppData $appData,
65
		GeneratorHelper $helper,
66
		EventDispatcherInterface $eventDispatcher
67
	) {
68
		$this->config = $config;
69
		$this->previewManager = $previewManager;
70
		$this->appData = $appData;
71
		$this->helper = $helper;
72
		$this->eventDispatcher = $eventDispatcher;
73
	}
74
75
	/**
76
	 * Returns a preview of a file
77
	 *
78
	 * The cache is searched first and if nothing usable was found then a preview is
79
	 * generated by one of the providers
80
	 *
81
	 * @param File $file
82
	 * @param int $width
83
	 * @param int $height
84
	 * @param bool $crop
85
	 * @param string $mode
86
	 * @param string $mimeType
87
	 * @return ISimpleFile
88
	 * @throws NotFoundException
89
	 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
90
	 */
91
	public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
92
		//Make sure that we can read the file
93
		if (!$file->isReadable()) {
94
			throw new NotFoundException('Cannot read file');
95
		}
96
97
98
		$this->eventDispatcher->dispatch(
99
			IPreview::EVENT,
100
			new GenericEvent($file,[
101
				'width' => $width,
102
				'height' => $height,
103
				'crop' => $crop,
104
				'mode' => $mode
105
			])
106
		);
107
108
		if ($mimeType === null) {
109
			$mimeType = $file->getMimeType();
110
		}
111
		if (!$this->previewManager->isMimeSupported($mimeType)) {
112
			throw new NotFoundException();
113
		}
114
115
		$previewFolder = $this->getPreviewFolder($file);
116
117
		// Get the max preview and infer the max preview sizes from that
118
		$maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType);
119
		if ($maxPreview->getSize() === 0) {
120
			$maxPreview->delete();
121
			throw new NotFoundException('Max preview size 0, invalid!');
122
		}
123
124
		list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview);
125
126
		// If both width and heigth are -1 we just want the max preview
127
		if ($width === -1 && $height === -1) {
128
			$width = $maxWidth;
129
			$height = $maxHeight;
130
		}
131
132
		// Calculate the preview size
133
		list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
134
135
		// No need to generate a preview that is just the max preview
136
		if ($width === $maxWidth && $height === $maxHeight) {
137
			return $maxPreview;
138
		}
139
140
		// Try to get a cached preview. Else generate (and store) one
141
		try {
142
			try {
143
				$preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType());
144
			} catch (NotFoundException $e) {
145
				$preview = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
146
			}
147
		} catch (\InvalidArgumentException $e) {
148
			throw new NotFoundException();
149
		}
150
151
		if ($preview->getSize() === 0) {
152
			$preview->delete();
153
			throw new NotFoundException('Cached preview size 0, invalid!');
154
		}
155
156
		return $preview;
157
	}
158
159
	/**
160
	 * @param ISimpleFolder $previewFolder
161
	 * @param File $file
162
	 * @param string $mimeType
163
	 * @return ISimpleFile
164
	 * @throws NotFoundException
165
	 */
166
	private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) {
167
		$nodes = $previewFolder->getDirectoryListing();
168
169
		foreach ($nodes as $node) {
170
			if (strpos($node->getName(), 'max')) {
171
				return $node;
172
			}
173
		}
174
175
		$previewProviders = $this->previewManager->getProviders();
176
		foreach ($previewProviders as $supportedMimeType => $providers) {
177
			if (!preg_match($supportedMimeType, $mimeType)) {
178
				continue;
179
			}
180
181
			foreach ($providers as $provider) {
182
				$provider = $this->helper->getProvider($provider);
183
				if (!($provider instanceof IProvider)) {
184
					continue;
185
				}
186
187
				if (!$provider->isAvailable($file)) {
188
					continue;
189
				}
190
191
				$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096);
192
				$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096);
193
194
				$preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
195
196
				if (!($preview instanceof IImage)) {
197
					continue;
198
				}
199
200
				// Try to get the extention.
201
				try {
202
					$ext = $this->getExtention($preview->dataMimeType());
203
				} catch (\InvalidArgumentException $e) {
204
					// Just continue to the next iteration if this preview doesn't have a valid mimetype
205
					continue;
206
				}
207
208
				$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
209
				try {
210
					$file = $previewFolder->newFile($path);
211
					$file->putContent($preview->data());
212
				} catch (NotPermittedException $e) {
213
					throw new NotFoundException();
214
				}
215
216
				return $file;
217
			}
218
		}
219
220
		throw new NotFoundException();
221
	}
222
223
	/**
224
	 * @param ISimpleFile $file
225
	 * @return int[]
226
	 */
227
	private function getPreviewSize(ISimpleFile $file) {
228
		$size = explode('-', $file->getName());
229
		return [(int)$size[0], (int)$size[1]];
230
	}
231
232
	/**
233
	 * @param int $width
234
	 * @param int $height
235
	 * @param bool $crop
236
	 * @param string $mimeType
237
	 * @return string
238
	 */
239
	private function generatePath($width, $height, $crop, $mimeType) {
240
		$path = (string)$width . '-' . (string)$height;
241
		if ($crop) {
242
			$path .= '-crop';
243
		}
244
245
		$ext = $this->getExtention($mimeType);
246
		$path .= '.' . $ext;
247
		return $path;
248
	}
249
250
251
252
	/**
253
	 * @param int $width
254
	 * @param int $height
255
	 * @param bool $crop
256
	 * @param string $mode
257
	 * @param int $maxWidth
258
	 * @param int $maxHeight
259
	 * @return int[]
260
	 */
261
	private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
262
263
		/*
264
		 * If we are not cropping we have to make sure the requested image
265
		 * respects the aspect ratio of the original.
266
		 */
267
		if (!$crop) {
268
			$ratio = $maxHeight / $maxWidth;
269
270
			if ($width === -1) {
271
				$width = $height / $ratio;
272
			}
273
			if ($height === -1) {
274
				$height = $width * $ratio;
275
			}
276
277
			$ratioH = $height / $maxHeight;
278
			$ratioW = $width / $maxWidth;
279
280
			/*
281
			 * Fill means that the $height and $width are the max
282
			 * Cover means min.
283
			 */
284
			if ($mode === IPreview::MODE_FILL) {
285
				if ($ratioH > $ratioW) {
286
					$height = $width * $ratio;
287
				} else {
288
					$width = $height / $ratio;
289
				}
290
			} else if ($mode === IPreview::MODE_COVER) {
291
				if ($ratioH > $ratioW) {
292
					$width = $height / $ratio;
293
				} else {
294
					$height = $width * $ratio;
295
				}
296
			}
297
		}
298
299
		if ($height !== $maxHeight && $width !== $maxWidth) {
300
			/*
301
			 * Scale to the nearest power of two
302
			 */
303
			$pow2height = 2 ** ceil(log($height) / log(2));
304
			$pow2width = 2 ** ceil(log($width) / log(2));
305
306
			$ratioH = $height / $pow2height;
307
			$ratioW = $width / $pow2width;
308
309
			if ($ratioH < $ratioW) {
310
				$width = $pow2width;
311
				$height /= $ratioW;
312
			} else {
313
				$height = $pow2height;
314
				$width /= $ratioH;
315
			}
316
		}
317
318
		/*
319
 		 * Make sure the requested height and width fall within the max
320
 		 * of the preview.
321
 		 */
322
		if ($height > $maxHeight) {
323
			$ratio = $height / $maxHeight;
324
			$height = $maxHeight;
325
			$width /= $ratio;
326
		}
327
		if ($width > $maxWidth) {
328
			$ratio = $width / $maxWidth;
329
			$width = $maxWidth;
330
			$height /= $ratio;
331
		}
332
333
		return [(int)round($width), (int)round($height)];
334
	}
335
336
	/**
337
	 * @param ISimpleFolder $previewFolder
338
	 * @param ISimpleFile $maxPreview
339
	 * @param int $width
340
	 * @param int $height
341
	 * @param bool $crop
342
	 * @param int $maxWidth
343
	 * @param int $maxHeight
344
	 * @return ISimpleFile
345
	 * @throws NotFoundException
346
	 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
347
	 */
348
	private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
349
		$preview = $this->helper->getImage($maxPreview);
350
351
		if (!$preview->valid()) {
352
			throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
353
		}
354
355
		if ($crop) {
356
			if ($height !== $preview->height() && $width !== $preview->width()) {
357
				//Resize
358
				$widthR = $preview->width() / $width;
359
				$heightR = $preview->height() / $height;
360
361
				if ($widthR > $heightR) {
362
					$scaleH = $height;
363
					$scaleW = $maxWidth / $heightR;
364
				} else {
365
					$scaleH = $maxHeight / $widthR;
366
					$scaleW = $width;
367
				}
368
				$preview->preciseResize((int)round($scaleW), (int)round($scaleH));
369
			}
370
			$cropX = (int)floor(abs($width - $preview->width()) * 0.5);
371
			$cropY = 0;
372
			$preview->crop($cropX, $cropY, $width, $height);
373
		} else {
374
			$preview->resize(max($width, $height));
375
		}
376
377
378
		$path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
379
		try {
380
			$file = $previewFolder->newFile($path);
381
			$file->putContent($preview->data());
382
		} catch (NotPermittedException $e) {
383
			throw new NotFoundException();
384
		}
385
386
		return $file;
387
	}
388
389
	/**
390
	 * @param ISimpleFolder $previewFolder
391
	 * @param int $width
392
	 * @param int $height
393
	 * @param bool $crop
394
	 * @param string $mimeType
395
	 * @return ISimpleFile
396
	 *
397
	 * @throws NotFoundException
398
	 */
399
	private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) {
400
		$path = $this->generatePath($width, $height, $crop, $mimeType);
401
402
		return $previewFolder->getFile($path);
403
	}
404
405
	/**
406
	 * Get the specific preview folder for this file
407
	 *
408
	 * @param File $file
409
	 * @return ISimpleFolder
410
	 */
411
	private function getPreviewFolder(File $file) {
412
		try {
413
			$folder = $this->appData->getFolder($file->getId());
414
		} catch (NotFoundException $e) {
415
			$folder = $this->appData->newFolder($file->getId());
416
		}
417
418
		return $folder;
419
	}
420
421
	/**
422
	 * @param string $mimeType
423
	 * @return null|string
424
	 * @throws \InvalidArgumentException
425
	 */
426
	private function getExtention($mimeType) {
427
		switch ($mimeType) {
428
			case 'image/png':
429
				return 'png';
430
			case 'image/jpeg':
431
				return 'jpg';
432
			case 'image/gif':
433
				return 'gif';
434
			default:
435
				throw new \InvalidArgumentException('Not a valid mimetype');
436
		}
437
	}
438
}
439