Skip to content

Commit 90e4852

Browse files
authored
Merge pull request #4101 from nextcloud/fix/direct-create
fix: Use proper file id for direct editing
2 parents 4e2387c + cf83444 commit 90e4852

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

cypress/e2e/direct.spec.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
* SPDX-FileCopyrightText: 2023 Julius Härtl <[email protected]>
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
5+
const getTemplates = (user, type) => {
6+
return cy.request({
7+
method: 'GET',
8+
url: `${Cypress.env('baseUrl')}/ocs/v2.php/apps/richdocuments/api/v1/templates/${type}?format=json`,
9+
auth: { user: user.userId, pass: user.password },
10+
headers: {
11+
'OCS-ApiRequest': 'true',
12+
'Content-Type': 'application/x-www-form-urlencoded',
13+
},
14+
}).then(response => {
15+
cy.log(response)
16+
const templates = response.body?.ocs?.data
17+
cy.wrap(templates)
18+
})
19+
}
20+
521
const createDirectEditingLink = (user, fileId) => {
622
cy.login(user)
723
return cy.request({
@@ -24,6 +40,28 @@ const createDirectEditingLink = (user, fileId) => {
2440
})
2541
}
2642

43+
const createNewFileDirectEditingLink = (user, path, template) => {
44+
cy.login(user)
45+
return cy.request({
46+
method: 'POST',
47+
url: `${Cypress.env('baseUrl')}/ocs/v2.php/apps/richdocuments/api/v1/templates/new?format=json`,
48+
form: true,
49+
body: {
50+
path, template,
51+
},
52+
// auth: { user: user.userId, pass: user.password },
53+
headers: {
54+
'OCS-ApiRequest': 'true',
55+
'Content-Type': 'application/x-www-form-urlencoded',
56+
},
57+
}).then(response => {
58+
cy.log(response)
59+
const token = response.body?.ocs?.data?.url
60+
cy.log(`Created direct editing token for ${user.userId}`, token)
61+
cy.wrap(token)
62+
})
63+
}
64+
2765
const createDirectEditingLinkForShareToken = (shareToken, host = undefined, path = '', password = undefined) => {
2866
cy.logout()
2967
return cy.request({
@@ -75,6 +113,21 @@ describe('Direct editing (legacy)', function() {
75113
})
76114
})
77115

116+
it('Open an new file', function() {
117+
getTemplates(randUser, 'document')
118+
.then((templates) => {
119+
const emptyTemplate = templates.find((template) => template.name === 'Empty')
120+
cy.nextcloudTestingAppConfigSet('richdocuments', 'uiDefaults-UIMode', 'classic')
121+
createNewFileDirectEditingLink(randUser, 'mynewfile.odt', emptyTemplate.id)
122+
.then((token) => {
123+
cy.logout()
124+
cy.visit(token)
125+
cy.waitForCollabora(false)
126+
cy.screenshot('direct-new')
127+
})
128+
})
129+
})
130+
78131
it('Open an existing file on a share link', function() {
79132
cy.shareLink(randUser, '/document.odt').then((token) => {
80133
createDirectEditingLinkForShareToken(token)

lib/Controller/DirectViewController.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use OCP\AppFramework\Http\JSONResponse;
2020
use OCP\AppFramework\Http\RedirectResponse;
2121
use OCP\AppFramework\Http\TemplateResponse;
22+
use OCP\Files\File;
2223
use OCP\Files\Folder;
2324
use OCP\Files\IRootFolder;
2425
use OCP\Files\Node;
@@ -80,7 +81,7 @@ public function show($token) {
8081

8182
try {
8283
$item = $folder->getFirstNodeById($direct->getFileid());
83-
if (!($item instanceof Node)) {
84+
if (!($item instanceof File)) {
8485
throw new \Exception();
8586
}
8687

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

95-
$urlSrc = $this->tokenManager->getUrlSrc($item);
96-
$wopi = $this->tokenManager->generateWopiToken($item->getId(), null, $direct->getUid(), true);
96+
$wopi = null;
97+
$template = $direct->getTemplateId() ? $this->templateManager->getTemplateSource($direct->getTemplateId()) : null;
98+
99+
if ($template !== null) {
100+
$wopi = $this->tokenManager->generateWopiTokenForTemplate($template, $direct->getUid(), $item->getId(), true);
101+
}
102+
103+
if ($wopi === null) {
104+
$urlSrc = $this->tokenManager->getUrlSrc($item);
105+
$wopi = $this->tokenManager->generateWopiToken($item->getId(), null, $direct->getUid(), true);
106+
}
97107
} catch (\Exception $e) {
98108
$this->logger->error('Failed to generate token for existing file on direct editing', ['exception' => $e]);
99109
return $this->renderErrorPage('Failed to open the requested file.');

lib/Controller/OCSController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function createFromTemplate($path, $template) {
278278
$name = $folder->getNonExistingName($info['basename']);
279279
$file = $folder->newFile($name);
280280

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

283283
return new DataResponse([
284284
'url' => $this->urlGenerator->linkToRouteAbsolute('richdocuments.directView.show', [

lib/Db/DirectMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public function __construct(
3131
* @param int $destination
3232
* @return Direct
3333
*/
34-
public function newDirect($uid, $fileid, $destination = null, $share = null, $initiatorHost = null, $initiatorToken = null) {
34+
public function newDirect($uid, $fileid, $template = null, $share = null, $initiatorHost = null, $initiatorToken = null) {
3535
$direct = new Direct();
3636
$direct->setUid($uid);
3737
$direct->setFileid($fileid);
3838
$direct->setToken($this->random->generate(64, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER));
3939
$direct->setTimestamp($this->timeFactory->getTime());
40-
$direct->setTemplateDestination($destination);
40+
$direct->setTemplateId($template);
4141
$direct->setShare($share);
4242
$direct->setInitiatorHost($initiatorHost);
4343
$direct->setInitiatorToken($initiatorToken);

0 commit comments

Comments
 (0)