-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathAvatarsController.php
115 lines (99 loc) Β· 2.86 KB
/
AvatarsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Mail\Controller;
use OCA\Mail\Contracts\IAvatarService;
use OCA\Mail\Http\AvatarDownloadResponse;
use OCA\Mail\Http\TrapError;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use OCP\IRequest;
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class AvatarsController extends Controller {
private IAvatarService $avatarService;
private string $uid;
public function __construct(string $appName,
IRequest $request,
IAvatarService $avatarService,
string $UserId) {
parent::__construct($appName, $request);
$this->avatarService = $avatarService;
$this->uid = $UserId;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $email
* @return JSONResponse
*/
#[TrapError]
public function url(string $email): JSONResponse {
$email = $this->normalizeEmail($email);
if ($this->validateEmail($email) === false) {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}
$avatar = $this->avatarService->getAvatar($email, $this->uid);
if (is_null($avatar)) {
// No avatar found
$response = new JSONResponse([], Http::STATUS_NO_CONTENT);
$response->cacheFor(60 * 60, false, true);
return $response;
}
$response = new JSONResponse($avatar);
// Let the browser cache this for a week
$response->cacheFor(7 * 24 * 60 * 60, false, true);
return $response;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $email
* @return Response
*/
#[TrapError]
public function image(string $email): Response {
$email = $this->normalizeEmail($email);
if ($this->validateEmail($email) === false) {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}
$imageData = $this->avatarService->getAvatarImage($email, $this->uid);
[$avatar, $image] = $imageData;
if (is_null($imageData) || !$avatar->isExternal()) {
// This could happen if the cache invalidated meanwhile
$response = new Response(Http::STATUS_NO_CONTENT);
// Clear cache
$response->cacheFor(0);
return $response;
}
$resp = new AvatarDownloadResponse($image);
$resp->addHeader('Content-Type', $avatar->getMime());
// Let the browser cache this for a week
$resp->cacheFor(7 * 24 * 60 * 60, false, true);
return $resp;
}
private function normalizeEmail(string $email): string {
// remove single quotes and whitespace the might exist
// Examples:
// '[email protected]'
// ' [email protected]'
return trim(trim($email, "'"));
}
private function validateEmail(string $email): bool {
if (empty($email)) {
return false;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
}
return true;
}
}