Skip to content

Commit

Permalink
chore: Cleanup old TemplateSaveAs code as this is no longer supported…
Browse files Browse the repository at this point in the history
… by Collabora

Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Apr 15, 2024
1 parent 28690d7 commit 1b5989e
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 81 deletions.
2 changes: 1 addition & 1 deletion lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public function getCapabilities() {
'mimetypesSecureView' => $this->config->useSecureViewAdditionalMimes() ? self::SECURE_VIEW_ADDITIONAL_MIMES : [],
'collabora' => $collaboraCapabilities,
'direct_editing' => ($collaboraCapabilities['hasMobileSupport'] ?? false) && $this->config->getAppValue('mobile_editing', 'yes') === 'yes',
'templates' => ($collaboraCapabilities['hasTemplateSaveAs'] ?? false) || ($collaboraCapabilities['hasTemplateSource'] ?? false),
'templates' => ($collaboraCapabilities['hasTemplateSource'] ?? false),
'productName' => $this->capabilitiesService->getProductName(),
'editonline_endpoint' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.document.editOnline'),
'config' => [
Expand Down
52 changes: 17 additions & 35 deletions lib/Controller/DirectViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,48 +94,30 @@ public function show($token) {
$this->userScopeService->setFilesystemScope($direct->getUid());

$folder = $this->rootFolder->getUserFolder($direct->getUid());
if ($this->templateManager->isTemplate($direct->getFileid())) {
$item = $this->templateManager->get($direct->getFileid());
if ($direct->getTemplateDestination() === 0 || $direct->getTemplateDestination() === null) {
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
}

try {
$urlSrc = $this->tokenManager->getUrlSrc($item);

$wopi = $this->tokenManager->generateWopiTokenForTemplate($item, $direct->getUid(), $direct->getTemplateDestination(), true);

$targetFile = $folder->getById($direct->getTemplateDestination())[0];
$relativePath = $folder->getRelativePath($targetFile->getPath());
} catch (\Exception $e) {
$this->logger->error('Failed to generate token for new file on direct editing', ['exception' => $e]);
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
try {
$item = $folder->getById($direct->getFileid())[0];
if (!($item instanceof Node)) {
throw new \Exception();
}
} else {
try {
$item = $folder->getById($direct->getFileid())[0];
if (!($item instanceof Node)) {
throw new \Exception();
}

/** Open file from remote collabora */
$federatedUrl = $this->federationService->getRemoteRedirectURL($item, $direct);
if ($federatedUrl !== null) {
$response = new RedirectResponse($federatedUrl);
$response->addHeader('X-Frame-Options', 'ALLOW');
return $response;
}

$urlSrc = $this->tokenManager->getUrlSrc($item);
$wopi = $this->tokenManager->generateWopiToken($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.');
/** Open file from remote collabora */
$federatedUrl = $this->federationService->getRemoteRedirectURL($item, $direct);
if ($federatedUrl !== null) {
$response = new RedirectResponse($federatedUrl);
$response->addHeader('X-Frame-Options', 'ALLOW');
return $response;
}

$relativePath = $folder->getRelativePath($item->getPath());
$urlSrc = $this->tokenManager->getUrlSrc($item);
$wopi = $this->tokenManager->generateWopiToken($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.');
}

$relativePath = $folder->getRelativePath($item->getPath());

try {
$params = [
'permissions' => $item->getPermissions(),
Expand Down
27 changes: 2 additions & 25 deletions lib/Controller/WopiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,7 @@ public function checkFileInfo($fileId, $access_token) {
[$fileId, , $version] = Helper::parseFileId($fileId);

$wopi = $this->wopiMapper->getWopiForToken($access_token);
if ($wopi->isTemplateToken()) {
$this->templateManager->setUserId($wopi->getOwnerUid());
$file = $this->templateManager->get($wopi->getFileid());
} else {
$file = $this->getFileForWopiToken($wopi);
}
$file = $this->getFileForWopiToken($wopi);
if (!($file instanceof File)) {
throw new NotFoundException('No valid file found for ' . $fileId);
}
Expand Down Expand Up @@ -195,11 +190,6 @@ public function checkFileInfo($fileId, $access_token) {
}
if ($wopi->hasTemplateId()) {
$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());
$file = $userFolder->getById($wopi->getTemplateDestination())[0];
$response['TemplateSaveAs'] = $file->getName();
}

$share = $this->getShareForWopiToken($wopi);
Expand Down Expand Up @@ -322,16 +312,6 @@ public function getFile($fileId,
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

// Template is just returned as there is no version logic
if ($wopi->isTemplateToken()) {
$this->templateManager->setUserId($wopi->getOwnerUid());
$file = $this->templateManager->get($wopi->getFileid());
$response = new StreamResponse($file->fopen('rb'));
$response->addHeader('Content-Disposition', 'attachment');
$response->addHeader('Content-Type', 'application/octet-stream');
return $response;
}

try {
/** @var File $file */
$file = $this->getFileForWopiToken($wopi);
Expand Down Expand Up @@ -585,10 +565,7 @@ public function postFile(string $fileId, string $access_token): JSONResponse {
// the new file needs to be installed in the current user dir
$userFolder = $this->rootFolder->getUserFolder($editor);

if ($wopi->isTemplateToken()) {
$this->templateManager->setUserId($wopi->getOwnerUid());
$file = $userFolder->getById($wopi->getTemplateDestination())[0];
} elseif ($isRenameFile) {
if ($isRenameFile) {
// the new file needs to be installed in the current user dir
$file = $this->getFileForWopiToken($wopi);

Expand Down
4 changes: 0 additions & 4 deletions lib/Db/Wopi.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ public function __construct() {
$this->addType('tokenType', 'int');
}

public function isTemplateToken() {
return $this->getTemplateDestination() !== 0 && $this->getTemplateDestination() !== null;
}

public function hasTemplateId() {
return $this->getTemplateId() !== 0 && $this->getTemplateId() !== null;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Db/WopiMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct(IDBConnection $db,
* @param int $templateDestination
* @return Wopi
*/
public function generateFileToken($fileId, $owner, $editor, $version, $updatable, $serverHost, $guestDisplayname = null, $templateDestination = 0, $hideDownload = false, $direct = false, $templateId = 0, $share = null) {
public function generateFileToken($fileId, $owner, $editor, $version, $updatable, $serverHost, $guestDisplayname = null, $hideDownload = false, $direct = false, $templateId = 0, $share = null) {
$token = $this->random->generate(32, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);

$wopi = Wopi::fromParams([
Expand All @@ -83,7 +83,6 @@ public function generateFileToken($fileId, $owner, $editor, $version, $updatable
'token' => $token,
'expiry' => $this->calculateNewTokenExpiry(),
'guestDisplayname' => $guestDisplayname,
'templateDestination' => $templateDestination,
'hideDownload' => $hideDownload,
'direct' => $direct,
'templateId' => $templateId,
Expand Down
4 changes: 0 additions & 4 deletions lib/Service/CapabilitiesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ public function hasDrawSupport(): bool {
return version_compare($productVersion, '6.4.7', '>=');
}

public function hasTemplateSaveAs(): bool {
return $this->getCapabilities()['hasTemplateSaveAs'] ?? false;
}

public function hasTemplateSource(): bool {
return $this->getCapabilities()['hasTemplateSource'] ?? false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getForm(): TemplateResponse {
'canonical_webroot' => $this->config->getAppValue('richdocuments', 'canonical_webroot'),
'disable_certificate_verification' => $this->config->getAppValue('richdocuments', 'disable_certificate_verification', '') === 'yes',
'templates' => $this->manager->getSystemFormatted(),
'templatesAvailable' => $this->capabilitiesService->hasTemplateSaveAs() || $this->capabilitiesService->hasTemplateSource(),
'templatesAvailable' => $this->capabilitiesService->hasTemplateSource(),
'settings' => $this->appConfig->getAppSettings(),
'demo_servers' => $this->demoService->fetchDemoServers(),
'web_server' => strtolower($_SERVER['SERVER_SOFTWARE']),
Expand Down
4 changes: 2 additions & 2 deletions lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(IConfig $config, CapabilitiesService $capabilitiesSe

/** @psalm-suppress InvalidNullableReturnType */
public function getForm() {
if (!$this->capabilitiesService->hasTemplateSaveAs() && !$this->capabilitiesService->hasTemplateSource()) {
if (!$this->capabilitiesService->hasTemplateSource()) {
/** @psalm-suppress NullableReturnStatement */
return null;
}
Expand All @@ -70,7 +70,7 @@ public function getForm() {
}

public function getSection() {
if (!$this->capabilitiesService->hasTemplateSaveAs() && !$this->capabilitiesService->hasTemplateSource()) {
if (!$this->capabilitiesService->hasTemplateSource()) {
return null;
}

Expand Down
10 changes: 3 additions & 7 deletions lib/TokenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function generateWopiToken(string $fileId, ?string $shareToken = null, ?s

$serverHost = $this->urlGenerator->getAbsoluteURL('/');
$guestName = $this->userId === null ? $this->prepareGuestName($this->helper->getGuestNameFromCookie()) : null;
return $this->wopiMapper->generateFileToken($fileId, $owneruid, $editoruid, $version, $updatable, $serverHost, $guestName, 0, $hideDownload, $direct, 0, $shareToken);
return $this->wopiMapper->generateFileToken($fileId, $owneruid, $editoruid, $version, $updatable, $serverHost, $guestName, $hideDownload, $direct, 0, $shareToken);
}

/**
Expand Down Expand Up @@ -255,12 +255,8 @@ public function generateWopiTokenForTemplate(File $templateFile, ?string $userId

$serverHost = $this->urlGenerator->getAbsoluteURL('/');

if ($this->capabilitiesService->hasTemplateSource()) {
return $this->wopiMapper->generateFileToken($targetFile->getId(), $owneruid, $editoruid, 0, $updatable, $serverHost, null, 0, false, $direct, $templateFile->getId());
}

// Legacy way of creating new documents from a template
return $this->wopiMapper->generateFileToken($templateFile->getId(), $owneruid, $editoruid, 0, $updatable, $serverHost, null, $targetFile->getId(), $direct);
return $this->wopiMapper->generateFileToken($targetFile->getId(), $owneruid, $editoruid, 0, $updatable, $serverHost, null,
false, $direct, $templateFile->getId());
}

public function newInitiatorToken($sourceServer, ?Node $node = null, $shareToken = null, bool $direct = false, $userId = null): Wopi {
Expand Down

0 comments on commit 1b5989e

Please sign in to comment.