Passed
Push — master ( ed35bc...3ee759 )
by Roeland
12:59
created
lib/private/Preview/Generator.php 1 patch
Indentation   +385 added lines, -385 removed lines patch added patch discarded remove patch
@@ -40,403 +40,403 @@
 block discarded – undo
40 40
 
41 41
 class Generator {
42 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
-		/*
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 264
 		 * If we are not cropping we have to make sure the requested image
265 265
 		 * respects the aspect ratio of the original.
266 266
 		 */
267
-		if (!$crop) {
268
-			$ratio = $maxHeight / $maxWidth;
267
+        if (!$crop) {
268
+            $ratio = $maxHeight / $maxWidth;
269 269
 
270
-			if ($width === -1) {
271
-				$width = $height / $ratio;
272
-			}
273
-			if ($height === -1) {
274
-				$height = $width * $ratio;
275
-			}
270
+            if ($width === -1) {
271
+                $width = $height / $ratio;
272
+            }
273
+            if ($height === -1) {
274
+                $height = $width * $ratio;
275
+            }
276 276
 
277
-			$ratioH = $height / $maxHeight;
278
-			$ratioW = $width / $maxWidth;
277
+            $ratioH = $height / $maxHeight;
278
+            $ratioW = $width / $maxWidth;
279 279
 
280
-			/*
280
+            /*
281 281
 			 * Fill means that the $height and $width are the max
282 282
 			 * Cover means min.
283 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
-			/*
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 301
 			 * Scale to the nearest power of four
302 302
 			 */
303
-			$pow4height = 4 ** ceil(log($height) / log(4));
304
-			$pow4width = 4 ** ceil(log($width) / log(4));
305
-
306
-			// Minimum size is 64
307
-			$pow4height = max($pow4height, 64);
308
-			$pow4width = max($pow4width, 64);
309
-
310
-			$ratioH = $height / $pow4height;
311
-			$ratioW = $width / $pow4width;
312
-
313
-			if ($ratioH < $ratioW) {
314
-				$width = $pow4width;
315
-				$height /= $ratioW;
316
-			} else {
317
-				$height = $pow4height;
318
-				$width /= $ratioH;
319
-			}
320
-		}
321
-
322
-		/*
303
+            $pow4height = 4 ** ceil(log($height) / log(4));
304
+            $pow4width = 4 ** ceil(log($width) / log(4));
305
+
306
+            // Minimum size is 64
307
+            $pow4height = max($pow4height, 64);
308
+            $pow4width = max($pow4width, 64);
309
+
310
+            $ratioH = $height / $pow4height;
311
+            $ratioW = $width / $pow4width;
312
+
313
+            if ($ratioH < $ratioW) {
314
+                $width = $pow4width;
315
+                $height /= $ratioW;
316
+            } else {
317
+                $height = $pow4height;
318
+                $width /= $ratioH;
319
+            }
320
+        }
321
+
322
+        /*
323 323
  		 * Make sure the requested height and width fall within the max
324 324
  		 * of the preview.
325 325
  		 */
326
-		if ($height > $maxHeight) {
327
-			$ratio = $height / $maxHeight;
328
-			$height = $maxHeight;
329
-			$width /= $ratio;
330
-		}
331
-		if ($width > $maxWidth) {
332
-			$ratio = $width / $maxWidth;
333
-			$width = $maxWidth;
334
-			$height /= $ratio;
335
-		}
336
-
337
-		return [(int)round($width), (int)round($height)];
338
-	}
339
-
340
-	/**
341
-	 * @param ISimpleFolder $previewFolder
342
-	 * @param ISimpleFile $maxPreview
343
-	 * @param int $width
344
-	 * @param int $height
345
-	 * @param bool $crop
346
-	 * @param int $maxWidth
347
-	 * @param int $maxHeight
348
-	 * @return ISimpleFile
349
-	 * @throws NotFoundException
350
-	 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
351
-	 */
352
-	private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
353
-		$preview = $this->helper->getImage($maxPreview);
354
-
355
-		if (!$preview->valid()) {
356
-			throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
357
-		}
358
-
359
-		if ($crop) {
360
-			if ($height !== $preview->height() && $width !== $preview->width()) {
361
-				//Resize
362
-				$widthR = $preview->width() / $width;
363
-				$heightR = $preview->height() / $height;
364
-
365
-				if ($widthR > $heightR) {
366
-					$scaleH = $height;
367
-					$scaleW = $maxWidth / $heightR;
368
-				} else {
369
-					$scaleH = $maxHeight / $widthR;
370
-					$scaleW = $width;
371
-				}
372
-				$preview->preciseResize((int)round($scaleW), (int)round($scaleH));
373
-			}
374
-			$cropX = (int)floor(abs($width - $preview->width()) * 0.5);
375
-			$cropY = 0;
376
-			$preview->crop($cropX, $cropY, $width, $height);
377
-		} else {
378
-			$preview->resize(max($width, $height));
379
-		}
380
-
381
-
382
-		$path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
383
-		try {
384
-			$file = $previewFolder->newFile($path);
385
-			$file->putContent($preview->data());
386
-		} catch (NotPermittedException $e) {
387
-			throw new NotFoundException();
388
-		}
389
-
390
-		return $file;
391
-	}
392
-
393
-	/**
394
-	 * @param ISimpleFolder $previewFolder
395
-	 * @param int $width
396
-	 * @param int $height
397
-	 * @param bool $crop
398
-	 * @param string $mimeType
399
-	 * @return ISimpleFile
400
-	 *
401
-	 * @throws NotFoundException
402
-	 */
403
-	private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) {
404
-		$path = $this->generatePath($width, $height, $crop, $mimeType);
405
-
406
-		return $previewFolder->getFile($path);
407
-	}
408
-
409
-	/**
410
-	 * Get the specific preview folder for this file
411
-	 *
412
-	 * @param File $file
413
-	 * @return ISimpleFolder
414
-	 */
415
-	private function getPreviewFolder(File $file) {
416
-		try {
417
-			$folder = $this->appData->getFolder($file->getId());
418
-		} catch (NotFoundException $e) {
419
-			$folder = $this->appData->newFolder($file->getId());
420
-		}
421
-
422
-		return $folder;
423
-	}
424
-
425
-	/**
426
-	 * @param string $mimeType
427
-	 * @return null|string
428
-	 * @throws \InvalidArgumentException
429
-	 */
430
-	private function getExtention($mimeType) {
431
-		switch ($mimeType) {
432
-			case 'image/png':
433
-				return 'png';
434
-			case 'image/jpeg':
435
-				return 'jpg';
436
-			case 'image/gif':
437
-				return 'gif';
438
-			default:
439
-				throw new \InvalidArgumentException('Not a valid mimetype');
440
-		}
441
-	}
326
+        if ($height > $maxHeight) {
327
+            $ratio = $height / $maxHeight;
328
+            $height = $maxHeight;
329
+            $width /= $ratio;
330
+        }
331
+        if ($width > $maxWidth) {
332
+            $ratio = $width / $maxWidth;
333
+            $width = $maxWidth;
334
+            $height /= $ratio;
335
+        }
336
+
337
+        return [(int)round($width), (int)round($height)];
338
+    }
339
+
340
+    /**
341
+     * @param ISimpleFolder $previewFolder
342
+     * @param ISimpleFile $maxPreview
343
+     * @param int $width
344
+     * @param int $height
345
+     * @param bool $crop
346
+     * @param int $maxWidth
347
+     * @param int $maxHeight
348
+     * @return ISimpleFile
349
+     * @throws NotFoundException
350
+     * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
351
+     */
352
+    private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
353
+        $preview = $this->helper->getImage($maxPreview);
354
+
355
+        if (!$preview->valid()) {
356
+            throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
357
+        }
358
+
359
+        if ($crop) {
360
+            if ($height !== $preview->height() && $width !== $preview->width()) {
361
+                //Resize
362
+                $widthR = $preview->width() / $width;
363
+                $heightR = $preview->height() / $height;
364
+
365
+                if ($widthR > $heightR) {
366
+                    $scaleH = $height;
367
+                    $scaleW = $maxWidth / $heightR;
368
+                } else {
369
+                    $scaleH = $maxHeight / $widthR;
370
+                    $scaleW = $width;
371
+                }
372
+                $preview->preciseResize((int)round($scaleW), (int)round($scaleH));
373
+            }
374
+            $cropX = (int)floor(abs($width - $preview->width()) * 0.5);
375
+            $cropY = 0;
376
+            $preview->crop($cropX, $cropY, $width, $height);
377
+        } else {
378
+            $preview->resize(max($width, $height));
379
+        }
380
+
381
+
382
+        $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
383
+        try {
384
+            $file = $previewFolder->newFile($path);
385
+            $file->putContent($preview->data());
386
+        } catch (NotPermittedException $e) {
387
+            throw new NotFoundException();
388
+        }
389
+
390
+        return $file;
391
+    }
392
+
393
+    /**
394
+     * @param ISimpleFolder $previewFolder
395
+     * @param int $width
396
+     * @param int $height
397
+     * @param bool $crop
398
+     * @param string $mimeType
399
+     * @return ISimpleFile
400
+     *
401
+     * @throws NotFoundException
402
+     */
403
+    private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) {
404
+        $path = $this->generatePath($width, $height, $crop, $mimeType);
405
+
406
+        return $previewFolder->getFile($path);
407
+    }
408
+
409
+    /**
410
+     * Get the specific preview folder for this file
411
+     *
412
+     * @param File $file
413
+     * @return ISimpleFolder
414
+     */
415
+    private function getPreviewFolder(File $file) {
416
+        try {
417
+            $folder = $this->appData->getFolder($file->getId());
418
+        } catch (NotFoundException $e) {
419
+            $folder = $this->appData->newFolder($file->getId());
420
+        }
421
+
422
+        return $folder;
423
+    }
424
+
425
+    /**
426
+     * @param string $mimeType
427
+     * @return null|string
428
+     * @throws \InvalidArgumentException
429
+     */
430
+    private function getExtention($mimeType) {
431
+        switch ($mimeType) {
432
+            case 'image/png':
433
+                return 'png';
434
+            case 'image/jpeg':
435
+                return 'jpg';
436
+            case 'image/gif':
437
+                return 'gif';
438
+            default:
439
+                throw new \InvalidArgumentException('Not a valid mimetype');
440
+        }
441
+    }
442 442
 }
Please login to merge, or discard this patch.