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