Skip to content

Commit

Permalink
fixup! fixup! chore: refactor iframes to load collabora directly
Browse files Browse the repository at this point in the history
  • Loading branch information
juliushaertl committed Aug 31, 2023
1 parent 3049ae2 commit be7c20f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
6 changes: 3 additions & 3 deletions lib/Controller/DirectViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function show($token) {
return $response;
}

list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($item->getId(), null, $direct->getUid(), true);
list($urlSrc, $wopi) = $this->tokenManager->getToken($item->getId(), null, $direct->getUid(), true);
} catch (\Exception $e) {
$this->logger->error('Failed to generate token for existing file on direct editing', ['exception' => $e]);
return $this->renderErrorPage('Failed to open the requested file.');
Expand Down Expand Up @@ -218,11 +218,11 @@ public function showPublicShare(Direct $direct) {
'directGuest' => empty($direct->getUid()),
];

list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($node->getId(), $direct->getShare(), $direct->getUid(), true);
list($urlSrc, $wopi) = $this->tokenManager->getToken($node->getId(), $direct->getShare(), $direct->getUid(), true);
if (!empty($direct->getInitiatorHost())) {
$this->tokenManager->upgradeFromDirectInitiator($direct, $wopi);
}
$params['token'] = $token;
$params['token'] = $wopi->getToken();
$params['token_ttl'] = $wopi->getExpiry();
$params['urlsrc'] = $urlSrc;

Expand Down
22 changes: 14 additions & 8 deletions lib/Controller/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public function extAppGetData(int $fileId): array {
if (!($item instanceof Node)) {
throw new Exception();
}
list($urlSrc, $token) = $this->tokenManager->getToken($item->getId());
list($urlSrc, $wopi) = $this->tokenManager->getToken($item->getId());
return [
'status' => 'success',
'urlsrc' => $urlSrc,
'token' => $token
'token' => $wopi->getToken()
];
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
Expand Down Expand Up @@ -147,14 +147,14 @@ public function index($fileId, ?string $path = null) {
list($urlSrc, $wopi) = $this->tokenManager->getTokenForTemplate($templateFile, $this->userId, $item->getId());
$token = $wopi->getToken();
} else {
list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($item->getId());
list($urlSrc, $wopi) = $this->tokenManager->getToken($item->getId());
}

$params = [
'permissions' => $item->getPermissions(),
'title' => $item->getName(),
'fileId' => $item->getId() . '_' . $this->config->getSystemValue('instanceid'),
'token' => $token,
'token' => $wopi->getToken(),
'token_ttl' => $wopi->getExpiry(),
'urlsrc' => $urlSrc,
'path' => $folder->getRelativePath($item->getPath()),
Expand Down Expand Up @@ -284,7 +284,7 @@ public function publicPage($shareToken, $fileName, $fileId) {
if ($templateFile) {
list($urlSrc, $wopi) = $this->tokenManager->getTokenForTemplate($templateFile, $share->getShareOwner(), $item->getId());
} else {
list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($item->getId(), $shareToken, $this->userId);
list($urlSrc, $wopi) = $this->tokenManager->getToken($item->getId(), $shareToken, $this->userId);
}
$params['token'] = $wopi->getToken();
$params['token_ttl'] = $wopi->getExpiry();
Expand Down Expand Up @@ -335,7 +335,7 @@ public function remote($shareToken, $remoteServer, $remoteServerToken, $filePath
}

if ($node instanceof Node) {
list($urlSrc, $token, $wopi) = $this->tokenManager->getToken($node->getId(), $shareToken, $this->userId);
list($urlSrc, $wopi) = $this->tokenManager->getToken($node->getId(), $shareToken, $this->userId);

$remoteWopi = $this->federationService->getRemoteFileDetails($remoteServer, $remoteServerToken);
if ($remoteWopi === null) {
Expand All @@ -352,7 +352,7 @@ public function remote($shareToken, $remoteServer, $remoteServerToken, $filePath
'permissions' => $permissions,
'title' => $node->getName(),
'fileId' => $node->getId() . '_' . $this->config->getSystemValue('instanceid'),
'token' => $token,
'token' => $wopi->getToken(),
'token_ttl' => $wopi->getExpiry(),
'urlsrc' => $urlSrc,
'path' => '/',
Expand Down Expand Up @@ -462,7 +462,13 @@ public function editOnlineTarget(int $fileId, ?string $target = null) {
#[PublicPage]
public function token(int $fileId, ?string $shareToken = null): DataResponse {
try {
[$urlSrc, , $wopi] = $this->tokenManager->getToken((string)$fileId, $shareToken, $this->userId);
// Get file and share
$templateFile = $this->templateManager->getTemplateSource($fileId);
if ($templateFile) {
[$urlSrc, $wopi] = $this->tokenManager->getTokenForTemplate($templateFile, $share->getShareOwner(), $item->getId());

Check failure on line 468 in lib/Controller/DocumentController.php

View workflow job for this annotation

GitHub Actions / Nextcloud

UndefinedVariable

lib/Controller/DocumentController.php:468:80: UndefinedVariable: Cannot find referenced variable $share (see https://psalm.dev/024)

Check failure on line 468 in lib/Controller/DocumentController.php

View workflow job for this annotation

GitHub Actions / Nextcloud

UndefinedVariable

lib/Controller/DocumentController.php:468:105: UndefinedVariable: Cannot find referenced variable $item (see https://psalm.dev/024)
} else {
[$urlSrc, $wopi] = $this->tokenManager->getToken($fileId, $shareToken, $this->userId);

Check failure on line 470 in lib/Controller/DocumentController.php

View workflow job for this annotation

GitHub Actions / Nextcloud

InvalidScalarArgument

lib/Controller/DocumentController.php:470:54: InvalidScalarArgument: Argument 1 of OCA\Richdocuments\TokenManager::getToken expects string, but int provided (see https://psalm.dev/012)
}

return new DataResponse(array_merge(
[ 'urlSrc' => $urlSrc ],
Expand Down
3 changes: 2 additions & 1 deletion lib/Controller/DocumentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace OCA\Richdocuments\Controller;

use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Db\Wopi;
use OCP\AppFramework\Http\FeaturePolicy;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\EventDispatcher\IEventDispatcher;

trait DocumentTrait {
private $appConfig;
private AppConfig $appConfig;

private function documentTemplateResponse(Wopi $wopi, array $params): TemplateResponse {
$eventDispatcher = \OC::$server->get(IEventDispatcher::class);
Expand Down
8 changes: 4 additions & 4 deletions lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,9 @@ public function putFile($fileId,

if ($isPutRelative) {
// generate a token for the new file (the user still has to be logged in)
list(, $wopiToken) = $this->tokenManager->getToken((string)$file->getId(), null, $wopi->getEditorUid(), $wopi->getDirect());
list(, $wopi) = $this->tokenManager->getToken((string)$file->getId(), null, $wopi->getEditorUid(), $wopi->getDirect());

$wopi = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopiToken;
$wopi = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopi->getToken();
$url = $this->urlGenerator->getAbsoluteURL($wopi);

return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $url ], Http::STATUS_OK);
Expand Down Expand Up @@ -684,9 +684,9 @@ public function postFile(string $fileId, string $access_token): JSONResponse {

// generate a token for the new file (the user still has to be
// logged in)
list(, $wopiToken) = $this->tokenManager->getToken((string)$file->getId(), null, $wopi->getEditorUid(), $wopi->getDirect());
list(, $wopi) = $this->tokenManager->getToken((string)$file->getId(), null, $wopi->getEditorUid(), $wopi->getDirect());

$wopi = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopiToken;
$wopi = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopi->getToken();
$url = $this->urlGenerator->getAbsoluteURL($wopi);

return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $url ], Http::STATUS_OK);
Expand Down
29 changes: 18 additions & 11 deletions lib/Service/InitialStateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function provideCapabilities(): void {
$this->initialState->provideInitialState('hasDrawSupport', $this->capabilitiesService->hasDrawSupport());
$this->initialState->provideInitialState('hasNextcloudBranding', $this->capabilitiesService->hasNextcloudBranding());

$this->provideOptions();

$this->hasProvidedCapabilities = true;
}

Expand All @@ -76,17 +78,8 @@ public function provideDocument(Wopi $wopi, array $params): void {
$this->initialState->provideInitialState('document', $this->prepareParams($params));

$this->initialState->provideInitialState('wopi', $wopi);
$this->initialState->provideInitialState('theme', $this->config->getAppValue(Application::APPNAME, 'theme', 'nextcloud'));
$this->initialState->provideInitialState('uiDefaults', [
'UIMode' => $this->config->getAppValue(Application::APPNAME, 'uiDefaults-UIMode', 'notebookbar')
]);
$logoSet = $this->config->getAppValue('theming', 'logoheaderMime', '') !== '';
if (!$logoSet) {
$logoSet = $this->config->getAppValue('theming', 'logoMime', '') !== '';
}
$this->initialState->provideInitialState('theming-customLogo', ($logoSet ?
\OC::$server->getURLGenerator()->getAbsoluteURL(\OC::$server->getThemingDefaults()->getLogo())
: false));

$this->provideOptions();
}

public function prepareParams(array $params): array {
Expand All @@ -108,4 +101,18 @@ public function prepareParams(array $params): array {

return array_merge($defaults, $params);
}

private function provideOptions(): void {
$this->initialState->provideInitialState('theme', $this->config->getAppValue(Application::APPNAME, 'theme', 'nextcloud'));
$this->initialState->provideInitialState('uiDefaults', [
'UIMode' => $this->config->getAppValue(Application::APPNAME, 'uiDefaults-UIMode', 'notebookbar')
]);
$logoSet = $this->config->getAppValue('theming', 'logoheaderMime', '') !== '';
if (!$logoSet) {
$logoSet = $this->config->getAppValue('theming', 'logoMime', '') !== '';
}
$this->initialState->provideInitialState('theming-customLogo', ($logoSet ?
\OC::$server->getURLGenerator()->getAbsoluteURL(\OC::$server->getThemingDefaults()->getLogo())
: false));
}
}
3 changes: 1 addition & 2 deletions lib/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ public function getToken(string $fileId, $shareToken = null, $editoruid = null,

return [
$this->wopiParser->getUrlSrc($file->getMimeType())['urlsrc'], // url src might not be found ehre
$wopi->getToken(),
$wopi
];
}
Expand Down Expand Up @@ -281,7 +280,7 @@ public function getTokenForTemplate(File $templateFile, $userId, $targetFileId,

public function newInitiatorToken($sourceServer, Node $node = null, $shareToken = null, bool $direct = false, $userId = null): Wopi {
if ($node !== null) {
list($urlSrc, $token, $wopi) = $this->getToken($node->getId(), $shareToken, $userId, $direct);
list($urlSrc, $wopi) = $this->getToken($node->getId(), $shareToken, $userId, $direct);
$wopi->setServerHost($sourceServer);
$wopi->setTokenType(Wopi::TOKEN_TYPE_INITIATOR);
$this->wopiMapper->update($wopi);
Expand Down

0 comments on commit be7c20f

Please sign in to comment.