From e14375f9397da358aad02c1a7c96012145285903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Tue, 31 Dec 2024 06:41:13 +0100 Subject: [PATCH] Register "share-attributes" DAV property in public share pages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- lib/Listeners/LoadViewerListener.php | 8 ++++++++ src/init.js | 17 +++++++++++++++++ webpack.config.js | 1 + 3 files changed, 26 insertions(+) create mode 100644 src/init.js diff --git a/lib/Listeners/LoadViewerListener.php b/lib/Listeners/LoadViewerListener.php index 54181ff4..18ea04e8 100644 --- a/lib/Listeners/LoadViewerListener.php +++ b/lib/Listeners/LoadViewerListener.php @@ -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'); } } diff --git a/src/init.js b/src/init.js new file mode 100644 index 00000000..5661df03 --- /dev/null +++ b/src/init.js @@ -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' }) +} diff --git a/webpack.config.js b/webpack.config.js index 4922d110..523c4caa 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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