Skip to content

Commit

Permalink
fix: Use proper file id for direct editing
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Knorr <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Nov 29, 2024
1 parent 4b1d0cd commit 249701a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
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 249701a

Please sign in to comment.