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