From ae2654e15e783756615d400ed543a270a0f825eb Mon Sep 17 00:00:00 2001 From: Julius Knorr Date: Thu, 19 Sep 2024 00:59:57 +0200 Subject: [PATCH] chore: Move to attributes in WopiController Signed-off-by: Julius Knorr --- appinfo/routes.php | 7 --- lib/Controller/WopiController.php | 82 +++++++++++-------------------- 2 files changed, 29 insertions(+), 60 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 49986d47a9..f5c7155010 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -23,13 +23,6 @@ // external api access ['name' => 'document#extAppGetData', 'url' => '/ajax/extapp/data/{fileId}', 'verb' => 'POST'], - // WOPI access - ['name' => 'wopi#checkFileInfo', 'url' => 'wopi/files/{fileId}', 'verb' => 'GET'], - ['name' => 'wopi#getFile', 'url' => 'wopi/files/{fileId}/contents', 'verb' => 'GET'], - ['name' => 'wopi#putFile', 'url' => 'wopi/files/{fileId}/contents', 'verb' => 'POST'], - ['name' => 'wopi#postFile', 'url' => 'wopi/files/{fileId}', 'verb' => 'POST'], - ['name' => 'wopi#getTemplate', 'url' => 'wopi/template/{fileId}', 'verb' => 'GET'], - // Settings ['name' => 'settings#setPersonalSettings', 'url' => 'ajax/personal.php', 'verb' => 'POST'], ['name' => 'settings#setSettings', 'url' => 'ajax/admin.php', 'verb' => 'POST'], diff --git a/lib/Controller/WopiController.php b/lib/Controller/WopiController.php index 25e0f71046..1c900cca58 100644 --- a/lib/Controller/WopiController.php +++ b/lib/Controller/WopiController.php @@ -21,8 +21,11 @@ use OCA\Richdocuments\TemplateManager; use OCA\Richdocuments\TokenManager; use OCP\AppFramework\Controller; -use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\Attribute\FrontpageRoute; +use OCP\AppFramework\Http\Attribute\NoAdminRequired; +use OCP\AppFramework\Http\Attribute\NoCSRFRequired; +use OCP\AppFramework\Http\Attribute\PublicPage; use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\StreamResponse; use OCP\AppFramework\QueryException; @@ -32,7 +35,6 @@ use OCP\Files\File; use OCP\Files\Folder; use OCP\Files\GenericFileException; -use OCP\Files\InvalidPathException; use OCP\Files\IRootFolder; use OCP\Files\Lock\ILock; use OCP\Files\Lock\ILockManager; @@ -41,7 +43,6 @@ use OCP\Files\Lock\OwnerLockedException; use OCP\Files\Node; use OCP\Files\NotFoundException; -use OCP\Files\NotPermittedException; use OCP\IConfig; use OCP\IGroupManager; use OCP\IRequest; @@ -89,18 +90,12 @@ public function __construct( /** * Returns general info about a file. - * - * @NoAdminRequired - * @NoCSRFRequired - * @PublicPage - * - * @param string $fileId - * @param string $access_token - * @return JSONResponse - * @throws InvalidPathException - * @throws NotFoundException */ - public function checkFileInfo($fileId, $access_token) { + #[NoAdminRequired] + #[NoCSRFRequired] + #[PublicPage] + #[FrontpageRoute(verb: 'GET', url: 'wopi/files/{fileId}')] + public function checkFileInfo(string $fileId, string $access_token): JSONResponse { try { [$fileId, , $version] = Helper::parseFileId($fileId); @@ -218,7 +213,7 @@ public function checkFileInfo($fileId, $access_token) { $response = array_merge($response, $this->appConfig->getWopiOverride()); $this->eventDispatcher->dispatchTyped(new DocumentOpenedEvent( - $user ? $user->getUID() : null, + $user?->getUID(), $file )); @@ -268,20 +263,12 @@ private function setFederationFileInfo(Wopi $wopi, $response) { /** * Given an access token and a fileId, returns the contents of the file. - * Expects a valid token in access_token parameter. - * - * @PublicPage - * @NoCSRFRequired - * - * @param string $fileId - * @param string $access_token - * @return Http\Response - * @throws NotFoundException - * @throws NotPermittedException - * @throws LockedException */ - public function getFile($fileId, - $access_token) { + #[NoAdminRequired] + #[NoCSRFRequired] + #[PublicPage] + #[FrontpageRoute(verb: 'GET', url: 'wopi/files/{fileId}/contents')] + public function getFile(string $fileId, string $access_token): JSONResponse|StreamResponse { [$fileId, , $version] = Helper::parseFileId($fileId); try { @@ -360,16 +347,12 @@ public function getFile($fileId, /** * Given an access token and a fileId, replaces the files with the request body. * Expects a valid token in access_token parameter. - * - * @PublicPage - * @NoCSRFRequired - * - * @param string $fileId - * @param string $access_token - * @return JSONResponse */ - public function putFile($fileId, - $access_token) { + #[NoAdminRequired] + #[NoCSRFRequired] + #[PublicPage] + #[FrontpageRoute(verb: 'POST', url: 'wopi/files/{fileId}/contents')] + public function putFile(string $fileId, string $access_token): JSONResponse { [$fileId, , ] = Helper::parseFileId($fileId); $isPutRelative = ($this->request->getHeader('X-WOPI-Override') === 'PUT_RELATIVE'); @@ -490,15 +473,11 @@ public function putFile($fileId, * handles both saving and saving as.* Given an access token and a fileId, replaces the files with the request body. * * FIXME Cleanup this code as is a lot of shared logic between putFile and putRelativeFile - * - * @PublicPage - * @NoCSRFRequired - * - * @param string $fileId - * @param string $access_token - * @return JSONResponse - * @throws DoesNotExistException */ + #[NoAdminRequired] + #[NoCSRFRequired] + #[PublicPage] + #[FrontpageRoute(verb: 'POST', url: 'wopi/files/{fileId}')] public function postFile(string $fileId, string $access_token): JSONResponse { try { $wopiOverride = $this->request->getHeader('X-WOPI-Override'); @@ -805,15 +784,12 @@ private function getShareForWopiToken(Wopi $wopi): ?IShare { /** * Endpoint to return the template file that is requested by collabora to create a new document - * - * @PublicPage - * @NoCSRFRequired - * - * @param $fileId - * @param $access_token - * @return JSONResponse|StreamResponse */ - public function getTemplate($fileId, $access_token) { + #[NoAdminRequired] + #[NoCSRFRequired] + #[PublicPage] + #[FrontpageRoute(verb: 'GET', url: 'wopi/template/{fileId}')] + public function getTemplate(string $fileId, string $access_token): JSONResponse|StreamResponse { try { $wopi = $this->wopiMapper->getWopiForToken($access_token); } catch (UnknownTokenException $e) {