Passed
Push — master ( 2bedbc...b43e21 )
by Roeland
13:17 queued 11s
created
core/Controller/AvatarController.php 1 patch
Indentation   +282 added lines, -282 removed lines patch added patch discarded remove patch
@@ -53,286 +53,286 @@
 block discarded – undo
53 53
  */
54 54
 class AvatarController extends Controller {
55 55
 
56
-	/** @var IAvatarManager */
57
-	protected $avatarManager;
58
-
59
-	/** @var ICache */
60
-	protected $cache;
61
-
62
-	/** @var IL10N */
63
-	protected $l;
64
-
65
-	/** @var IUserManager */
66
-	protected $userManager;
67
-
68
-	/** @var IUserSession */
69
-	protected $userSession;
70
-
71
-	/** @var IRootFolder */
72
-	protected $rootFolder;
73
-
74
-	/** @var ILogger */
75
-	protected $logger;
76
-
77
-	/** @var string */
78
-	protected $userId;
79
-
80
-	/** @var TimeFactory */
81
-	protected $timeFactory;
82
-
83
-	public function __construct($appName,
84
-								IRequest $request,
85
-								IAvatarManager $avatarManager,
86
-								ICache $cache,
87
-								IL10N $l10n,
88
-								IUserManager $userManager,
89
-								IRootFolder $rootFolder,
90
-								ILogger $logger,
91
-								$userId,
92
-								TimeFactory $timeFactory) {
93
-		parent::__construct($appName, $request);
94
-
95
-		$this->avatarManager = $avatarManager;
96
-		$this->cache = $cache;
97
-		$this->l = $l10n;
98
-		$this->userManager = $userManager;
99
-		$this->rootFolder = $rootFolder;
100
-		$this->logger = $logger;
101
-		$this->userId = $userId;
102
-		$this->timeFactory = $timeFactory;
103
-	}
104
-
105
-
106
-	/**
107
-	 * @NoAdminRequired
108
-	 * @NoCSRFRequired
109
-	 * @NoSameSiteCookieRequired
110
-	 * @PublicPage
111
-	 *
112
-	 * @param string $userId
113
-	 * @param int $size
114
-	 * @return JSONResponse|FileDisplayResponse
115
-	 */
116
-	public function getAvatar($userId, $size) {
117
-		// min/max size
118
-		if ($size > 2048) {
119
-			$size = 2048;
120
-		} elseif ($size <= 0) {
121
-			$size = 64;
122
-		}
123
-
124
-		try {
125
-			$avatar = $this->avatarManager->getAvatar($userId);
126
-			$avatarFile = $avatar->getFile($size);
127
-			$response = new FileDisplayResponse(
128
-				$avatarFile,
129
-				Http::STATUS_OK,
130
-				['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()]
131
-			);
132
-		} catch (\Exception $e) {
133
-			return new JSONResponse([], Http::STATUS_NOT_FOUND);
134
-		}
135
-
136
-		// Cache for 1 day
137
-		$response->cacheFor(60 * 60 * 24);
138
-		return $response;
139
-	}
140
-
141
-	/**
142
-	 * @NoAdminRequired
143
-	 *
144
-	 * @param string $path
145
-	 * @return JSONResponse
146
-	 */
147
-	public function postAvatar($path) {
148
-		$files = $this->request->getUploadedFile('files');
149
-
150
-		if (isset($path)) {
151
-			$path = stripslashes($path);
152
-			$userFolder = $this->rootFolder->getUserFolder($this->userId);
153
-			/** @var File $node */
154
-			$node = $userFolder->get($path);
155
-			if (!($node instanceof File)) {
156
-				return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]);
157
-			}
158
-			if ($node->getSize() > 20 * 1024 * 1024) {
159
-				return new JSONResponse(
160
-					['data' => ['message' => $this->l->t('File is too big')]],
161
-					Http::STATUS_BAD_REQUEST
162
-				);
163
-			}
164
-
165
-			if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') {
166
-				return new JSONResponse(
167
-					['data' => ['message' => $this->l->t('The selected file is not an image.')]],
168
-					Http::STATUS_BAD_REQUEST
169
-				);
170
-			}
171
-
172
-			try {
173
-				$content = $node->getContent();
174
-			} catch (\OCP\Files\NotPermittedException $e) {
175
-				return new JSONResponse(
176
-					['data' => ['message' => $this->l->t('The selected file cannot be read.')]],
177
-					Http::STATUS_BAD_REQUEST
178
-				);
179
-			}
180
-		} elseif (!is_null($files)) {
181
-			if (
182
-				$files['error'][0] === 0 &&
183
-				 is_uploaded_file($files['tmp_name'][0]) &&
184
-				!\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
185
-			) {
186
-				if ($files['size'][0] > 20 * 1024 * 1024) {
187
-					return new JSONResponse(
188
-						['data' => ['message' => $this->l->t('File is too big')]],
189
-						Http::STATUS_BAD_REQUEST
190
-					);
191
-				}
192
-				$this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
193
-				$content = $this->cache->get('avatar_upload');
194
-				unlink($files['tmp_name'][0]);
195
-			} else {
196
-				$phpFileUploadErrors = [
197
-					UPLOAD_ERR_OK => $this->l->t('The file was uploaded'),
198
-					UPLOAD_ERR_INI_SIZE => $this->l->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
199
-					UPLOAD_ERR_FORM_SIZE => $this->l->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
200
-					UPLOAD_ERR_PARTIAL => $this->l->t('The file was only partially uploaded'),
201
-					UPLOAD_ERR_NO_FILE => $this->l->t('No file was uploaded'),
202
-					UPLOAD_ERR_NO_TMP_DIR => $this->l->t('Missing a temporary folder'),
203
-					UPLOAD_ERR_CANT_WRITE => $this->l->t('Could not write file to disk'),
204
-					UPLOAD_ERR_EXTENSION => $this->l->t('A PHP extension stopped the file upload'),
205
-				];
206
-				$message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l->t('Invalid file provided');
207
-				$this->logger->warning($message, ['app' => 'core']);
208
-				return new JSONResponse(
209
-					['data' => ['message' => $message]],
210
-					Http::STATUS_BAD_REQUEST
211
-				);
212
-			}
213
-		} else {
214
-			//Add imgfile
215
-			return new JSONResponse(
216
-				['data' => ['message' => $this->l->t('No image or file provided')]],
217
-				Http::STATUS_BAD_REQUEST
218
-			);
219
-		}
220
-
221
-		try {
222
-			$image = new \OC_Image();
223
-			$image->loadFromData($content);
224
-			$image->readExif($content);
225
-			$image->fixOrientation();
226
-
227
-			if ($image->valid()) {
228
-				$mimeType = $image->mimeType();
229
-				if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
230
-					return new JSONResponse(
231
-						['data' => ['message' => $this->l->t('Unknown filetype')]],
232
-						Http::STATUS_OK
233
-					);
234
-				}
235
-
236
-				$this->cache->set('tmpAvatar', $image->data(), 7200);
237
-				return new JSONResponse(
238
-					['data' => 'notsquare'],
239
-					Http::STATUS_OK
240
-				);
241
-			} else {
242
-				return new JSONResponse(
243
-					['data' => ['message' => $this->l->t('Invalid image')]],
244
-					Http::STATUS_OK
245
-				);
246
-			}
247
-		} catch (\Exception $e) {
248
-			$this->logger->logException($e, ['app' => 'core']);
249
-			return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK);
250
-		}
251
-	}
252
-
253
-	/**
254
-	 * @NoAdminRequired
255
-	 *
256
-	 * @return JSONResponse
257
-	 */
258
-	public function deleteAvatar() {
259
-		try {
260
-			$avatar = $this->avatarManager->getAvatar($this->userId);
261
-			$avatar->remove();
262
-			return new JSONResponse();
263
-		} catch (\Exception $e) {
264
-			$this->logger->logException($e, ['app' => 'core']);
265
-			return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
266
-		}
267
-	}
268
-
269
-	/**
270
-	 * @NoAdminRequired
271
-	 *
272
-	 * @return JSONResponse|DataDisplayResponse
273
-	 */
274
-	public function getTmpAvatar() {
275
-		$tmpAvatar = $this->cache->get('tmpAvatar');
276
-		if (is_null($tmpAvatar)) {
277
-			return new JSONResponse(['data' => [
278
-				'message' => $this->l->t("No temporary profile picture available, try again")
279
-			]],
280
-									Http::STATUS_NOT_FOUND);
281
-		}
282
-
283
-		$image = new \OC_Image();
284
-		$image->loadFromData($tmpAvatar);
285
-
286
-		$resp = new DataDisplayResponse($image->data(),
287
-				Http::STATUS_OK,
288
-				['Content-Type' => $image->mimeType()]);
289
-
290
-		$resp->setETag((string)crc32($image->data()));
291
-		$resp->cacheFor(0);
292
-		$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
293
-		return $resp;
294
-	}
295
-
296
-	/**
297
-	 * @NoAdminRequired
298
-	 *
299
-	 * @param array $crop
300
-	 * @return JSONResponse
301
-	 */
302
-	public function postCroppedAvatar($crop) {
303
-		if (is_null($crop)) {
304
-			return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]],
305
-									Http::STATUS_BAD_REQUEST);
306
-		}
307
-
308
-		if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) {
309
-			return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]],
310
-									Http::STATUS_BAD_REQUEST);
311
-		}
312
-
313
-		$tmpAvatar = $this->cache->get('tmpAvatar');
314
-		if (is_null($tmpAvatar)) {
315
-			return new JSONResponse(['data' => [
316
-				'message' => $this->l->t("No temporary profile picture available, try again")
317
-			]],
318
-									Http::STATUS_BAD_REQUEST);
319
-		}
320
-
321
-		$image = new \OC_Image();
322
-		$image->loadFromData($tmpAvatar);
323
-		$image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h']));
324
-		try {
325
-			$avatar = $this->avatarManager->getAvatar($this->userId);
326
-			$avatar->set($image);
327
-			// Clean up
328
-			$this->cache->remove('tmpAvatar');
329
-			return new JSONResponse(['status' => 'success']);
330
-		} catch (\OC\NotSquareException $e) {
331
-			return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]],
332
-									Http::STATUS_BAD_REQUEST);
333
-		} catch (\Exception $e) {
334
-			$this->logger->logException($e, ['app' => 'core']);
335
-			return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
336
-		}
337
-	}
56
+    /** @var IAvatarManager */
57
+    protected $avatarManager;
58
+
59
+    /** @var ICache */
60
+    protected $cache;
61
+
62
+    /** @var IL10N */
63
+    protected $l;
64
+
65
+    /** @var IUserManager */
66
+    protected $userManager;
67
+
68
+    /** @var IUserSession */
69
+    protected $userSession;
70
+
71
+    /** @var IRootFolder */
72
+    protected $rootFolder;
73
+
74
+    /** @var ILogger */
75
+    protected $logger;
76
+
77
+    /** @var string */
78
+    protected $userId;
79
+
80
+    /** @var TimeFactory */
81
+    protected $timeFactory;
82
+
83
+    public function __construct($appName,
84
+                                IRequest $request,
85
+                                IAvatarManager $avatarManager,
86
+                                ICache $cache,
87
+                                IL10N $l10n,
88
+                                IUserManager $userManager,
89
+                                IRootFolder $rootFolder,
90
+                                ILogger $logger,
91
+                                $userId,
92
+                                TimeFactory $timeFactory) {
93
+        parent::__construct($appName, $request);
94
+
95
+        $this->avatarManager = $avatarManager;
96
+        $this->cache = $cache;
97
+        $this->l = $l10n;
98
+        $this->userManager = $userManager;
99
+        $this->rootFolder = $rootFolder;
100
+        $this->logger = $logger;
101
+        $this->userId = $userId;
102
+        $this->timeFactory = $timeFactory;
103
+    }
104
+
105
+
106
+    /**
107
+     * @NoAdminRequired
108
+     * @NoCSRFRequired
109
+     * @NoSameSiteCookieRequired
110
+     * @PublicPage
111
+     *
112
+     * @param string $userId
113
+     * @param int $size
114
+     * @return JSONResponse|FileDisplayResponse
115
+     */
116
+    public function getAvatar($userId, $size) {
117
+        // min/max size
118
+        if ($size > 2048) {
119
+            $size = 2048;
120
+        } elseif ($size <= 0) {
121
+            $size = 64;
122
+        }
123
+
124
+        try {
125
+            $avatar = $this->avatarManager->getAvatar($userId);
126
+            $avatarFile = $avatar->getFile($size);
127
+            $response = new FileDisplayResponse(
128
+                $avatarFile,
129
+                Http::STATUS_OK,
130
+                ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()]
131
+            );
132
+        } catch (\Exception $e) {
133
+            return new JSONResponse([], Http::STATUS_NOT_FOUND);
134
+        }
135
+
136
+        // Cache for 1 day
137
+        $response->cacheFor(60 * 60 * 24);
138
+        return $response;
139
+    }
140
+
141
+    /**
142
+     * @NoAdminRequired
143
+     *
144
+     * @param string $path
145
+     * @return JSONResponse
146
+     */
147
+    public function postAvatar($path) {
148
+        $files = $this->request->getUploadedFile('files');
149
+
150
+        if (isset($path)) {
151
+            $path = stripslashes($path);
152
+            $userFolder = $this->rootFolder->getUserFolder($this->userId);
153
+            /** @var File $node */
154
+            $node = $userFolder->get($path);
155
+            if (!($node instanceof File)) {
156
+                return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]);
157
+            }
158
+            if ($node->getSize() > 20 * 1024 * 1024) {
159
+                return new JSONResponse(
160
+                    ['data' => ['message' => $this->l->t('File is too big')]],
161
+                    Http::STATUS_BAD_REQUEST
162
+                );
163
+            }
164
+
165
+            if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') {
166
+                return new JSONResponse(
167
+                    ['data' => ['message' => $this->l->t('The selected file is not an image.')]],
168
+                    Http::STATUS_BAD_REQUEST
169
+                );
170
+            }
171
+
172
+            try {
173
+                $content = $node->getContent();
174
+            } catch (\OCP\Files\NotPermittedException $e) {
175
+                return new JSONResponse(
176
+                    ['data' => ['message' => $this->l->t('The selected file cannot be read.')]],
177
+                    Http::STATUS_BAD_REQUEST
178
+                );
179
+            }
180
+        } elseif (!is_null($files)) {
181
+            if (
182
+                $files['error'][0] === 0 &&
183
+                 is_uploaded_file($files['tmp_name'][0]) &&
184
+                !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])
185
+            ) {
186
+                if ($files['size'][0] > 20 * 1024 * 1024) {
187
+                    return new JSONResponse(
188
+                        ['data' => ['message' => $this->l->t('File is too big')]],
189
+                        Http::STATUS_BAD_REQUEST
190
+                    );
191
+                }
192
+                $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
193
+                $content = $this->cache->get('avatar_upload');
194
+                unlink($files['tmp_name'][0]);
195
+            } else {
196
+                $phpFileUploadErrors = [
197
+                    UPLOAD_ERR_OK => $this->l->t('The file was uploaded'),
198
+                    UPLOAD_ERR_INI_SIZE => $this->l->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
199
+                    UPLOAD_ERR_FORM_SIZE => $this->l->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
200
+                    UPLOAD_ERR_PARTIAL => $this->l->t('The file was only partially uploaded'),
201
+                    UPLOAD_ERR_NO_FILE => $this->l->t('No file was uploaded'),
202
+                    UPLOAD_ERR_NO_TMP_DIR => $this->l->t('Missing a temporary folder'),
203
+                    UPLOAD_ERR_CANT_WRITE => $this->l->t('Could not write file to disk'),
204
+                    UPLOAD_ERR_EXTENSION => $this->l->t('A PHP extension stopped the file upload'),
205
+                ];
206
+                $message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l->t('Invalid file provided');
207
+                $this->logger->warning($message, ['app' => 'core']);
208
+                return new JSONResponse(
209
+                    ['data' => ['message' => $message]],
210
+                    Http::STATUS_BAD_REQUEST
211
+                );
212
+            }
213
+        } else {
214
+            //Add imgfile
215
+            return new JSONResponse(
216
+                ['data' => ['message' => $this->l->t('No image or file provided')]],
217
+                Http::STATUS_BAD_REQUEST
218
+            );
219
+        }
220
+
221
+        try {
222
+            $image = new \OC_Image();
223
+            $image->loadFromData($content);
224
+            $image->readExif($content);
225
+            $image->fixOrientation();
226
+
227
+            if ($image->valid()) {
228
+                $mimeType = $image->mimeType();
229
+                if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
230
+                    return new JSONResponse(
231
+                        ['data' => ['message' => $this->l->t('Unknown filetype')]],
232
+                        Http::STATUS_OK
233
+                    );
234
+                }
235
+
236
+                $this->cache->set('tmpAvatar', $image->data(), 7200);
237
+                return new JSONResponse(
238
+                    ['data' => 'notsquare'],
239
+                    Http::STATUS_OK
240
+                );
241
+            } else {
242
+                return new JSONResponse(
243
+                    ['data' => ['message' => $this->l->t('Invalid image')]],
244
+                    Http::STATUS_OK
245
+                );
246
+            }
247
+        } catch (\Exception $e) {
248
+            $this->logger->logException($e, ['app' => 'core']);
249
+            return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK);
250
+        }
251
+    }
252
+
253
+    /**
254
+     * @NoAdminRequired
255
+     *
256
+     * @return JSONResponse
257
+     */
258
+    public function deleteAvatar() {
259
+        try {
260
+            $avatar = $this->avatarManager->getAvatar($this->userId);
261
+            $avatar->remove();
262
+            return new JSONResponse();
263
+        } catch (\Exception $e) {
264
+            $this->logger->logException($e, ['app' => 'core']);
265
+            return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
266
+        }
267
+    }
268
+
269
+    /**
270
+     * @NoAdminRequired
271
+     *
272
+     * @return JSONResponse|DataDisplayResponse
273
+     */
274
+    public function getTmpAvatar() {
275
+        $tmpAvatar = $this->cache->get('tmpAvatar');
276
+        if (is_null($tmpAvatar)) {
277
+            return new JSONResponse(['data' => [
278
+                'message' => $this->l->t("No temporary profile picture available, try again")
279
+            ]],
280
+                                    Http::STATUS_NOT_FOUND);
281
+        }
282
+
283
+        $image = new \OC_Image();
284
+        $image->loadFromData($tmpAvatar);
285
+
286
+        $resp = new DataDisplayResponse($image->data(),
287
+                Http::STATUS_OK,
288
+                ['Content-Type' => $image->mimeType()]);
289
+
290
+        $resp->setETag((string)crc32($image->data()));
291
+        $resp->cacheFor(0);
292
+        $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
293
+        return $resp;
294
+    }
295
+
296
+    /**
297
+     * @NoAdminRequired
298
+     *
299
+     * @param array $crop
300
+     * @return JSONResponse
301
+     */
302
+    public function postCroppedAvatar($crop) {
303
+        if (is_null($crop)) {
304
+            return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]],
305
+                                    Http::STATUS_BAD_REQUEST);
306
+        }
307
+
308
+        if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) {
309
+            return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]],
310
+                                    Http::STATUS_BAD_REQUEST);
311
+        }
312
+
313
+        $tmpAvatar = $this->cache->get('tmpAvatar');
314
+        if (is_null($tmpAvatar)) {
315
+            return new JSONResponse(['data' => [
316
+                'message' => $this->l->t("No temporary profile picture available, try again")
317
+            ]],
318
+                                    Http::STATUS_BAD_REQUEST);
319
+        }
320
+
321
+        $image = new \OC_Image();
322
+        $image->loadFromData($tmpAvatar);
323
+        $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h']));
324
+        try {
325
+            $avatar = $this->avatarManager->getAvatar($this->userId);
326
+            $avatar->set($image);
327
+            // Clean up
328
+            $this->cache->remove('tmpAvatar');
329
+            return new JSONResponse(['status' => 'success']);
330
+        } catch (\OC\NotSquareException $e) {
331
+            return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]],
332
+                                    Http::STATUS_BAD_REQUEST);
333
+        } catch (\Exception $e) {
334
+            $this->logger->logException($e, ['app' => 'core']);
335
+            return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
336
+        }
337
+    }
338 338
 }
Please login to merge, or discard this patch.