Skip to content

Commit

Permalink
Merge pull request #3580 from nextcloud/fix/wopi-callback-url
Browse files Browse the repository at this point in the history
fix: Use wopi callback url for all urls handed over to Collabora
  • Loading branch information
juliusknorr authored Apr 12, 2024
2 parents 8efeb16 + 2cc2536 commit 1c0dee5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
30 changes: 16 additions & 14 deletions lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ public function checkFileInfo($fileId, $access_token) {
$response['UserPrivateInfo']['ZoteroAPIKey'] = $zoteroAPIKey;
}
if ($wopi->hasTemplateId()) {
$templateUrl = 'index.php/apps/richdocuments/wopi/template/' . $wopi->getTemplateId() . '?access_token=' . $wopi->getToken();
$templateUrl = $this->urlGenerator->getAbsoluteURL($templateUrl);
$response['TemplateSource'] = $templateUrl;
$response['TemplateSource'] = $this->getWopiUrlForTemplate($wopi);
} elseif ($wopi->isTemplateToken()) {
// FIXME: Remove backward compatibility layer once TemplateSource is available in all supported Collabora versions
$userFolder = $this->rootFolder->getUserFolder($wopi->getOwnerUid());
Expand Down Expand Up @@ -283,7 +281,7 @@ private function setFederationFileInfo(Wopi $wopi, $response) {
$response['TemplateSource'] = $templateUrl;
}
if ($wopi->getTokenType() === Wopi::TOKEN_TYPE_REMOTE_USER || ($wopi->getTokenType() === Wopi::TOKEN_TYPE_REMOTE_GUEST && $initiator->getEditorUid())) {
$response['UserExtraInfo']['avatar'] = $wopi->getRemoteServer() . '/index.php/avatar/' . $initiator->getEditorUid() . '/'. self::WOPI_AVATAR_SIZE;
$response['UserExtraInfo']['avatar'] = $wopi->getRemoteServer() . '/index.php/avatar/' . $initiator->getEditorUid() . '/' . self::WOPI_AVATAR_SIZE;
}

return $response;
Expand Down Expand Up @@ -503,11 +501,7 @@ public function putFile($fileId,
if ($isPutRelative) {
// generate a token for the new file (the user still has to be logged in)
$wopi = $this->tokenManager->generateWopiToken((string)$file->getId(), null, $wopi->getEditorUid(), $wopi->getDirect());

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

return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $wopiUrl ], Http::STATUS_OK);
return new JSONResponse(['Name' => $file->getName(), 'Url' => $this->getWopiUrlForFile($wopi, $file)], Http::STATUS_OK);
}
if ($wopi->hasTemplateId()) {
$wopi->setTemplateId(null);
Expand Down Expand Up @@ -671,17 +665,14 @@ public function postFile(string $fileId, string $access_token): JSONResponse {

// epub is exception (can be uploaded but not opened so don't try to get access token)
if ($file->getMimeType() == 'application/epub+zip') {
return new JSONResponse([ 'Name' => $file->getName() ], Http::STATUS_OK);
return new JSONResponse(['Name' => $file->getName()], Http::STATUS_OK);
}

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

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

return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $wopiUrl ], Http::STATUS_OK);
return new JSONResponse(['Name' => $file->getName(), 'Url' => $this->getWopiUrlForFile($wopi, $file)], Http::STATUS_OK);
} catch (NotFoundException $e) {
$this->logger->warning($e->getMessage(), ['exception' => $e]);
return new JSONResponse([], Http::STATUS_NOT_FOUND);
Expand Down Expand Up @@ -712,6 +703,7 @@ private function lock(Wopi $wopi, string $lock): JSONResponse {
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}

private function unlock(Wopi $wopi, string $lock): JSONResponse {
try {
$this->lockManager->unlock(new LockContext(
Expand Down Expand Up @@ -895,4 +887,14 @@ private function isMasterKeyEnabled(): bool {
return false;
}
}

private function getWopiUrlForFile(Wopi $wopi, File $file): string {
$nextcloudUrl = $this->appConfig->getNextcloudUrl() ?: trim($this->urlGenerator->getAbsoluteURL(''), '/');
return $nextcloudUrl . '/index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopi->getToken();
}

private function getWopiUrlForTemplate(Wopi $wopi): string {
$nextcloudUrl = $this->appConfig->getNextcloudUrl() ?: trim($this->urlGenerator->getAbsoluteURL(''), '/');
return $nextcloudUrl . '/index.php/apps/richdocuments/wopi/template/' . $wopi->getTemplateId() . '?access_token=' . $wopi->getToken();
}
}
2 changes: 1 addition & 1 deletion src/helpers/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const getCallbackBaseUrl = () => {
}

const getWopiSrc = (fileId) => {
// WOPISrc - URL that loolwsd will access (ie. pointing to ownCloud)
// WOPISrc - URL that Collabora will use to access Nextcloud
// index.php is forced here to avoid different wopi srcs for the same document
const wopiurl = getCallbackBaseUrl() + '/index.php/apps/richdocuments/wopi/files/' + fileId
console.debug('[getWopiUrl] ' + wopiurl)
Expand Down

0 comments on commit 1c0dee5

Please sign in to comment.