From 104c780733e7c79d8ba28a6731887e5af5d09bb4 Mon Sep 17 00:00:00 2001 From: codewithvk Date: Tue, 7 Jan 2025 22:15:52 +0530 Subject: [PATCH] WIP: wopi setting upload Signed-off-by: codewithvk --- lib/Controller/WopiController.php | 44 +++++++++++++++++++++++++++++++ lib/TokenManager.php | 24 ----------------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/lib/Controller/WopiController.php b/lib/Controller/WopiController.php index 1c232325a7..3c95904831 100644 --- a/lib/Controller/WopiController.php +++ b/lib/Controller/WopiController.php @@ -364,6 +364,50 @@ public function getFile(string $fileId, string $access_token): JSONResponse|Stre } } + #[NoAdminRequired] + #[NoCSRFRequired] + #[PublicPage] + #[FrontpageRoute(verb: 'POST', url: 'wopi/settings')] + public function handleSettingsFile(string $access_token): JSONResponse { + try { + $wopi = $this->wopiMapper->getWopiForToken($access_token); + + if ($wopi->getTokenType() !== Wopi::TOKEN_TYPE_SETTING_AUTH) { + return new JSONResponse(['error' => 'Invalid token type'], Http::STATUS_FORBIDDEN); + } + + $content = fopen('php://input', 'rb'); + if (!$content) { + throw new \Exception("Failed to read input stream."); + } + + $fileContent = stream_get_contents($content); + fclose($content); + + if (empty($fileContent)) { + throw new \Exception("No file content received."); + } + + $jsonContent = json_decode($fileContent, true); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new \Exception("Invalid JSON content: " . json_last_error_msg()); + } + + return new JSONResponse($jsonContent, Http::STATUS_OK); + + } catch (UnknownTokenException $e) { + $this->logger->debug($e->getMessage(), ['exception' => $e]); + return new JSONResponse(['error' => 'Invalid token'], Http::STATUS_FORBIDDEN); + } catch (ExpiredTokenException $e) { + $this->logger->debug($e->getMessage(), ['exception' => $e]); + return new JSONResponse(['error' => 'Token expired'], Http::STATUS_UNAUTHORIZED); + } catch (\Exception $e) { + $this->logger->error($e->getMessage(), ['exception' => $e]); + return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } + + /** * Given an access token and a fileId, replaces the files with the request body. * Expects a valid token in access_token parameter. diff --git a/lib/TokenManager.php b/lib/TokenManager.php index 2709c10d3e..19b2358671 100644 --- a/lib/TokenManager.php +++ b/lib/TokenManager.php @@ -60,30 +60,6 @@ public function generateWopiToken(string $fileId, ?string $shareToken = null, ?s [$fileId, , $version] = Helper::parseFileId($fileId); - // // // Parse docKey to extract fileId - // // // Usually docKey is something like "_[_version]" - // $parts = explode('_', $docKey); - // $fileId = (int)$parts[0]; - - // // If fileId is -1, this is the admin-settings scenario - // // Skip any file node lookup here and just proceed to generate a token - // if ($fileId === -1) { - // // Create a token without referencing a file node - // // Set fields as needed. No file-related constraints - // return $this->createToken([ - // 'fileid' => $fileId, - // 'editor' => $editorUid, - // 'canwrite' => true, // or false, depending on your needs - // 'hideDownload' => false, - // 'direct' => $direct, - // 'templateId' => null, - // 'version' => 0, - // 'server_host' => '', // fill as needed - // 'ownerUid' => $editorUid, - // ]); - // } - - // if the user is not logged-in do use the sharers storage if ($shareToken !== null) { /** @var File $file */