Skip to content

Commit

Permalink
Register "share-attributes" DAV property in public share pages
Browse files Browse the repository at this point in the history
The viewer loads the default and registered DAV properties in the
FileInfo object provided to the PDF viewer. The "share-attributes"
property is needed to know if downloads are allowed, but it is not one
of the properties included by default. The Files app registers it during
its initialization, so it is automatically available when the PDF viewer
is opened in the Files app. However, when the PDF viewer is opened in
public share page nothing registers it, so it needs to be explicitly
registered by the PDF viewer itself before the viewer is loaded.

Signed-off-by: Daniel Calviño Sánchez <[email protected]>
  • Loading branch information
danxuliu committed Jan 20, 2025
1 parent 304019a commit e14375f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Listeners/LoadViewerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public function handle(Event $event): void {
if (!$event instanceof LoadViewer) {
return;
}

// The PDF viewer needs to register the "share-attributes" DAV property
// before the viewer is loaded in public share pages. Therefore an init
// script is needed rather than a regular script, and it needs to be
// done for "LoadViewer" rather than the "BeforeTemplateRenderedEvent",
// as when that event is handled the viewer is already initialized.
Util::addInitScript(Application::APP_ID, 'files_pdfviewer-init');

Util::addScript(Application::APP_ID, 'files_pdfviewer-main', 'viewer');
}
}
17 changes: 17 additions & 0 deletions src/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { registerDavProperty } from '@nextcloud/files'

import isPublicPage from './utils/isPublicPage.js'
import isPdf from './utils/isPdf.js'
import isSecureViewerAvailable from './utils/isSecureViewerAvailable.js'

// The "share-attributes" DAV property needs to be explicitly registered so the
// viewer returns it in the file info. This is implicitly done by the Files app,
// so it does not need to be explicitly done by the PDF viewer when it is opened
// in the Files app, only for public shares pages.
if (isPublicPage() && isPdf() && !isSecureViewerAvailable()) {
registerDavProperty('nc:share-attributes', { nc: 'http://nextcloud.org/ns' })
}
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const l10nContent = readdirSync(path.resolve(__dirname, 'js', 'pdfjs', 'web', 'l

webpackConfig.entry.workersrc = path.resolve(path.join('src', 'workersrc.js'))
webpackConfig.entry.public = path.resolve(path.join('src', 'public.js'))
webpackConfig.entry.init = path.resolve(path.join('src', 'init.js'))

// keep pdfjs vendor in the js folder
webpackConfig.output.clean = false
Expand Down

0 comments on commit e14375f

Please sign in to comment.