Skip to content

Commit

Permalink
Merge pull request #4101 from nextcloud/fix/direct-create
Browse files Browse the repository at this point in the history
fix: Use proper file id for direct editing
  • Loading branch information
juliusknorr authored Dec 6, 2024
2 parents 4e2387c + cf83444 commit 90e4852
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
53 changes: 53 additions & 0 deletions cypress/e2e/direct.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
* SPDX-FileCopyrightText: 2023 Julius Härtl <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
const getTemplates = (user, type) => {
return cy.request({
method: 'GET',
url: `${Cypress.env('baseUrl')}/ocs/v2.php/apps/richdocuments/api/v1/templates/${type}?format=json`,
auth: { user: user.userId, pass: user.password },
headers: {
'OCS-ApiRequest': 'true',
'Content-Type': 'application/x-www-form-urlencoded',
},
}).then(response => {
cy.log(response)
const templates = response.body?.ocs?.data
cy.wrap(templates)
})
}

const createDirectEditingLink = (user, fileId) => {
cy.login(user)
return cy.request({
Expand All @@ -24,6 +40,28 @@ const createDirectEditingLink = (user, fileId) => {
})
}

const createNewFileDirectEditingLink = (user, path, template) => {
cy.login(user)
return cy.request({
method: 'POST',
url: `${Cypress.env('baseUrl')}/ocs/v2.php/apps/richdocuments/api/v1/templates/new?format=json`,
form: true,
body: {
path, template,
},
// auth: { user: user.userId, pass: user.password },
headers: {
'OCS-ApiRequest': 'true',
'Content-Type': 'application/x-www-form-urlencoded',
},
}).then(response => {
cy.log(response)
const token = response.body?.ocs?.data?.url
cy.log(`Created direct editing token for ${user.userId}`, token)
cy.wrap(token)
})
}

const createDirectEditingLinkForShareToken = (shareToken, host = undefined, path = '', password = undefined) => {
cy.logout()
return cy.request({
Expand Down Expand Up @@ -75,6 +113,21 @@ describe('Direct editing (legacy)', function() {
})
})

it('Open an new file', function() {
getTemplates(randUser, 'document')
.then((templates) => {
const emptyTemplate = templates.find((template) => template.name === 'Empty')
cy.nextcloudTestingAppConfigSet('richdocuments', 'uiDefaults-UIMode', 'classic')
createNewFileDirectEditingLink(randUser, 'mynewfile.odt', emptyTemplate.id)
.then((token) => {
cy.logout()
cy.visit(token)
cy.waitForCollabora(false)
cy.screenshot('direct-new')
})
})
})

it('Open an existing file on a share link', function() {
cy.shareLink(randUser, '/document.odt').then((token) => {
createDirectEditingLinkForShareToken(token)
Expand Down
16 changes: 13 additions & 3 deletions lib/Controller/DirectViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
Expand Down Expand Up @@ -80,7 +81,7 @@ public function show($token) {

try {
$item = $folder->getFirstNodeById($direct->getFileid());
if (!($item instanceof Node)) {
if (!($item instanceof File)) {
throw new \Exception();
}

Expand All @@ -92,8 +93,17 @@ public function show($token) {
return $response;
}

$urlSrc = $this->tokenManager->getUrlSrc($item);
$wopi = $this->tokenManager->generateWopiToken($item->getId(), null, $direct->getUid(), true);
$wopi = null;
$template = $direct->getTemplateId() ? $this->templateManager->getTemplateSource($direct->getTemplateId()) : null;

if ($template !== null) {
$wopi = $this->tokenManager->generateWopiTokenForTemplate($template, $direct->getUid(), $item->getId(), true);
}

if ($wopi === null) {
$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.');
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/OCSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public function createFromTemplate($path, $template) {
$name = $folder->getNonExistingName($info['basename']);
$file = $folder->newFile($name);

$direct = $this->directMapper->newDirect($this->userId, $template, $file->getId());
$direct = $this->directMapper->newDirect($this->userId, $file->getId(), $template);

return new DataResponse([
'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/DirectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public function __construct(
* @param int $destination
* @return Direct
*/
public function newDirect($uid, $fileid, $destination = null, $share = null, $initiatorHost = null, $initiatorToken = null) {
public function newDirect($uid, $fileid, $template = null, $share = null, $initiatorHost = null, $initiatorToken = null) {
$direct = new Direct();
$direct->setUid($uid);
$direct->setFileid($fileid);
$direct->setToken($this->random->generate(64, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER));
$direct->setTimestamp($this->timeFactory->getTime());
$direct->setTemplateDestination($destination);
$direct->setTemplateId($template);
$direct->setShare($share);
$direct->setInitiatorHost($initiatorHost);
$direct->setInitiatorToken($initiatorToken);
Expand Down

0 comments on commit 90e4852

Please sign in to comment.