Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Admin setting iframe #4373

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
WOPI: Update the wopi setting upload route to accept a file and store…
… it to system-settings dir

Signed-off-by: codewithvk <vivek.javiya@collabora.com>
  • Loading branch information
codewithvk committed Jan 9, 2025
commit ce2ccba4ba8443a9d24ae356ed5059189a752862
27 changes: 13 additions & 14 deletions lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
use OCA\Richdocuments\Service\SettingsService;

#[RestrictToWopiServer]
class WopiController extends Controller {
Expand Down Expand Up @@ -84,6 +85,7 @@ public function __construct(
private IGroupManager $groupManager,
private ILockManager $lockManager,
private IEventDispatcher $eventDispatcher,
private SettingsService $settingsService,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -367,7 +369,7 @@ public function getFile(string $fileId, string $access_token): JSONResponse|Stre
#[NoAdminRequired]
#[NoCSRFRequired]
#[PublicPage]
#[FrontpageRoute(verb: 'POST', url: 'wopi/settings')]
#[FrontpageRoute(verb: 'POST', url: 'wopi/settings/upload')]
public function handleSettingsFile(string $access_token): JSONResponse {
try {
$wopi = $this->wopiMapper->getWopiForToken($access_token);
Expand All @@ -380,34 +382,31 @@ public function handleSettingsFile(string $access_token): JSONResponse {
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());
}
$newFileName = 'settings-' . uniqid() . '.json';

$result = $this->settingsService->uploadSystemFile($newFileName, $fileContent);

return new JSONResponse($jsonContent, Http::STATUS_OK);
return new JSONResponse([
'status' => 'success',
'filename' => $newFileName,
'details' => $result,
], 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