|
|
|
|
@ -43,7 +43,6 @@ use OCP\ICache; |
|
|
|
|
use OCP\IL10N; |
|
|
|
|
use OCP\IRequest; |
|
|
|
|
use OCP\IUserManager; |
|
|
|
|
use OCP\IUserSession; |
|
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -52,36 +51,17 @@ use Psr\Log\LoggerInterface; |
|
|
|
|
* @package OC\Core\Controller |
|
|
|
|
*/ |
|
|
|
|
class AvatarController extends Controller { |
|
|
|
|
protected IAvatarManager $avatarManager; |
|
|
|
|
protected ICache $cache; |
|
|
|
|
protected IL10N $l; |
|
|
|
|
protected IUserManager $userManager; |
|
|
|
|
protected IUserSession $userSession; |
|
|
|
|
protected IRootFolder $rootFolder; |
|
|
|
|
protected LoggerInterface $logger; |
|
|
|
|
protected ?string $userId; |
|
|
|
|
protected TimeFactory $timeFactory; |
|
|
|
|
|
|
|
|
|
public function __construct(string $appName, |
|
|
|
|
IRequest $request, |
|
|
|
|
IAvatarManager $avatarManager, |
|
|
|
|
ICache $cache, |
|
|
|
|
IL10N $l10n, |
|
|
|
|
IUserManager $userManager, |
|
|
|
|
IRootFolder $rootFolder, |
|
|
|
|
LoggerInterface $logger, |
|
|
|
|
?string $userId, |
|
|
|
|
TimeFactory $timeFactory) { |
|
|
|
|
protected IAvatarManager $avatarManager, |
|
|
|
|
protected ICache $cache, |
|
|
|
|
protected IL10N $l10n, |
|
|
|
|
protected IUserManager $userManager, |
|
|
|
|
protected IRootFolder $rootFolder, |
|
|
|
|
protected LoggerInterface $logger, |
|
|
|
|
protected ?string $userId, |
|
|
|
|
protected TimeFactory $timeFactory) { |
|
|
|
|
parent::__construct($appName, $request); |
|
|
|
|
|
|
|
|
|
$this->avatarManager = $avatarManager; |
|
|
|
|
$this->cache = $cache; |
|
|
|
|
$this->l = $l10n; |
|
|
|
|
$this->userManager = $userManager; |
|
|
|
|
$this->rootFolder = $rootFolder; |
|
|
|
|
$this->logger = $logger; |
|
|
|
|
$this->userId = $userId; |
|
|
|
|
$this->timeFactory = $timeFactory; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -173,18 +153,18 @@ class AvatarController extends Controller { |
|
|
|
|
/** @var File $node */ |
|
|
|
|
$node = $userFolder->get($path); |
|
|
|
|
if (!($node instanceof File)) { |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]); |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('Please select a file.')]]); |
|
|
|
|
} |
|
|
|
|
if ($node->getSize() > 20 * 1024 * 1024) { |
|
|
|
|
return new JSONResponse( |
|
|
|
|
['data' => ['message' => $this->l->t('File is too big')]], |
|
|
|
|
['data' => ['message' => $this->l10n->t('File is too big')]], |
|
|
|
|
Http::STATUS_BAD_REQUEST |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') { |
|
|
|
|
return new JSONResponse( |
|
|
|
|
['data' => ['message' => $this->l->t('The selected file is not an image.')]], |
|
|
|
|
['data' => ['message' => $this->l10n->t('The selected file is not an image.')]], |
|
|
|
|
Http::STATUS_BAD_REQUEST |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
@ -193,7 +173,7 @@ class AvatarController extends Controller { |
|
|
|
|
$content = $node->getContent(); |
|
|
|
|
} catch (\OCP\Files\NotPermittedException $e) { |
|
|
|
|
return new JSONResponse( |
|
|
|
|
['data' => ['message' => $this->l->t('The selected file cannot be read.')]], |
|
|
|
|
['data' => ['message' => $this->l10n->t('The selected file cannot be read.')]], |
|
|
|
|
Http::STATUS_BAD_REQUEST |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
@ -205,7 +185,7 @@ class AvatarController extends Controller { |
|
|
|
|
) { |
|
|
|
|
if ($files['size'][0] > 20 * 1024 * 1024) { |
|
|
|
|
return new JSONResponse( |
|
|
|
|
['data' => ['message' => $this->l->t('File is too big')]], |
|
|
|
|
['data' => ['message' => $this->l10n->t('File is too big')]], |
|
|
|
|
Http::STATUS_BAD_REQUEST |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
@ -214,16 +194,16 @@ class AvatarController extends Controller { |
|
|
|
|
unlink($files['tmp_name'][0]); |
|
|
|
|
} else { |
|
|
|
|
$phpFileUploadErrors = [ |
|
|
|
|
UPLOAD_ERR_OK => $this->l->t('The file was uploaded'), |
|
|
|
|
UPLOAD_ERR_INI_SIZE => $this->l->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'), |
|
|
|
|
UPLOAD_ERR_FORM_SIZE => $this->l->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'), |
|
|
|
|
UPLOAD_ERR_PARTIAL => $this->l->t('The file was only partially uploaded'), |
|
|
|
|
UPLOAD_ERR_NO_FILE => $this->l->t('No file was uploaded'), |
|
|
|
|
UPLOAD_ERR_NO_TMP_DIR => $this->l->t('Missing a temporary folder'), |
|
|
|
|
UPLOAD_ERR_CANT_WRITE => $this->l->t('Could not write file to disk'), |
|
|
|
|
UPLOAD_ERR_EXTENSION => $this->l->t('A PHP extension stopped the file upload'), |
|
|
|
|
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'), |
|
|
|
|
]; |
|
|
|
|
$message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l->t('Invalid file provided'); |
|
|
|
|
$message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l10n->t('Invalid file provided'); |
|
|
|
|
$this->logger->warning($message, ['app' => 'core']); |
|
|
|
|
return new JSONResponse( |
|
|
|
|
['data' => ['message' => $message]], |
|
|
|
|
@ -233,7 +213,7 @@ class AvatarController extends Controller { |
|
|
|
|
} else { |
|
|
|
|
//Add imgfile |
|
|
|
|
return new JSONResponse( |
|
|
|
|
['data' => ['message' => $this->l->t('No image or file provided')]], |
|
|
|
|
['data' => ['message' => $this->l10n->t('No image or file provided')]], |
|
|
|
|
Http::STATUS_BAD_REQUEST |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
@ -248,7 +228,7 @@ class AvatarController extends Controller { |
|
|
|
|
$mimeType = $image->mimeType(); |
|
|
|
|
if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { |
|
|
|
|
return new JSONResponse( |
|
|
|
|
['data' => ['message' => $this->l->t('Unknown filetype')]], |
|
|
|
|
['data' => ['message' => $this->l10n->t('Unknown filetype')]], |
|
|
|
|
Http::STATUS_OK |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
@ -262,7 +242,7 @@ class AvatarController extends Controller { |
|
|
|
|
return new JSONResponse(['status' => 'success']); |
|
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -273,13 +253,13 @@ class AvatarController extends Controller { |
|
|
|
|
); |
|
|
|
|
} else { |
|
|
|
|
return new JSONResponse( |
|
|
|
|
['data' => ['message' => $this->l->t('Invalid image')]], |
|
|
|
|
['data' => ['message' => $this->l10n->t('Invalid image')]], |
|
|
|
|
Http::STATUS_OK |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} catch (\Exception $e) { |
|
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -293,7 +273,7 @@ class AvatarController extends Controller { |
|
|
|
|
return new JSONResponse(); |
|
|
|
|
} catch (\Exception $e) { |
|
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -306,7 +286,7 @@ class AvatarController extends Controller { |
|
|
|
|
$tmpAvatar = $this->cache->get('tmpAvatar'); |
|
|
|
|
if (is_null($tmpAvatar)) { |
|
|
|
|
return new JSONResponse(['data' => [ |
|
|
|
|
'message' => $this->l->t("No temporary profile picture available, try again") |
|
|
|
|
'message' => $this->l10n->t("No temporary profile picture available, try again") |
|
|
|
|
]], |
|
|
|
|
Http::STATUS_NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
@ -330,19 +310,19 @@ class AvatarController extends Controller { |
|
|
|
|
*/ |
|
|
|
|
public function postCroppedAvatar(?array $crop = null): JSONResponse { |
|
|
|
|
if (is_null($crop)) { |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]], |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t("No crop data provided")]], |
|
|
|
|
Http::STATUS_BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]], |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t("No valid crop data provided")]], |
|
|
|
|
Http::STATUS_BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$tmpAvatar = $this->cache->get('tmpAvatar'); |
|
|
|
|
if (is_null($tmpAvatar)) { |
|
|
|
|
return new JSONResponse(['data' => [ |
|
|
|
|
'message' => $this->l->t("No temporary profile picture available, try again") |
|
|
|
|
'message' => $this->l10n->t("No temporary profile picture available, try again") |
|
|
|
|
]], |
|
|
|
|
Http::STATUS_BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
@ -357,11 +337,11 @@ class AvatarController extends Controller { |
|
|
|
|
$this->cache->remove('tmpAvatar'); |
|
|
|
|
return new JSONResponse(['status' => 'success']); |
|
|
|
|
} catch (\OC\NotSquareException $e) { |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]], |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('Crop is not square')]], |
|
|
|
|
Http::STATUS_BAD_REQUEST); |
|
|
|
|
} catch (\Exception $e) { |
|
|
|
|
$this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
|
|
|
return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|