Moved logic from controller to service

Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
Vitor Mattos 2022-11-19 09:47:55 -03:00 committed by Joas Schilling
parent 18d11a6b2b
commit 3a34b14822
No known key found for this signature in database
GPG key ID: 74434EFE0D2E2205
3 changed files with 35 additions and 28 deletions

View file

@ -27,7 +27,6 @@ declare(strict_types=1);
namespace OCA\Talk\Controller;
use InvalidArgumentException;
use OC\Files\Filesystem;
use OCA\Talk\Service\AvatarService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
@ -36,23 +35,27 @@ use OCP\AppFramework\Http\Response;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
class AvatarController extends AEnvironmentAwareController {
private AvatarService $avatarService;
private IUserSession $userSession;
private IL10N $l;
private LoggerInterface $logger;
public function __construct(
string $appName,
IRequest $request,
AvatarService $avatarService,
IUserSession $userSession,
IL10N $l10n
IL10N $l10n,
LoggerInterface $logger
) {
parent::__construct($appName, $request);
$this->avatarService = $avatarService;
$this->userSession = $userSession;
$this->l = $l10n;
$this->logger = $logger;
}
/**
@ -62,25 +65,7 @@ class AvatarController extends AEnvironmentAwareController {
public function uploadAvatar(): DataResponse {
try {
$file = $this->request->getUploadedFile('file');
if (is_null($file)) {
throw new InvalidArgumentException($this->l->t('No image file provided'));
}
if (
$file['error'] !== 0 ||
!is_uploaded_file($file['tmp_name']) ||
Filesystem::isFileBlacklisted($file['tmp_name'])
) {
throw new InvalidArgumentException($this->l->t('Invalid file provided'));
}
if ($file['size'] > 20 * 1024 * 1024) {
throw new InvalidArgumentException($this->l->t('File is too big'));
}
$content = file_get_contents($file['tmp_name']);
unlink($file['tmp_name']);
$this->avatarService->setAvatar($this->getRoom(), $content);
$this->avatarService->setAvatarFromRequest($this->getRoom(), $file);
return new DataResponse();
} catch (InvalidArgumentException $e) {
return new DataResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST);

View file

@ -27,6 +27,7 @@ declare(strict_types=1);
namespace OCA\Talk\Service;
use InvalidArgumentException;
use OC\Files\Filesystem;
use OCA\Talk\Room;
use OCP\Files\IAppData;
use OCP\Files\NotFoundException;
@ -59,13 +60,38 @@ class AvatarService {
$this->avatarManager = $avatarManager;
}
public function setAvatar(Room $room, string $content): void {
public function setAvatarFromRequest(Room $room, array $file): void {
if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
throw new InvalidArgumentException($this->l->t('One to one rooms always need to show the other users avatar'));
}
if (is_null($file)) {
throw new InvalidArgumentException($this->l->t('No image file provided'));
}
if (
$file['error'] !== 0 ||
!is_uploaded_file($file['tmp_name']) ||
Filesystem::isFileBlacklisted($file['tmp_name'])
) {
throw new InvalidArgumentException($this->l->t('Invalid file provided'));
}
if ($file['size'] > 20 * 1024 * 1024) {
throw new InvalidArgumentException($this->l->t('File is too big'));
}
$content = file_get_contents($file['tmp_name']);
unlink($file['tmp_name']);
$image = new \OC_Image();
$image->loadFromData($content);
$image->readExif($content);
$this->setAvatar($room, $image);
}
public function setAvatar(Room $room, \OC_Image $image): void {
if ($room->getType() === Room::TYPE_ONE_TO_ONE) {
throw new InvalidArgumentException($this->l->t('One to one rooms always need to show the other users avatar'));
}
$image->fixOrientation();
if (!($image->height() === $image->width())) {
throw new InvalidArgumentException($this->l->t('Avatar image is not square'));

View file

@ -167,11 +167,6 @@
<code>null</code>
</NullArgument>
</file>
<file src="lib/Controller/AvatarController.php">
<UndefinedClass occurrences="1">
<code>Filesystem</code>
</UndefinedClass>
</file>
<file src="lib/Controller/ChatController.php">
<InvalidArrayOffset occurrences="1">
<code>$commentIdToIndex[$parentId]</code>
@ -259,7 +254,8 @@
</UndefinedClass>
</file>
<file src="lib/Service/AvatarService.php">
<UndefinedClass occurrences="1">
<UndefinedClass occurrences="2">
<code>Filesystem</code>
<code>\OC_Image</code>
</UndefinedClass>
</file>