Completed
Pull Request — master (#9666)
by Morris
175:24 queued 156:08
created
lib/private/Preview/Generator.php 2 patches
Indentation   +377 added lines, -377 removed lines patch added patch discarded remove patch
@@ -40,395 +40,395 @@
 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
-				$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096);
188
-				$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096);
189
-
190
-				$preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
191
-
192
-				if (!($preview instanceof IImage)) {
193
-					continue;
194
-				}
195
-
196
-				// Try to get the extention.
197
-				try {
198
-					$ext = $this->getExtention($preview->dataMimeType());
199
-				} catch (\InvalidArgumentException $e) {
200
-					// Just continue to the next iteration if this preview doesn't have a valid mimetype
201
-					continue;
202
-				}
203
-
204
-				$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
205
-				try {
206
-					$file = $previewFolder->newFile($path);
207
-					$file->putContent($preview->data());
208
-				} catch (NotPermittedException $e) {
209
-					throw new NotFoundException();
210
-				}
211
-
212
-				return $file;
213
-			}
214
-		}
215
-
216
-		throw new NotFoundException();
217
-	}
218
-
219
-	/**
220
-	 * @param ISimpleFile $file
221
-	 * @return int[]
222
-	 */
223
-	private function getPreviewSize(ISimpleFile $file) {
224
-		$size = explode('-', $file->getName());
225
-		return [(int)$size[0], (int)$size[1]];
226
-	}
227
-
228
-	/**
229
-	 * @param int $width
230
-	 * @param int $height
231
-	 * @param bool $crop
232
-	 * @param string $mimeType
233
-	 * @return string
234
-	 */
235
-	private function generatePath($width, $height, $crop, $mimeType) {
236
-		$path = (string)$width . '-' . (string)$height;
237
-		if ($crop) {
238
-			$path .= '-crop';
239
-		}
240
-
241
-		$ext = $this->getExtention($mimeType);
242
-		$path .= '.' . $ext;
243
-		return $path;
244
-	}
245
-
246
-
247
-
248
-	/**
249
-	 * @param int $width
250
-	 * @param int $height
251
-	 * @param bool $crop
252
-	 * @param string $mode
253
-	 * @param int $maxWidth
254
-	 * @param int $maxHeight
255
-	 * @return int[]
256
-	 */
257
-	private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
258
-
259
-		/*
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
+                $maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096);
188
+                $maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096);
189
+
190
+                $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
191
+
192
+                if (!($preview instanceof IImage)) {
193
+                    continue;
194
+                }
195
+
196
+                // Try to get the extention.
197
+                try {
198
+                    $ext = $this->getExtention($preview->dataMimeType());
199
+                } catch (\InvalidArgumentException $e) {
200
+                    // Just continue to the next iteration if this preview doesn't have a valid mimetype
201
+                    continue;
202
+                }
203
+
204
+                $path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
205
+                try {
206
+                    $file = $previewFolder->newFile($path);
207
+                    $file->putContent($preview->data());
208
+                } catch (NotPermittedException $e) {
209
+                    throw new NotFoundException();
210
+                }
211
+
212
+                return $file;
213
+            }
214
+        }
215
+
216
+        throw new NotFoundException();
217
+    }
218
+
219
+    /**
220
+     * @param ISimpleFile $file
221
+     * @return int[]
222
+     */
223
+    private function getPreviewSize(ISimpleFile $file) {
224
+        $size = explode('-', $file->getName());
225
+        return [(int)$size[0], (int)$size[1]];
226
+    }
227
+
228
+    /**
229
+     * @param int $width
230
+     * @param int $height
231
+     * @param bool $crop
232
+     * @param string $mimeType
233
+     * @return string
234
+     */
235
+    private function generatePath($width, $height, $crop, $mimeType) {
236
+        $path = (string)$width . '-' . (string)$height;
237
+        if ($crop) {
238
+            $path .= '-crop';
239
+        }
240
+
241
+        $ext = $this->getExtention($mimeType);
242
+        $path .= '.' . $ext;
243
+        return $path;
244
+    }
245
+
246
+
247
+
248
+    /**
249
+     * @param int $width
250
+     * @param int $height
251
+     * @param bool $crop
252
+     * @param string $mode
253
+     * @param int $maxWidth
254
+     * @param int $maxHeight
255
+     * @return int[]
256
+     */
257
+    private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) {
258
+
259
+        /*
260 260
 		 * If we are not cropping we have to make sure the requested image
261 261
 		 * respects the aspect ratio of the original.
262 262
 		 */
263
-		if (!$crop) {
264
-			$ratio = $maxHeight / $maxWidth;
263
+        if (!$crop) {
264
+            $ratio = $maxHeight / $maxWidth;
265 265
 
266
-			if ($width === -1) {
267
-				$width = $height / $ratio;
268
-			}
269
-			if ($height === -1) {
270
-				$height = $width * $ratio;
271
-			}
266
+            if ($width === -1) {
267
+                $width = $height / $ratio;
268
+            }
269
+            if ($height === -1) {
270
+                $height = $width * $ratio;
271
+            }
272 272
 
273
-			$ratioH = $height / $maxHeight;
274
-			$ratioW = $width / $maxWidth;
273
+            $ratioH = $height / $maxHeight;
274
+            $ratioW = $width / $maxWidth;
275 275
 
276
-			/*
276
+            /*
277 277
 			 * Fill means that the $height and $width are the max
278 278
 			 * Cover means min.
279 279
 			 */
280
-			if ($mode === IPreview::MODE_FILL) {
281
-				if ($ratioH > $ratioW) {
282
-					$height = $width * $ratio;
283
-				} else {
284
-					$width = $height / $ratio;
285
-				}
286
-			} else if ($mode === IPreview::MODE_COVER) {
287
-				if ($ratioH > $ratioW) {
288
-					$width = $height / $ratio;
289
-				} else {
290
-					$height = $width * $ratio;
291
-				}
292
-			}
293
-		}
294
-
295
-		if ($height !== $maxHeight && $width !== $maxWidth) {
296
-			/*
280
+            if ($mode === IPreview::MODE_FILL) {
281
+                if ($ratioH > $ratioW) {
282
+                    $height = $width * $ratio;
283
+                } else {
284
+                    $width = $height / $ratio;
285
+                }
286
+            } else if ($mode === IPreview::MODE_COVER) {
287
+                if ($ratioH > $ratioW) {
288
+                    $width = $height / $ratio;
289
+                } else {
290
+                    $height = $width * $ratio;
291
+                }
292
+            }
293
+        }
294
+
295
+        if ($height !== $maxHeight && $width !== $maxWidth) {
296
+            /*
297 297
 			 * Scale to the nearest power of two
298 298
 			 */
299
-			$pow2height = 2 ** ceil(log($height) / log(2));
300
-			$pow2width = 2 ** ceil(log($width) / log(2));
301
-
302
-			$ratioH = $height / $pow2height;
303
-			$ratioW = $width / $pow2width;
304
-
305
-			if ($ratioH < $ratioW) {
306
-				$width = $pow2width;
307
-				$height /= $ratioW;
308
-			} else {
309
-				$height = $pow2height;
310
-				$width /= $ratioH;
311
-			}
312
-		}
313
-
314
-		/*
299
+            $pow2height = 2 ** ceil(log($height) / log(2));
300
+            $pow2width = 2 ** ceil(log($width) / log(2));
301
+
302
+            $ratioH = $height / $pow2height;
303
+            $ratioW = $width / $pow2width;
304
+
305
+            if ($ratioH < $ratioW) {
306
+                $width = $pow2width;
307
+                $height /= $ratioW;
308
+            } else {
309
+                $height = $pow2height;
310
+                $width /= $ratioH;
311
+            }
312
+        }
313
+
314
+        /*
315 315
  		 * Make sure the requested height and width fall within the max
316 316
  		 * of the preview.
317 317
  		 */
318
-		if ($height > $maxHeight) {
319
-			$ratio = $height / $maxHeight;
320
-			$height = $maxHeight;
321
-			$width /= $ratio;
322
-		}
323
-		if ($width > $maxWidth) {
324
-			$ratio = $width / $maxWidth;
325
-			$width = $maxWidth;
326
-			$height /= $ratio;
327
-		}
328
-
329
-		return [(int)round($width), (int)round($height)];
330
-	}
331
-
332
-	/**
333
-	 * @param ISimpleFolder $previewFolder
334
-	 * @param ISimpleFile $maxPreview
335
-	 * @param int $width
336
-	 * @param int $height
337
-	 * @param bool $crop
338
-	 * @param int $maxWidth
339
-	 * @param int $maxHeight
340
-	 * @return ISimpleFile
341
-	 * @throws NotFoundException
342
-	 * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
343
-	 */
344
-	private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
345
-		$preview = $this->helper->getImage($maxPreview);
346
-
347
-		if (!$preview->valid()) {
348
-			throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
349
-		}
350
-
351
-		if ($crop) {
352
-			if ($height !== $preview->height() && $width !== $preview->width()) {
353
-				//Resize
354
-				$widthR = $preview->width() / $width;
355
-				$heightR = $preview->height() / $height;
356
-
357
-				if ($widthR > $heightR) {
358
-					$scaleH = $height;
359
-					$scaleW = $maxWidth / $heightR;
360
-				} else {
361
-					$scaleH = $maxHeight / $widthR;
362
-					$scaleW = $width;
363
-				}
364
-				$preview->preciseResize((int)round($scaleW), (int)round($scaleH));
365
-			}
366
-			$cropX = (int)floor(abs($width - $preview->width()) * 0.5);
367
-			$cropY = 0;
368
-			$preview->crop($cropX, $cropY, $width, $height);
369
-		} else {
370
-			$preview->resize(max($width, $height));
371
-		}
372
-
373
-
374
-		$path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
375
-		try {
376
-			$file = $previewFolder->newFile($path);
377
-			$file->putContent($preview->data());
378
-		} catch (NotPermittedException $e) {
379
-			throw new NotFoundException();
380
-		}
381
-
382
-		return $file;
383
-	}
384
-
385
-	/**
386
-	 * @param ISimpleFolder $previewFolder
387
-	 * @param int $width
388
-	 * @param int $height
389
-	 * @param bool $crop
390
-	 * @param string $mimeType
391
-	 * @return ISimpleFile
392
-	 *
393
-	 * @throws NotFoundException
394
-	 */
395
-	private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) {
396
-		$path = $this->generatePath($width, $height, $crop, $mimeType);
397
-
398
-		return $previewFolder->getFile($path);
399
-	}
400
-
401
-	/**
402
-	 * Get the specific preview folder for this file
403
-	 *
404
-	 * @param File $file
405
-	 * @return ISimpleFolder
406
-	 */
407
-	private function getPreviewFolder(File $file) {
408
-		try {
409
-			$folder = $this->appData->getFolder($file->getId());
410
-		} catch (NotFoundException $e) {
411
-			$folder = $this->appData->newFolder($file->getId());
412
-		}
413
-
414
-		return $folder;
415
-	}
416
-
417
-	/**
418
-	 * @param string $mimeType
419
-	 * @return null|string
420
-	 * @throws \InvalidArgumentException
421
-	 */
422
-	private function getExtention($mimeType) {
423
-		switch ($mimeType) {
424
-			case 'image/png':
425
-				return 'png';
426
-			case 'image/jpeg':
427
-				return 'jpg';
428
-			case 'image/gif':
429
-				return 'gif';
430
-			default:
431
-				throw new \InvalidArgumentException('Not a valid mimetype');
432
-		}
433
-	}
318
+        if ($height > $maxHeight) {
319
+            $ratio = $height / $maxHeight;
320
+            $height = $maxHeight;
321
+            $width /= $ratio;
322
+        }
323
+        if ($width > $maxWidth) {
324
+            $ratio = $width / $maxWidth;
325
+            $width = $maxWidth;
326
+            $height /= $ratio;
327
+        }
328
+
329
+        return [(int)round($width), (int)round($height)];
330
+    }
331
+
332
+    /**
333
+     * @param ISimpleFolder $previewFolder
334
+     * @param ISimpleFile $maxPreview
335
+     * @param int $width
336
+     * @param int $height
337
+     * @param bool $crop
338
+     * @param int $maxWidth
339
+     * @param int $maxHeight
340
+     * @return ISimpleFile
341
+     * @throws NotFoundException
342
+     * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
343
+     */
344
+    private function generatePreview(ISimpleFolder $previewFolder, ISimpleFile $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight) {
345
+        $preview = $this->helper->getImage($maxPreview);
346
+
347
+        if (!$preview->valid()) {
348
+            throw new \InvalidArgumentException('Failed to generate preview, failed to load image');
349
+        }
350
+
351
+        if ($crop) {
352
+            if ($height !== $preview->height() && $width !== $preview->width()) {
353
+                //Resize
354
+                $widthR = $preview->width() / $width;
355
+                $heightR = $preview->height() / $height;
356
+
357
+                if ($widthR > $heightR) {
358
+                    $scaleH = $height;
359
+                    $scaleW = $maxWidth / $heightR;
360
+                } else {
361
+                    $scaleH = $maxHeight / $widthR;
362
+                    $scaleW = $width;
363
+                }
364
+                $preview->preciseResize((int)round($scaleW), (int)round($scaleH));
365
+            }
366
+            $cropX = (int)floor(abs($width - $preview->width()) * 0.5);
367
+            $cropY = 0;
368
+            $preview->crop($cropX, $cropY, $width, $height);
369
+        } else {
370
+            $preview->resize(max($width, $height));
371
+        }
372
+
373
+
374
+        $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType());
375
+        try {
376
+            $file = $previewFolder->newFile($path);
377
+            $file->putContent($preview->data());
378
+        } catch (NotPermittedException $e) {
379
+            throw new NotFoundException();
380
+        }
381
+
382
+        return $file;
383
+    }
384
+
385
+    /**
386
+     * @param ISimpleFolder $previewFolder
387
+     * @param int $width
388
+     * @param int $height
389
+     * @param bool $crop
390
+     * @param string $mimeType
391
+     * @return ISimpleFile
392
+     *
393
+     * @throws NotFoundException
394
+     */
395
+    private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType) {
396
+        $path = $this->generatePath($width, $height, $crop, $mimeType);
397
+
398
+        return $previewFolder->getFile($path);
399
+    }
400
+
401
+    /**
402
+     * Get the specific preview folder for this file
403
+     *
404
+     * @param File $file
405
+     * @return ISimpleFolder
406
+     */
407
+    private function getPreviewFolder(File $file) {
408
+        try {
409
+            $folder = $this->appData->getFolder($file->getId());
410
+        } catch (NotFoundException $e) {
411
+            $folder = $this->appData->newFolder($file->getId());
412
+        }
413
+
414
+        return $folder;
415
+    }
416
+
417
+    /**
418
+     * @param string $mimeType
419
+     * @return null|string
420
+     * @throws \InvalidArgumentException
421
+     */
422
+    private function getExtention($mimeType) {
423
+        switch ($mimeType) {
424
+            case 'image/png':
425
+                return 'png';
426
+            case 'image/jpeg':
427
+                return 'jpg';
428
+            case 'image/gif':
429
+                return 'gif';
430
+            default:
431
+                throw new \InvalidArgumentException('Not a valid mimetype');
432
+        }
433
+    }
434 434
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
 
