Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Generator.php « Preview « private « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 402732ecda9927a7b9b591682939a676480e02da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
304
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/**
 * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @author Morris Jobke <hey@morrisjobke.de>
 * @author Robin Appelman <robin@icewind.nl>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * 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
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OC\Preview;

use OCP\Files\File;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig;
use OCP\IImage;
use OCP\IPreview;
use OCP\Preview\IProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class Generator {

	/** @var IPreview */
	private $previewManager;
	/** @var IConfig */
	private $config;
	/** @var IAppData */
	private $appData;
	/** @var GeneratorHelper */
	private $helper;
	/** @var EventDispatcherInterface */
	private $eventDispatcher;

	/**
	 * @param IConfig $config
	 * @param IPreview $previewManager
	 * @param IAppData $appData
	 * @param GeneratorHelper $helper
	 * @param EventDispatcherInterface $eventDispatcher
	 */
	public function __construct(
		IConfig $config,
		IPreview $previewManager,
		IAppData $appData,
		GeneratorHelper $helper,
		EventDispatcherInterface $eventDispatcher
	) {
		$this->config = $config;
		$this->previewManager = $previewManager;
		$this->appData = $appData;
		$this->helper = $helper;
		$this->eventDispatcher = $eventDispatcher;
	}

	/**
	 * Returns a preview of a file
	 *
	 * The cache is searched first and if nothing usable was found then a preview is
	 * generated by one of the providers
	 *
	 * @param File $file
	 * @param int $width
	 * @param int $height
	 * @param bool $crop
	 * @param string $mode
	 * @param string $mimeType
	 * @return ISimpleFile
	 * @throws NotFoundException
	 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
	 */
	public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
		$this->eventDispatcher->dispatch(
			IPreview::EVENT,
			new GenericEvent($file,[
				'width' => $width,
				'height' => $height,
				'crop' => $crop,
				'mode' => $mode
			])
		);

		if ($mimeType === null) {
			$mimeType = $file->getMimeType();
		}
		if (!$this->previewManager->isMimeSupported($mimeType)) {
			throw new NotFoundException();
		}

		$previewFolder = $this->getPreviewFolder($file);

		// Get the max preview and infer the max preview sizes from that
		$maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType);
		list($maxWidth, $maxHeight) = $this->getPreviewSize($maxPreview);

		// If both width and heigth are -1 we just want the max preview
		if ($width === -1 && $height === -1) {
			$width = $maxWidth;
			$height = $maxHeight;
		}

		// Calculate the preview size
		list($width, $height) = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight);

		// No need to generate a preview that is just the max preview
		if ($width === $maxWidth && $height === $maxHeight) {
			return $maxPreview;
		}

		// Try to get a cached preview. Else generate (and store) one
		try {
			$file = $this->getCachedPreview($previewFolder, $width, $height, $crop);
		} catch (NotFoundException $e) {
			$file = $this->generatePreview($previewFolder, $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight);
		}

		return $file;
	}

	/**
	 * @param ISimpleFolder $previewFolder
	 * @param File $file
	 * @param string $mimeType
	 * @return ISimpleFile
	 * @throws NotFoundException
	 */
	private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType) {
		$nodes = $previewFolder->getDirectoryListing();

		foreach ($nodes as $node) {
			if (strpos($node->getName(), 'max')) {
				return $node;
			}
		}

		$previewProviders = $this->previewManager->getProviders();
		foreach ($previewProviders as $supportedMimeType => $providers) {
			if (!preg_match($supportedMimeType, $mimeType)) {
				continue;
			}

			foreach ($providers as $provider) {
				$provider = $this->helper->getProvider($provider);
				if (!($provider instanceof IProvider)) {
					continue;
				}

				$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 2048);
				$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 2048);

				$preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);

				if (!($preview instanceof IImage)) {
					continue;
				}

				$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.png';
				try {
					$file = $previewFolder->newFile($path);
					$file->putContent($preview->data());
				} catch (NotPermittedException $e) {
					throw new NotFoundException();
				}

				return $file;
			}
		}

		throw new NotFoundException();
	}

	/**
	 * @param ISimpleFile $file
	 * @return int[]
	 */
	private function getPreviewSize(ISimpleFile $file) {
		$size = explode('-', $file->getName());
		return [(int)$size[0], (int)$size[1]];
	}

	/**
	 * @param int $width
	 * @param int $height
	 * @param bool $crop
	 * @return string
	 */
	private function generatePath($width, $height, $crop) {
		$path = (string)$width . '-' . (string)$height;
		if ($crop) {
			$path .= '-crop';
		}
		$path .= '.png';
		return $path;
	}



	/**
	 * @param int $width
	 * @param int $height
	 * @param bool $crop
	 * @param string $mode
	 * @param int $maxWidth
	 * @param int $maxHeight
	 * @return int[]
	 */
	private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {

		/*
		 * If we are not cropping we have to make sure the requested image
		 * respects the aspect ratio of the original.
		 */
		if (!$crop) {
			$ratio = $maxHeight / $maxWidth;

			if ($width === -1) {
				$width = $height / $ratio;
			}
			if ($height === -1) {
				$height = $width * $ratio;
			}

			$ratioH = $height / $maxHeight;
			$ratioW = $width / $maxWidth;

			/*
			 * Fill means that the $height and $width are the max
			 * Cover means min.
			 */
			if ($mode === IPreview::MODE_FILL) {
				if ($ratioH > $ratioW) {
					$height = $width * $ratio;
				} else {
					$width = $height / $ratio;
				}
			} else if ($mode === IPreview::MODE_COVER) {
				if ($ratioH > $ratioW) {
					$width = $height / $ratio;
				} else {
					$height = $width * $ratio;
				}
			}
		}

		if ($height !== $maxHeight && $width !== $maxWidth) {
			/*
			 * Scale to the nearest power of two
			 */
			$pow2height = 2 ** ceil(log($height) / log(2));
			$pow2width = 2 ** ceil(log($width) / log(2));

			$ratioH = $height / $pow2height;
			$ratioW = $width / $pow2width;

			if ($ratioH < $ratioW) {
				$width = $pow2width;
				$height /= $ratioW;
			} else {
				$height = $pow2height;
				$width /= $ratioH;
			}
		}

		/*
 		 * Make sure the requested height and width fall within the max
 		 * of the preview.
 		 */
		if ($height > $maxHeight) {
			$ratio = $height / $maxHeight;
			$height = $maxHeight;
			$width /= $ratio;
		}
		if ($width > $maxWidth) {
			$ratio = $width / $maxWidth;
			$width = $maxWidth;
			$height /= $ratio;
		}

		return [(int)round($width), (int)round($height)];
	}

	/**
	 * @param ISimpleFolder $previewFolder
	 * @param ISimpleFile $maxPreview
	 * @param int $width
	 * @param int $height
	 * @param bool $crop
	 * @param int $maxWidth
	 * @param int $maxHeight
	 * @return ISimpleFile
	 * @throws NotFoundException
	 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
	 */
	private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
		$preview = $this->helper->getImage($maxPreview);

		if (!$preview->valid()) {
			throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
		}

		if ($crop) {
			if ($height !== $preview->height() && $width !== $preview->width()) {
				//Resize
				$widthR = $preview->width() / $width;
				$heightR = $preview->height() / $height;

				if ($widthR > $heightR) {
					$scaleH = $height;
					$scaleW = $maxWidth / $heightR;
				} else {
					$scaleH = $maxHeight / $widthR;
					$scaleW = $width;
				}
				$preview->preciseResize(round($scaleW), round($scaleH));
			}
			$cropX = floor(abs($width - $preview->width()) * 0.5);
			$cropY = 0;
			$preview->crop($cropX, $cropY, $width, $height);
		} else {
			$preview->resize(max($width, $height));
		}


		$path = $this->generatePath($width, $height, $crop);
		try {
			$file = $previewFolder->newFile($path);
			$file->putContent($preview->data());
		} catch (NotPermittedException $e) {
			throw new NotFoundException();
		}

		return $file;
	}

	/**
	 * @param ISimpleFolder $previewFolder
	 * @param int $width
	 * @param int $height
	 * @param bool $crop
	 * @return ISimpleFile
	 *
	 * @throws NotFoundException
	 */
	private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop) {
		$path = $this->generatePath($width, $height, $crop);

		return $previewFolder->getFile($path);
	}

	/**
	 * Get the specific preview folder for this file
	 *
	 * @param File $file
	 * @return ISimpleFolder
	 */
	private function getPreviewFolder(File $file) {
		try {
			$folder = $this->appData->getFolder($file->getId());
		} catch (NotFoundException $e) {
			$folder = $this->appData->newFolder($file->getId());
		}

		return $folder;
	}
}