1<?php 2 3declare(strict_types=1); 4 5/** 6 * @author Christoph Wurst <christoph@winzerhof-wurst.at> 7 * @author Jakob Sack <mail@jakobsack.de> 8 * 9 * Mail 10 * 11 * This code is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Affero General Public License, version 3, 13 * as published by the Free Software Foundation. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Affero General Public License for more details. 19 * 20 * You should have received a copy of the GNU Affero General Public License, version 3, 21 * along with this program. If not, see <http://www.gnu.org/licenses/> 22 * 23 */ 24 25namespace OCA\Mail\Controller; 26 27use OCA\Mail\Contracts\IAvatarService; 28use OCA\Mail\Http\AvatarDownloadResponse; 29use OCP\AppFramework\Controller; 30use OCP\AppFramework\Http; 31use OCP\AppFramework\Http\JSONResponse; 32use OCP\AppFramework\Http\Response; 33use OCP\IRequest; 34 35class AvatarsController extends Controller { 36 37 /** @var IAvatarService */ 38 private $avatarService; 39 40 /** @var string */ 41 private $uid; 42 43 public function __construct(string $appName, 44 IRequest $request, 45 IAvatarService $avatarService, 46 string $UserId) { 47 parent::__construct($appName, $request); 48 49 $this->avatarService = $avatarService; 50 $this->uid = $UserId; 51 } 52 53 /** 54 * @NoAdminRequired 55 * @NoCSRFRequired 56 * @TrapError 57 * 58 * @param string $email 59 * @return JSONResponse 60 */ 61 public function url(string $email): JSONResponse { 62 if (empty($email)) { 63 return new JSONResponse([], Http::STATUS_BAD_REQUEST); 64 } 65 66 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 67 return new JSONResponse([], Http::STATUS_BAD_REQUEST); 68 } 69 70 $avatar = $this->avatarService->getAvatar($email, $this->uid); 71 if (is_null($avatar)) { 72 // No avatar found 73 $response = new JSONResponse([], Http::STATUS_NOT_FOUND); 74 75 // Debounce this a bit 76 // (cache for one day) 77 $response->cacheFor(24 * 60 * 60); 78 79 return $response; 80 } 81 82 $response = new JSONResponse($avatar); 83 84 // Let the browser cache this for a week 85 $response->cacheFor(7 * 24 * 60 * 60); 86 87 return $response; 88 } 89 90 /** 91 * @NoAdminRequired 92 * @NoCSRFRequired 93 * @TrapError 94 * 95 * @param string $email 96 * @return Response 97 */ 98 public function image(string $email): Response { 99 if (empty($email)) { 100 return new JSONResponse([], Http::STATUS_BAD_REQUEST); 101 } 102 103 $imageData = $this->avatarService->getAvatarImage($email, $this->uid); 104 [$avatar, $image] = $imageData; 105 106 if (is_null($imageData) || !$avatar->isExternal()) { 107 // This could happen if the cache invalidated meanwhile 108 return $this->noAvatarFoundResponse(); 109 } 110 111 $resp = new AvatarDownloadResponse($image); 112 $resp->addHeader('Content-Type', $avatar->getMime()); 113 114 // Let the browser cache this for a week 115 $resp->cacheFor(7 * 24 * 60 * 60); 116 117 return $resp; 118 } 119 120 private function noAvatarFoundResponse(): Response { 121 $response = new Response(); 122 $response->setStatus(Http::STATUS_NOT_FOUND); 123 // Clear cache 124 $response->cacheFor(0); 125 return $response; 126 } 127} 128