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