Skip to content

Commit

Permalink
WIP: wopi setting upload
Browse files Browse the repository at this point in the history
Signed-off-by: codewithvk <vivek.javiya@collabora.com>
  • Loading branch information
codewithvk committed Jan 7, 2025
1 parent 7fac5ca commit 104c780
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
44 changes: 44 additions & 0 deletions lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 0 additions & 24 deletions lib/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<fileId>_<instanceid>[_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 */
Expand Down

0 comments on commit 104c780

Please sign in to comment.