Completed
Pull Request — master (#7756)
by Julius
26:36
created

Generator::getPreview()   C

Complexity

Conditions 9
Paths 22

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 29
nc 22
nop 6
dl 0
loc 51
rs 6.2727
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 View Code Duplication
	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
		$this->eventDispatcher->dispatch(
93
			IPreview::EVENT,
94
			new GenericEvent($file,[
95
				'width' => $width,
96
				'height' => $height,
97
				'crop' => $crop,
98
				'mode' => $mode
99
			])
100
		);
101
102
		if ($mimeType === null) {
103
			$mimeType = $file->getMimeType();
104
		}
105
		if (!$this->previewManager->isMimeSupported($mimeType)) {
106
			throw new NotFoundException();
107
		}
108
109
		$previewFolder = $this->getPreviewFolder($file);
110
111
		// Get the max preview and infer the max preview sizes from that
112
		$maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType);
113
		list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview);
114
115
		// If both width and heigth are -1 we just want the max preview
116
		if ($width === -1 && $height === -1) {
117
			$width = $maxWidth;
118
			$height = $maxHeight;
119
		}
120
121
		// Calculate the preview size
122
		list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);
123
124
		// No need to generate a preview that is just the max preview
125
		if ($width === $maxWidth && $height === $maxHeight) {
126
			return $maxPreview;
127
		}
128
129
		// Try to get a cached preview. Else generate (and store) one
130
		try {
131
			try {
132
				$file = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType());
133
			} catch (NotFoundException $e) {
134
				$file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
135
			}
136
		} catch (\InvalidArgumentException $e) {
137
			throw new NotFoundException();
138
		}
139
140
		return $file;
141
	}
142
143
	/**
144
	 * @param ISimpleFolder $previewFolder
145
	 * @param File $file
146
	 * @param string $mimeType
147
	 * @return ISimpleFile
148
	 * @throws NotFoundException
149
	 */
150
	private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) {
151
		$nodes = $previewFolder->getDirectoryListing();
152
153
		foreach ($nodes as $node) {
154
			if (strpos($node->getName(), 'max')) {
155
				return $node;
156
			}
157
		}
158
159
		$previewProviders = $this->previewManager->getProviders();
160
		foreach ($previewProviders as $supportedMimeType => $providers) {
161
			if (!preg_match($supportedMimeType, $mimeType)) {
162
				continue;
163
			}
164
165
			foreach ($providers as $provider) {
166
				$provider = $this->helper->getProvider($provider);
167
				if (!($provider instanceof IProvider)) {
168
					continue;
169
				}
170
171
				$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096);
172
				$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096);
173
174
				$preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
175
176
				if (!($preview instanceof IImage)) {
177
					continue;
178
				}
179
180
				// Try to get the extention.
181
				try {
182
					$ext = $this->getExtention($preview->dataMimeType());
183
				} catch (\InvalidArgumentException $e) {
184
					// Just continue to the next iteration if this preview doesn't have a valid mimetype
185
					continue;
186
				}
187
188
				$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
189
				try {
190
					$file = $previewFolder->newFile($path);
191
					$file->putContent($preview->data());
192
				} catch (NotPermittedException $e) {
193
					throw new NotFoundException();
194
				}
195
196
				return $file;
197
			}
198
		}
199
200
		throw new NotFoundException();
201
	}
202
203
	/**
204
	 * @param ISimpleFile $file
205
	 * @return int[]
206
	 */
207
	private function getPreviewSize(ISimpleFile $file) {
208
		$size = explode('-', $file->getName());
209
		return [(int)$size[0], (int)$size[1]];
210
	}
211
212
	/**
213
	 * @param int $width
214
	 * @param int $height
215
	 * @param bool $crop
216
	 * @param string $mimeType
217
	 * @return string
218
	 */
219
	private function generatePath($width, $height, $crop, $mimeType) {
220
		$path = (string)$width . '-' . (string)$height;
221
		if ($crop) {
222
			$path .= '-crop';
223
		}
224
225
		$ext = $this->getExtention($mimeType);
226
		$path .= '.' . $ext;
227
		return $path;
228
	}
229
230
231
232
	/**
233
	 * @param int $width
234
	 * @param int $height
235
	 * @param bool $crop
236
	 * @param string $mode
237
	 * @param int $maxWidth
238
	 * @param int $maxHeight
239
	 * @return int[]
240
	 */
241
	private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
242
243
		/*
244
		 * If we are not cropping we have to make sure the requested image
245
		 * respects the aspect ratio of the original.
246
		 */
