Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
Signed-off-by: Hoang Pham <hoangmaths96@gmail.com>
  • Loading branch information
hweihwang committed Aug 30, 2024
1 parent 8e2d57b commit fbccf25
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 124 deletions.
11 changes: 6 additions & 5 deletions lib/Controller/JWTController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Exception;
use OCA\Whiteboard\Service\Authentication\AuthenticateUserServiceFactory;
use OCA\Whiteboard\Service\ExceptionService;
use OCA\Whiteboard\Service\FileService;
use OCA\Whiteboard\Service\File\GetFileServiceFactory;
use OCA\Whiteboard\Service\JWTService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
Expand All @@ -25,7 +25,7 @@
final class JWTController extends Controller {
public function __construct(
IRequest $request,
private FileService $fileService,
private GetFileServiceFactory $getFileServiceFactory,
private JWTService $jwtService,
private ExceptionService $exceptionService,
private AuthenticateUserServiceFactory $authenticateUserServiceFactory
Expand All @@ -42,11 +42,12 @@ public function getJWT(int $fileId): DataResponse {
try {
$publicSharingToken = $this->request->getParam('publicSharingToken');

$authService = $this->authenticateUserServiceFactory->create($publicSharingToken);
$user = $this->authenticateUserServiceFactory->create($publicSharingToken)->authenticate();

$file = $this->getFileServiceFactory->create($user, $fileId)->getFile();

$user = $authService->authenticate();
$file = $this->fileService->getFile($user, $fileId);
$jwt = $this->jwtService->generateJWT($user, $file);

return new DataResponse(['token' => $jwt]);
} catch (Exception $e) {
return $this->exceptionService->handleException($e);
Expand Down
24 changes: 16 additions & 8 deletions lib/Controller/WhiteboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use OCA\Whiteboard\Service\Authentication\GetUserFromIdServiceFactory;
use OCA\Whiteboard\Service\ConfigService;
use OCA\Whiteboard\Service\ExceptionService;
use OCA\Whiteboard\Service\FileService;
use OCA\Whiteboard\Service\File\GetFileServiceFactory;
use OCA\Whiteboard\Service\JWTService;
use OCA\Whiteboard\Service\WhiteboardContentService;
use OCP\AppFramework\ApiController;
Expand All @@ -34,7 +34,7 @@ public function __construct(
$appName,
IRequest $request,
private GetUserFromIdServiceFactory $getUserFromIdServiceFactory,
private FileService $fileService,
private GetFileServiceFactory $getFileServiceFactory,
private JWTService $jwtService,
private WhiteboardContentService $contentService,
private ExceptionService $exceptionService,
Expand All @@ -49,11 +49,15 @@ public function __construct(
public function show(int $fileId): DataResponse {
try {
$jwt = $this->getJwtFromRequest();

$userId = $this->jwtService->getUserIdFromJWT($jwt);
$getUserService = $this->getUserFromIdServiceFactory->create($userId);
$user = $getUserService->getUser();
$file = $this->fileService->getFile($user, $fileId);

$user = $this->getUserFromIdServiceFactory->create($userId)->getUser();

$file = $this->getFileServiceFactory->create($user, $fileId)->getFile();

$data = $this->contentService->getContent($file);

return new DataResponse(['data' => $data]);
} catch (Exception $e) {
return $this->exceptionService->handleException($e);
Expand All @@ -66,11 +70,15 @@ public function show(int $fileId): DataResponse {
public function update(int $fileId, array $data): DataResponse {
try {
$this->validateBackendSharedToken($fileId);

$userId = $this->getUserIdFromRequest();
$getUserService = $this->getUserFromIdServiceFactory->create($userId);
$user = $getUserService->getUser();
$file = $this->fileService->getFile($user, $fileId);

$user = $this->getUserFromIdServiceFactory->create($userId)->getUser();

$file = $this->getFileServiceFactory->create($user, $fileId)->getFile();

$this->contentService->updateContent($file, $data);

return new DataResponse(['status' => 'success']);
} catch (Exception $e) {
return $this->exceptionService->handleException($e);
Expand Down
63 changes: 63 additions & 0 deletions lib/Service/File/GetFileFromIdService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Whiteboard\Service\File;

use OC\User\NoUserException;
use OCP\Constants;
use OCP\Files\File;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;

final class GetFileFromIdService implements GetFileService {
public function __construct(
private IRootFolder $rootFolder,

Check failure on line 23 in lib/Service/File/GetFileFromIdService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MissingDependency

lib/Service/File/GetFileFromIdService.php:23:3: MissingDependency: OCP\Files\IRootFolder depends on class or interface oc\hooks\emitter that does not exist (see https://psalm.dev/157)
private string $userId,
private int $fileId
) {
}

/**
* @throws NotFoundException
* @throws NotPermittedException
* @throws NoUserException

Check failure on line 32 in lib/Service/File/GetFileFromIdService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedDocblockClass

lib/Service/File/GetFileFromIdService.php:32:13: UndefinedDocblockClass: Docblock-defined class, interface or enum named OC\User\NoUserException does not exist (see https://psalm.dev/200)
* @throws InvalidPathException
*/
public function getFile(): File {
$userFolder = $this->rootFolder->getUserFolder($this->userId);

Check failure on line 36 in lib/Service/File/GetFileFromIdService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MissingDependency

lib/Service/File/GetFileFromIdService.php:36:17: MissingDependency: OCP\Files\IRootFolder depends on class or interface oc\hooks\emitter that does not exist (see https://psalm.dev/157)

$file = $userFolder->getFirstNodeById($this->fileId);
if ($file instanceof File && $file->getPermissions() & Constants::PERMISSION_UPDATE) {
return $file;
}

$files = $userFolder->getById($this->fileId);
if (empty($files)) {
throw new NotFoundException('File not found');
}

usort($files, static function (Node $a, Node $b) {
return ($b->getPermissions() & Constants::PERMISSION_UPDATE) <=> ($a->getPermissions() & Constants::PERMISSION_UPDATE);
});

$file = $files[0];
if (!$file instanceof File) {
throw new NotFoundException('Not a file');
}

if (!($file->getPermissions() & Constants::PERMISSION_READ)) {
throw new NotPermittedException('No read permission');
}

return $file;
}
}
43 changes: 43 additions & 0 deletions lib/Service/File/GetFileFromPublicSharingTokenService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Whiteboard\Service\File;

use InvalidArgumentException;
use OCP\Files\File;
use OCP\Files\NotFoundException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager as ShareManager;

final class GetFileFromPublicSharingTokenService implements GetFileService {
public function __construct(
private ShareManager $shareManager,
private string $publicSharingToken
) {
}

/**
* @throws NotFoundException
*/
public function getFile(): File {
try {
$share = $this->shareManager->getShareByToken($this->publicSharingToken);
} catch (ShareNotFound) {
throw new NotFoundException();
}

$node = $share->getNode();

if ($node instanceof File) {
return $node;
}

throw new InvalidArgumentException('No proper share data');
}
}
16 changes: 16 additions & 0 deletions lib/Service/File/GetFileService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Whiteboard\Service\File;

use OCP\Files\File;

interface GetFileService {
public function getFile(): File;
}
37 changes: 37 additions & 0 deletions lib/Service/File/GetFileServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Whiteboard\Service\File;

use OCA\Whiteboard\Exception\InvalidUserException;
use OCA\Whiteboard\Model\AuthenticatedUser;
use OCA\Whiteboard\Model\PublicSharingUser;
use OCA\Whiteboard\Model\User;
use OCP\Files\IRootFolder;
use OCP\Share\IManager as ShareManager;

final class GetFileServiceFactory {
public function __construct(
private IRootFolder $rootFolder,

Check failure on line 21 in lib/Service/File/GetFileServiceFactory.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MissingDependency

lib/Service/File/GetFileServiceFactory.php:21:3: MissingDependency: OCP\Files\IRootFolder depends on class or interface oc\hooks\emitter that does not exist (see https://psalm.dev/157)
private ShareManager $shareManager
) {
}

public function create(User $user, int $fileId): GetFileService {
if ($user instanceof AuthenticatedUser) {
return new GetFileFromIdService($this->rootFolder, $user->getUID(), $fileId);
}

if ($user instanceof PublicSharingUser) {
return new GetFileFromPublicSharingTokenService($this->shareManager, $user->getPublicSharingToken());
}

throw new InvalidUserException();
}
}
111 changes: 0 additions & 111 deletions lib/Service/FileService.php

This file was deleted.

0 comments on commit fbccf25

Please sign in to comment.