98 98
 		$this->eventDispatcher->dispatch(
99 99
 			IPreview::EVENT,
100
-			new GenericEvent($file,[
100
+			new GenericEvent($file, [
101 101
 				'width' => $width,
102 102
 				'height' => $height,
103 103
 				'crop' => $crop,
@@ -184,8 +184,8 @@  discard block
 block discarded – undo
184 184
 					continue;
185 185
 				}
186 186
 
187
-				$maxWidth = (int)$this->config->getSystemValue('preview_max_x', 4096);
188
-				$maxHeight = (int)$this->config->getSystemValue('preview_max_y', 4096);
187
+				$maxWidth = (int) $this->config->getSystemValue('preview_max_x', 4096);
188
+				$maxHeight = (int) $this->config->getSystemValue('preview_max_y', 4096);
189 189
 
190 190
 				$preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight);
191 191
 
@@ -201,7 +201,7 @@  discard block
 block discarded – undo
201 201
 					continue;
202 202
 				}
203 203
 
204
-				$path = (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext;
204
+				$path = (string) $preview->width().'-'.(string) $preview->height().'-max.'.$ext;
205 205
 				try {
206 206
 					$file = $previewFolder->newFile($path);
207 207
 					$file->putContent($preview->data());
@@ -222,7 +222,7 @@  discard block
 block discarded – undo
222 222
 	 */
223 223
 	private function getPreviewSize(ISimpleFile $file) {
224 224
 		$size = explode('-', $file->getName());
225
-		return [(int)$size[0], (int)$size[1]];
225
+		return [(int) $size[0], (int) $size[1]];
226 226
 	}
227 227
 
228 228
 	/**
@@ -233,13 +233,13 @@  discard block
 block discarded – undo
233 233
 	 * @return string
234 234
 	 */
235 235
 	private function generatePath($width, $height, $crop, $mimeType) {
236
-		$path = (string)$width . '-' . (string)$height;
236
+		$path = (string) $width.'-'.(string) $height;
237 237
 		if ($crop) {
238 238
 			$path .= '-crop';
239 239
 		}
240 240
 
241 241
 		$ext = $this->getExtention($mimeType);
242
-		$path .= '.' . $ext;
242
+		$path .= '.'.$ext;
243 243
 		return $path;
244 244
 	}
245 245
 
@@ -326,7 +326,7 @@  discard block
 block discarded – undo
326 326
 			$height /= $ratio;
327 327
 		}
328 328
 
329
-		return [(int)round($width), (int)round($height)];
329
+		return [(int) round($width), (int) round($height)];
330 330
 	}
331 331
 
332 332
 	/**
@@ -361,9 +361,9 @@  discard block
 block discarded – undo
361 361
 					$scaleH = $maxHeight / $widthR;
362 362
 					$scaleW = $width;
363 363
 				}
364
-				$preview->preciseResize((int)round($scaleW), (int)round($scaleH));
364
+				$preview->preciseResize((int) round($scaleW), (int) round($scaleH));
365 365
 			}
366
-			$cropX = (int)floor(abs($width - $preview->width()) * 0.5);
366
+			$cropX = (int) floor(abs($width - $preview->width()) * 0.5);
367 367
 			$cropY = 0;
368 368
 			$preview->crop($cropX, $cropY, $width, $height);
369 369
 		} else {
Please login to merge, or discard this patch.