Skip to content

Commit

Permalink
Merge pull request #3846 from nextcloud/fix/public-share-fileid
Browse files Browse the repository at this point in the history
fix: Provide file id for single file share links
  • Loading branch information
max-nextcloud authored Feb 28, 2023
2 parents 123985e + c13824c commit 0db90d4
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
13 changes: 13 additions & 0 deletions cypress/e2e/share.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,17 @@ describe('Open test.md in viewer', function() {
})
})

it('makes use of the file id', function() {
cy.intercept({ method: 'PUT', url: '**/apps/text/public/session/create' })
.as('create')
cy.shareFile('/test2.md', { edit: true })
.then((token) => {
cy.logout()
cy.visit(`/s/${token}`)
})
cy.wait('@create', { timeout: 10000 })
.its('request.body.fileId')
.should('be.a', 'Number')
})

})
7 changes: 6 additions & 1 deletion lib/Listeners/FilesSharingLoadAdditionalScriptsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@

namespace OCA\Text\Listeners;

use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Text\Service\InitialStateProvider;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\Util;

/** @implements IEventListener<Event> */
/** @implements IEventListener<Event|BeforeTemplateRenderedEvent> */
class FilesSharingLoadAdditionalScriptsListener implements IEventListener {
protected InitialStateProvider $initialStateProvider;

Expand All @@ -43,5 +44,9 @@ public function handle(Event $event): void {
Util::addScript('text', 'text-public');

$this->initialStateProvider->provideState();
$node = $event->getShare()->getNode();
if ($node instanceof \OCP\Files\File) {
$this->initialStateProvider->provideFileId($node->getId());
}
}
}
4 changes: 4 additions & 0 deletions lib/Service/InitialStateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public function provideState(): void {
$this->configService->isRichEditingEnabled()
);
}

public function provideFileId(int $fileId): void {
$this->initialState->provideInitialState('file_id', $fileId);
}
}
7 changes: 5 additions & 2 deletions src/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import store from './store/index.js'
__webpack_nonce__ = btoa(OC.requestToken) // eslint-disable-line
__webpack_public_path__ = OC.linkTo('text', 'js/') // eslint-disable-line

const loadEditor = ({ sharingToken, mimetype, $el }) => {
const loadEditor = ({ sharingToken, mimetype, fileId, $el }) => {
const container = document.createElement('div')
container.id = 'texteditor'

Expand All @@ -37,6 +37,7 @@ const loadEditor = ({ sharingToken, mimetype, $el }) => {
active: true,
shareToken: sharingToken,
mime: mimetype,
fileId,
},
}),
store,
Expand Down Expand Up @@ -67,7 +68,9 @@ documentReady(() => {

// single file share
if (openMimetypes.indexOf(mimetype) !== -1) {
loadEditor({ mimetype, sharingToken, $el: document.getElementById('preview') })
const $el = document.getElementById('preview')
const fileId = loadState('text', 'file_id')
loadEditor({ mimetype, sharingToken, fileId, $el })
}
})

Expand Down
6 changes: 6 additions & 0 deletions tests/stub.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace OCA\Viewer\Event {
class LoadViewer extends \OCP\EventDispatcher\Event {}
}

namespace OCA\Files_Sharing\Event {
class BeforeTemplateRenderedEvent extends \OCP\EventDispatcher\Event {
abstract public function getShare(): \OCP\Share\IShare;
}
}

namespace OC\User {
class NoUserException extends \Exception {}
}
Expand Down

0 comments on commit 0db90d4

Please sign in to comment.