247
		if (!$crop) {
248
			$ratio = $maxHeight / $maxWidth;
249
250
			if ($width === -1) {
251
				$width = $height / $ratio;
252
			}
253
			if ($height === -1) {
254
				$height = $width * $ratio;
255
			}
256
257
			$ratioH = $height / $maxHeight;
258
			$ratioW = $width / $maxWidth;
259
260
			/*
261
			 * Fill means that the $height and $width are the max
262
			 * Cover means min.
263
			 */
264
			if ($mode === IPreview::MODE_FILL) {
265
				if ($ratioH > $ratioW) {
266
					$height = $width * $ratio;
267
				} else {
268
					$width = $height / $ratio;
269
				}
270
			} else if ($mode === IPreview::MODE_COVER) {
271
				if ($ratioH > $ratioW) {
272
					$width = $height / $ratio;
273
				} else {
274
					$height = $width * $ratio;
275
				}
276
			}
277
		}
278
279
		if ($height !== $maxHeight && $width !== $maxWidth) {
280
			/*
281
			 * Scale to the nearest power of two
282
			 */
283
			$pow2height = 2 ** ceil(log($height) / log(2));
284
			$pow2width = 2 ** ceil(log($width) / log(2));
285
286
			$ratioH = $height / $pow2height;
287
			$ratioW = $width / $pow2width;
288
289
			if ($ratioH < $ratioW) {
290
				$width = $pow2width;
291
				$height /= $ratioW;
292
			} else {
293
				$height = $pow2height;
294
				$width /= $ratioH;
295
			}
296
		}
297
298
		/*
299
 		 * Make sure the requested height and width fall within the max
300
 		 * of the preview.
301
 		 */
302
		if ($height > $maxHeight) {
303
			$ratio = $height / $maxHeight;
304
			$height = $maxHeight;
305
			$width /= $ratio;
306
		}
307
		if ($width > $maxWidth) {
308
			$ratio = $width / $maxWidth;
309
			$width = $maxWidth;
310
			$height /= $ratio;
311
		}
312
313
		return [(int)round($width), (int)round($height)];
314
	}
315
316
	/**
317
	 * @param ISimpleFolder $previewFolder
318
	 * @param ISimpleFile $maxPreview
319
	 * @param int $width
320
	 * @param int $height
321
	 * @param bool $crop
322
	 * @param int $maxWidth
323
	 * @param int $maxHeight
324
	 * @return ISimpleFile
325
	 * @throws NotFoundException
326
	 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
327
	 */
328
	private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
329
		$preview = $this->helper->getImage($maxPreview);
330
331
		if (!$preview->valid()) {
332
			throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
333
		}
334
335
		if ($crop) {
336
			if ($height !== $preview->height() && $width !== $preview->width()) {
337
				//Resize
338
				$widthR = $preview->width() / $width;
339
				$heightR = $preview->height() / $height;
340
341
				if ($widthR > $heightR) {
342
					$scaleH = $height;
343
					$scaleW = $maxWidth / $heightR;
344
				} else {
345
					$scaleH = $maxHeight / $widthR;
346
					$scaleW = $width;
347
				}
348
				$preview->preciseResize(round($scaleW), round($scaleH));
349
			}
350
			$cropX = floor(abs($width - $preview->width()) * 0.5);
351
			$cropY = 0;
352
			$preview->crop($cropX, $cropY, $width, $height);
353
		} else {
354
			$preview->resize(max($width, $height));
355
		}
356
357
358
		$path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
359
		try {
360
			$file = $previewFolder->newFile($path);
361
			$file->putContent($preview->data());
362
		} catch (NotPermittedException $e) {
363
			throw new NotFoundException();
364
		}
365
366
		return $file;
367
	}
368
369
	/**
370
	 * @param ISimpleFolder $previewFolder
371
	 * @param int $width
372
	 * @param int $height
373
	 * @param bool $crop
374
	 * @param string $mimeType
375
	 * @return ISimpleFile
376
	 *
377
	 * @throws NotFoundException
378
	 */
379
	private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) {
380
		$path = $this->generatePath($width, $height, $crop, $mimeType);
381
382
		return $previewFolder->getFile($path);
383
	}
384
385
	/**
386
	 * Get the specific preview folder for this file
387
	 *
388
	 * @param File $file
389
	 * @return ISimpleFolder
390
	 */
391
	private function getPreviewFolder(File $file) {
392
		try {
393
			$folder = $this->appData->getFolder($file->getId());
394
		} catch (NotFoundException $e) {
395
			$folder = $this->appData->newFolder($file->getId());
396
		}
397
398
		return $folder;
399
	}
400
401
	/**
402
	 * @param string $mimeType
403
	 * @return null|string
404
	 * @throws \InvalidArgumentException
405
	 */
406
	private function getExtention($mimeType) {
407
		switch ($mimeType) {
408
			case 'image/png':
409
				return 'png';
410
			case 'image/jpeg':
411
				return 'jpg';
412
			case 'image/gif':
413
				return 'gif';
414
			default:
415
				throw new \InvalidArgumentException('Not a valid mimetype');
416
		}
417
	}
418
}
419