2015-03-03 11:52:27 +00:00
|
|
|
<?php
|
2024-05-27 08:08:53 +00:00
|
|
|
|
2015-03-03 11:52:27 +00:00
|
|
|
/**
|
2024-05-27 08:08:53 +00:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-03-03 11:52:27 +00:00
|
|
|
*/
|
2016-01-20 09:17:46 +00:00
|
|
|
namespace OC\Core\Controller;
|
2015-03-03 11:52:27 +00:00
|
|
|
|
2016-08-29 12:55:23 +00:00
|
|
|
use OC\AppFramework\Utility\TimeFactory;
|
2015-03-03 11:52:27 +00:00
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http;
|
2024-01-10 11:35:44 +00:00
|
|
|
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
|
2024-07-25 11:24:59 +00:00
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
|
|
|
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
|
2024-12-17 12:17:22 +00:00
|
|
|
use OCP\AppFramework\Http\Attribute\OpenAPI;
|
2024-07-25 11:24:59 +00:00
|
|
|
use OCP\AppFramework\Http\Attribute\PublicPage;
|
2015-03-03 11:52:27 +00:00
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
2016-08-23 19:44:05 +00:00
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
2016-08-29 19:31:41 +00:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2024-06-06 17:36:19 +00:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2016-08-25 19:39:14 +00:00
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\Files\IRootFolder;
|
2015-03-03 11:52:27 +00:00
|
|
|
use OCP\IAvatarManager;
|
2016-08-25 19:39:14 +00:00
|
|
|
use OCP\ICache;
|
2015-03-03 11:52:27 +00:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IUserManager;
|
2022-04-12 15:55:01 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-03-03 11:52:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AvatarController
|
|
|
|
*
|
2016-01-20 09:17:46 +00:00
|
|
|
* @package OC\Core\Controller
|
2015-03-03 11:52:27 +00:00
|
|
|
*/
|
|
|
|
class AvatarController extends Controller {
|
2023-06-05 15:05:32 +00:00
|
|
|
public function __construct(
|
|
|
|
string $appName,
|
|
|
|
IRequest $request,
|
|
|
|
protected IAvatarManager $avatarManager,
|
|
|
|
protected ICache $cache,
|
|
|
|
protected IL10N $l10n,
|
|
|
|
protected IUserManager $userManager,
|
|
|
|
protected IRootFolder $rootFolder,
|
|
|
|
protected LoggerInterface $logger,
|
|
|
|
protected ?string $userId,
|
|
|
|
protected TimeFactory $timeFactory,
|
2024-06-06 16:48:14 +00:00
|
|
|
protected GuestAvatarController $guestAvatarController,
|
2023-06-05 15:05:32 +00:00
|
|
|
) {
|
2015-03-03 11:52:27 +00:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
}
|
|
|
|
|
2022-08-30 11:27:21 +00:00
|
|
|
/**
|
|
|
|
* @NoSameSiteCookieRequired
|
|
|
|
*
|
2023-03-15 16:29:32 +00:00
|
|
|
* Get the dark avatar
|
|
|
|
*
|
|
|
|
* @param string $userId ID of the user
|
2024-08-14 07:29:30 +00:00
|
|
|
* @param 64|512 $size Size of the avatar
|
2024-06-06 17:36:19 +00:00
|
|
|
* @param bool $guestFallback Fallback to guest avatar if not found
|
2024-09-24 13:53:13 +00:00
|
|
|
* @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
|
2023-03-15 16:29:32 +00:00
|
|
|
*
|
|
|
|
* 200: Avatar returned
|
2024-06-07 10:09:36 +00:00
|
|
|
* 201: Avatar returned
|
2023-03-15 16:29:32 +00:00
|
|
|
* 404: Avatar not found
|
2022-08-30 11:27:21 +00:00
|
|
|
*/
|
2024-07-25 11:24:59 +00:00
|
|
|
#[NoCSRFRequired]
|
|
|
|
#[PublicPage]
|
2024-01-10 11:35:44 +00:00
|
|
|
#[FrontpageRoute(verb: 'GET', url: '/avatar/{userId}/{size}/dark')]
|
2024-12-17 12:17:22 +00:00
|
|
|
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
|
2024-06-06 16:48:14 +00:00
|
|
|
public function getAvatarDark(string $userId, int $size, bool $guestFallback = false) {
|
2022-08-30 11:27:21 +00:00
|
|
|
if ($size <= 64) {
|
|
|
|
if ($size !== 64) {
|
|
|
|
$this->logger->debug('Avatar requested in deprecated size ' . $size);
|
|
|
|
}
|
|
|
|
$size = 64;
|
|
|
|
} else {
|
|
|
|
if ($size !== 512) {
|
|
|
|
$this->logger->debug('Avatar requested in deprecated size ' . $size);
|
|
|
|
}
|
|
|
|
$size = 512;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$avatar = $this->avatarManager->getAvatar($userId);
|
|
|
|
$avatarFile = $avatar->getFile($size, true);
|
|
|
|
$response = new FileDisplayResponse(
|
|
|
|
$avatarFile,
|
|
|
|
Http::STATUS_OK,
|
|
|
|
['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()]
|
|
|
|
);
|
|
|
|
} catch (\Exception $e) {
|
2024-06-06 16:48:14 +00:00
|
|
|
if ($guestFallback) {
|
2024-08-14 07:29:30 +00:00
|
|
|
return $this->guestAvatarController->getAvatarDark($userId, $size);
|
2024-06-06 16:48:14 +00:00
|
|
|
}
|
2022-08-30 11:27:21 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache for 1 day
|
|
|
|
$response->cacheFor(60 * 60 * 24, false, true);
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2017-10-19 14:22:43 +00:00
|
|
|
|
2015-03-03 11:52:27 +00:00
|
|
|
/**
|
2017-10-11 12:41:34 +00:00
|
|
|
* @NoSameSiteCookieRequired
|
2015-03-03 11:52:27 +00:00
|
|
|
*
|
2023-03-15 16:29:32 +00:00
|
|
|
* Get the avatar
|
|
|
|
*
|
|
|
|
* @param string $userId ID of the user
|
2024-08-14 07:29:30 +00:00
|
|
|
* @param 64|512 $size Size of the avatar
|
2024-06-06 17:36:19 +00:00
|
|
|
* @param bool $guestFallback Fallback to guest avatar if not found
|
2024-09-24 13:53:13 +00:00
|
|
|
* @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
|
2023-03-15 16:29:32 +00:00
|
|
|
*
|
|
|
|
* 200: Avatar returned
|
2024-06-07 10:09:36 +00:00
|
|
|
* 201: Avatar returned
|
2023-03-15 16:29:32 +00:00
|
|
|
* 404: Avatar not found
|
2015-03-03 11:52:27 +00:00
|
|
|
*/
|
2024-07-25 11:24:59 +00:00
|
|
|
#[NoCSRFRequired]
|
|
|
|
#[PublicPage]
|
2024-01-10 11:35:44 +00:00
|
|
|
#[FrontpageRoute(verb: 'GET', url: '/avatar/{userId}/{size}')]
|
2024-12-17 12:17:22 +00:00
|
|
|
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
|
2024-06-06 16:48:14 +00:00
|
|
|
public function getAvatar(string $userId, int $size, bool $guestFallback = false) {
|
2022-02-04 11:12:48 +00:00
|
|
|
if ($size <= 64) {
|
|
|
|
if ($size !== 64) {
|
|
|
|
$this->logger->debug('Avatar requested in deprecated size ' . $size);
|
|
|
|
}
|
2015-03-03 11:52:27 +00:00
|
|
|
$size = 64;
|
2022-02-04 11:12:48 +00:00
|
|
|
} else {
|
|
|
|
if ($size !== 512) {
|
|
|
|
$this->logger->debug('Avatar requested in deprecated size ' . $size);
|
|
|
|
}
|
|
|
|
$size = 512;
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 07:44:10 +00:00
|
|
|
try {
|
2018-08-01 08:56:22 +00:00
|
|
|
$avatar = $this->avatarManager->getAvatar($userId);
|
|
|
|
$avatarFile = $avatar->getFile($size);
|
2020-02-28 09:44:15 +00:00
|
|
|
$response = new FileDisplayResponse(
|
2018-08-01 08:56:22 +00:00
|
|
|
$avatarFile,
|
2020-02-29 23:42:24 +00:00
|
|
|
Http::STATUS_OK,
|
|
|
|
['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()]
|
2018-08-01 08:56:22 +00:00
|
|
|
);
|
2018-05-28 07:44:10 +00:00
|
|
|
} catch (\Exception $e) {
|
2024-06-06 16:48:14 +00:00
|
|
|
if ($guestFallback) {
|
2024-08-14 07:29:30 +00:00
|
|
|
return $this->guestAvatarController->getAvatar($userId, $size);
|
2024-06-06 16:48:14 +00:00
|
|
|
}
|
2019-11-13 19:43:20 +00:00
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 12:52:55 +00:00
|
|
|
// Cache for 1 day
|
2022-02-25 13:24:07 +00:00
|
|
|
$response->cacheFor(60 * 60 * 24, false, true);
|
2020-02-28 09:44:15 +00:00
|
|
|
return $response;
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 11:24:59 +00:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 11:35:44 +00:00
|
|
|
#[FrontpageRoute(verb: 'POST', url: '/avatar/')]
|
2022-04-12 15:55:01 +00:00
|
|
|
public function postAvatar(?string $path = null): JSONResponse {
|
2015-03-03 11:52:27 +00:00
|
|
|
$files = $this->request->getUploadedFile('files');
|
|
|
|
|
|
|
|
if (isset($path)) {
|
|
|
|
$path = stripslashes($path);
|
2016-08-25 19:39:14 +00:00
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId);
|
2017-07-19 14:15:00 +00:00
|
|
|
/** @var File $node */
|
2016-08-25 19:39:14 +00:00
|
|
|
$node = $userFolder->get($path);
|
|
|
|
if (!($node instanceof File)) {
|
2023-06-04 19:50:35 +00:00
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('Please select a file.')]]);
|
2016-01-08 09:03:49 +00:00
|
|
|
}
|
2020-10-05 13:12:57 +00:00
|
|
|
if ($node->getSize() > 20 * 1024 * 1024) {
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse(
|
2023-06-04 19:50:35 +00:00
|
|
|
['data' => ['message' => $this->l10n->t('File is too big')]],
|
2016-08-29 19:31:41 +00:00
|
|
|
Http::STATUS_BAD_REQUEST
|
2015-08-26 15:08:25 +00:00
|
|
|
);
|
2015-07-22 11:13:39 +00:00
|
|
|
}
|
2016-08-29 14:50:01 +00:00
|
|
|
|
|
|
|
if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') {
|
|
|
|
return new JSONResponse(
|
2023-06-04 19:50:35 +00:00
|
|
|
['data' => ['message' => $this->l10n->t('The selected file is not an image.')]],
|
2016-08-29 14:50:01 +00:00
|
|
|
Http::STATUS_BAD_REQUEST
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$content = $node->getContent();
|
|
|
|
} catch (\OCP\Files\NotPermittedException $e) {
|
|
|
|
return new JSONResponse(
|
2023-06-04 19:50:35 +00:00
|
|
|
['data' => ['message' => $this->l10n->t('The selected file cannot be read.')]],
|
2016-08-29 14:50:01 +00:00
|
|
|
Http::STATUS_BAD_REQUEST
|
|
|
|
);
|
|
|
|
}
|
2015-03-03 11:52:27 +00:00
|
|
|
} elseif (!is_null($files)) {
|
|
|
|
if (
|
|
|
|
$files['error'][0] === 0 &&
|
2024-07-17 00:03:18 +00:00
|
|
|
is_uploaded_file($files['tmp_name'][0])
|
2015-03-03 11:52:27 +00:00
|
|
|
) {
|
2020-10-05 13:12:57 +00:00
|
|
|
if ($files['size'][0] > 20 * 1024 * 1024) {
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse(
|
2023-06-04 19:50:35 +00:00
|
|
|
['data' => ['message' => $this->l10n->t('File is too big')]],
|
2016-08-29 19:31:41 +00:00
|
|
|
Http::STATUS_BAD_REQUEST
|
2015-08-26 15:08:25 +00:00
|
|
|
);
|
2015-07-22 11:13:39 +00:00
|
|
|
}
|
2015-03-03 11:52:27 +00:00
|
|
|
$this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200);
|
2015-03-12 10:13:12 +00:00
|
|
|
$content = $this->cache->get('avatar_upload');
|
2015-03-03 11:52:27 +00:00
|
|
|
unlink($files['tmp_name'][0]);
|
|
|
|
} else {
|
2021-04-01 09:42:39 +00:00
|
|
|
$phpFileUploadErrors = [
|
2023-06-04 19:50:35 +00:00
|
|
|
UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'),
|
|
|
|
UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
|
|
|
|
UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
|
|
|
|
UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'),
|
|
|
|
UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'),
|
|
|
|
UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'),
|
|
|
|
UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'),
|
|
|
|
UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'),
|
2021-04-01 09:42:39 +00:00
|
|
|
];
|
2023-06-04 19:50:35 +00:00
|
|
|
$message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l10n->t('Invalid file provided');
|
2021-04-01 09:42:39 +00:00
|
|
|
$this->logger->warning($message, ['app' => 'core']);
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse(
|
2021-04-01 09:42:39 +00:00
|
|
|
['data' => ['message' => $message]],
|
2016-08-29 19:31:41 +00:00
|
|
|
Http::STATUS_BAD_REQUEST
|
2015-08-26 15:08:25 +00:00
|
|
|
);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//Add imgfile
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse(
|
2023-06-04 19:50:35 +00:00
|
|
|
['data' => ['message' => $this->l10n->t('No image or file provided')]],
|
2016-08-29 19:31:41 +00:00
|
|
|
Http::STATUS_BAD_REQUEST
|
2015-08-26 15:08:25 +00:00
|
|
|
);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-06-02 00:37:36 +00:00
|
|
|
$image = new \OCP\Image();
|
2015-03-12 10:13:12 +00:00
|
|
|
$image->loadFromData($content);
|
2016-10-26 19:36:33 +00:00
|
|
|
$image->readExif($content);
|
2015-03-03 11:52:27 +00:00
|
|
|
$image->fixOrientation();
|
|
|
|
|
|
|
|
if ($image->valid()) {
|
|
|
|
$mimeType = $image->mimeType();
|
|
|
|
if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') {
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse(
|
2023-06-04 19:50:35 +00:00
|
|
|
['data' => ['message' => $this->l10n->t('Unknown filetype')]],
|
2016-08-29 19:31:41 +00:00
|
|
|
Http::STATUS_OK
|
2015-08-26 15:08:25 +00:00
|
|
|
);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 00:32:16 +00:00
|
|
|
if ($image->width() === $image->height()) {
|
|
|
|
try {
|
|
|
|
$avatar = $this->avatarManager->getAvatar($this->userId);
|
|
|
|
$avatar->set($image);
|
|
|
|
// Clean up
|
|
|
|
$this->cache->remove('tmpAvatar');
|
|
|
|
return new JSONResponse(['status' => 'success']);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
|
2023-06-04 19:50:35 +00:00
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
|
2022-07-30 00:32:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 11:52:27 +00:00
|
|
|
$this->cache->set('tmpAvatar', $image->data(), 7200);
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse(
|
2015-08-26 15:08:25 +00:00
|
|
|
['data' => 'notsquare'],
|
2016-08-29 19:31:41 +00:00
|
|
|
Http::STATUS_OK
|
2015-08-26 15:08:25 +00:00
|
|
|
);
|
2015-03-03 11:52:27 +00:00
|
|
|
} else {
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse(
|
2023-06-04 19:50:35 +00:00
|
|
|
['data' => ['message' => $this->l10n->t('Invalid image')]],
|
2016-08-29 19:31:41 +00:00
|
|
|
Http::STATUS_OK
|
2015-08-26 15:08:25 +00:00
|
|
|
);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2022-04-12 15:55:01 +00:00
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
|
2023-06-04 19:50:35 +00:00
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 11:24:59 +00:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 11:35:44 +00:00
|
|
|
#[FrontpageRoute(verb: 'DELETE', url: '/avatar/')]
|
2022-04-12 15:55:01 +00:00
|
|
|
public function deleteAvatar(): JSONResponse {
|
2015-03-03 11:52:27 +00:00
|
|
|
try {
|
2016-08-25 19:39:14 +00:00
|
|
|
$avatar = $this->avatarManager->getAvatar($this->userId);
|
2015-03-03 11:52:27 +00:00
|
|
|
$avatar->remove();
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse();
|
2015-03-03 11:52:27 +00:00
|
|
|
} catch (\Exception $e) {
|
2022-04-12 15:55:01 +00:00
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
|
2023-06-04 19:50:35 +00:00
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-29 19:31:41 +00:00
|
|
|
* @return JSONResponse|DataDisplayResponse
|
2015-03-03 11:52:27 +00:00
|
|
|
*/
|
2024-07-25 11:24:59 +00:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 11:35:44 +00:00
|
|
|
#[FrontpageRoute(verb: 'GET', url: '/avatar/tmp')]
|
2015-03-03 11:52:27 +00:00
|
|
|
public function getTmpAvatar() {
|
|
|
|
$tmpAvatar = $this->cache->get('tmpAvatar');
|
|
|
|
if (is_null($tmpAvatar)) {
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse(['data' => [
|
2023-06-04 19:50:35 +00:00
|
|
|
'message' => $this->l10n->t('No temporary profile picture available, try again')
|
2020-04-09 07:22:29 +00:00
|
|
|
]],
|
2023-01-20 10:45:08 +00:00
|
|
|
Http::STATUS_NOT_FOUND);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 00:37:36 +00:00
|
|
|
$image = new \OCP\Image();
|
2018-01-17 10:46:30 +00:00
|
|
|
$image->loadFromData($tmpAvatar);
|
2015-03-03 11:52:27 +00:00
|
|
|
|
2021-10-25 14:17:27 +00:00
|
|
|
$resp = new DataDisplayResponse(
|
2023-01-20 10:45:08 +00:00
|
|
|
$image->data() ?? '',
|
|
|
|
Http::STATUS_OK,
|
|
|
|
['Content-Type' => $image->mimeType()]);
|
2015-03-03 11:52:27 +00:00
|
|
|
|
2021-10-25 14:17:27 +00:00
|
|
|
$resp->setETag((string)crc32($image->data() ?? ''));
|
2015-03-03 11:52:27 +00:00
|
|
|
$resp->cacheFor(0);
|
|
|
|
$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
|
|
|
|
return $resp;
|
|
|
|
}
|
|
|
|
|
2024-07-25 11:24:59 +00:00
|
|
|
#[NoAdminRequired]
|
2024-01-10 11:35:44 +00:00
|
|
|
#[FrontpageRoute(verb: 'POST', url: '/avatar/cropped')]
|
2022-04-12 15:55:01 +00:00
|
|
|
public function postCroppedAvatar(?array $crop = null): JSONResponse {
|
2015-03-03 11:52:27 +00:00
|
|
|
if (is_null($crop)) {
|
2023-06-04 19:50:35 +00:00
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('No crop data provided')]],
|
2023-01-20 10:45:08 +00:00
|
|
|
Http::STATUS_BAD_REQUEST);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) {
|
2023-06-04 19:50:35 +00:00
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('No valid crop data provided')]],
|
2023-01-20 10:45:08 +00:00
|
|
|
Http::STATUS_BAD_REQUEST);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$tmpAvatar = $this->cache->get('tmpAvatar');
|
|
|
|
if (is_null($tmpAvatar)) {
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse(['data' => [
|
2023-06-04 19:50:35 +00:00
|
|
|
'message' => $this->l10n->t('No temporary profile picture available, try again')
|
2020-04-09 07:22:29 +00:00
|
|
|
]],
|
2023-01-20 10:45:08 +00:00
|
|
|
Http::STATUS_BAD_REQUEST);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 00:37:36 +00:00
|
|
|
$image = new \OCP\Image();
|
2018-01-17 10:46:30 +00:00
|
|
|
$image->loadFromData($tmpAvatar);
|
2017-07-19 14:15:00 +00:00
|
|
|
$image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h']));
|
2015-03-03 11:52:27 +00:00
|
|
|
try {
|
2016-08-25 19:39:14 +00:00
|
|
|
$avatar = $this->avatarManager->getAvatar($this->userId);
|
2015-03-03 11:52:27 +00:00
|
|
|
$avatar->set($image);
|
|
|
|
// Clean up
|
|
|
|
$this->cache->remove('tmpAvatar');
|
2016-08-29 19:31:41 +00:00
|
|
|
return new JSONResponse(['status' => 'success']);
|
2015-03-03 11:52:27 +00:00
|
|
|
} catch (\OC\NotSquareException $e) {
|
2023-06-04 19:50:35 +00:00
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('Crop is not square')]],
|
2023-01-20 10:45:08 +00:00
|
|
|
Http::STATUS_BAD_REQUEST);
|
2015-10-13 12:12:10 +00:00
|
|
|
} catch (\Exception $e) {
|
2022-04-12 15:55:01 +00:00
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']);
|
2023-06-04 19:50:35 +00:00
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST);
|
2015-03-03 11:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|