From ff2d9c69de1fdd2f0751c1ea32b327b13327c029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Tue, 29 Oct 2024 07:52:58 +0100 Subject: [PATCH 1/3] Rename "canDownload" to "hideDownload" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "canDownload" inverted the value of the "hideDownload" setting. However, that was not accurate, as if the download is hidden the file can still be downloaded. Moreover, it is possible to actually disallow downloads, which is a different setting (using share attributes) than hiding it. Therefore, to better differentiate between a hidden download and a disabled download the previous "canDownload" was renamed (and adjusted as needed) to "hideDownload". Signed-off-by: Daniel Calviño Sánchez --- src/public.js | 8 ++++---- src/utils/{canDownload.js => hideDownload.js} | 2 +- src/utils/isSecureViewerAvailable.js | 4 ++-- src/views/PDFView.vue | 6 +++--- src/workersrc.js | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) rename src/utils/{canDownload.js => hideDownload.js} (66%) diff --git a/src/public.js b/src/public.js index 5f1bb101..1da69f8d 100644 --- a/src/public.js +++ b/src/public.js @@ -5,7 +5,7 @@ import { generateUrl } from '@nextcloud/router' import logger from './services/logger.js' -import canDownload from './utils/canDownload.js' +import hideDownload from './utils/hideDownload.js' import isPublicPage from './utils/isPublicPage.js' import isPdf from './utils/isPdf.js' import isSecureViewerAvailable from './utils/isSecureViewerAvailable.js' @@ -13,7 +13,7 @@ import isSecureViewerAvailable from './utils/isSecureViewerAvailable.js' window.addEventListener('DOMContentLoaded', function() { logger.debug('Initializing for public page', { isPublicPage: isPublicPage(), - canDownload: canDownload(), + hideDownload: hideDownload(), isSecureViewerAvailable: isSecureViewerAvailable(), }) @@ -36,8 +36,8 @@ window.addEventListener('DOMContentLoaded', function() { const sharingToken = sharingTokenElmt.value const downloadUrl = generateUrl('/s/{token}/download', { token: sharingToken }) - const viewerUrl = generateUrl('/apps/files_pdfviewer/?file={downloadUrl}&canDownload={canDownload}#page={page}', { - canDownload: canDownload() ? 1 : 0, + const viewerUrl = generateUrl('/apps/files_pdfviewer/?file={downloadUrl}&hideDownload={hideDownload}#page={page}', { + hideDownload: hideDownload() ? 1 : 0, downloadUrl, page, }) diff --git a/src/utils/canDownload.js b/src/utils/hideDownload.js similarity index 66% rename from src/utils/canDownload.js rename to src/utils/hideDownload.js index 361753b0..77539f4d 100644 --- a/src/utils/canDownload.js +++ b/src/utils/hideDownload.js @@ -3,4 +3,4 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ const hideDownloadElmt = document.getElementById('hideDownload') -export default () => !hideDownloadElmt || (hideDownloadElmt && hideDownloadElmt.value !== 'true') +export default () => hideDownloadElmt && hideDownloadElmt.value === 'true' diff --git a/src/utils/isSecureViewerAvailable.js b/src/utils/isSecureViewerAvailable.js index 8954c3d7..9294faad 100644 --- a/src/utils/isSecureViewerAvailable.js +++ b/src/utils/isSecureViewerAvailable.js @@ -2,6 +2,6 @@ * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -import canDownload from './canDownload.js' +import hideDownload from './hideDownload.js' -export default () => !canDownload() && typeof OCA.RichDocuments !== 'undefined' +export default () => hideDownload() && typeof OCA.RichDocuments !== 'undefined' diff --git a/src/views/PDFView.vue b/src/views/PDFView.vue index 474e6175..9b3477c8 100644 --- a/src/views/PDFView.vue +++ b/src/views/PDFView.vue @@ -14,7 +14,7 @@ import { getLanguage } from '@nextcloud/l10n' import { generateUrl } from '@nextcloud/router' import logger from '../services/logger.js' import uploadPdfFile from '../services/uploadPdfFile.js' -import canDownload from '../utils/canDownload.js' +import hideDownload from '../utils/hideDownload.js' import isPdf from '../utils/isPdf.js' import isPublicPage from '../utils/isPublicPage.js' @@ -30,8 +30,8 @@ export default { computed: { iframeSrc() { - return generateUrl('/apps/files_pdfviewer/?file={file}&canDownload={canDownload}', { - canDownload: canDownload() ? 1 : 0, + return generateUrl('/apps/files_pdfviewer/?file={file}&hideDownload={hideDownload}', { + hideDownload: hideDownload() ? 1 : 0, file: this.source ?? this.davPath, }) }, diff --git a/src/workersrc.js b/src/workersrc.js index 8930a698..b53535b5 100644 --- a/src/workersrc.js +++ b/src/workersrc.js @@ -10,11 +10,11 @@ import redirectIfNotIframe from './utils/redirectIfNotIframe.js' // Checks if the page is displayed in an iframe. If not redirect to /. redirectIfNotIframe() -// Retrieve the canDownload from the url, this is +// Retrieve the hideDownload from the url, this is // the most easy way to pass the prop to this iframe const queryString = window.location.search const urlParams = new URLSearchParams(queryString) -const canDownload = urlParams.get('canDownload') +const hideDownload = urlParams.get('hideDownload') function initializeCustomPDFViewerApplication() { const head = document.getElementsByTagName('head')[0] @@ -32,7 +32,7 @@ function initializeCustomPDFViewerApplication() { PDFViewerApplicationOptions.set('imageResourcesPath', './js/pdfjs/web/images/') PDFViewerApplicationOptions.set('enableScripting', head.getAttribute('data-enableScripting') === true) - if (canDownload === '0') { + if (hideDownload === '1') { const pdfViewer = window.document.querySelector('.pdfViewer') if (pdfViewer) { From 44b906784633902f60682fce3f3db78b2ee71c1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Tue, 29 Oct 2024 08:00:07 +0100 Subject: [PATCH 2/3] Show error when trying to open a shared PDF without download permissions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In order to show a PDF file it needs to be downloaded. Therefore, if a shared PDF file does not have download permissions it is not possible to show it, so now an error is shown instead. The error is a custom one rather than a standard error from the viewer (although with the same appearance) to better explain the reason. Note that the error is shown only when the PDF file is loaded through the viewer, which should be always the case. There is a fallback to inject the UI in public shares in case the viewer is not available, but handling the error also in that case was not trivial and that fallback should never be used anyway, so it was not taken into account. Signed-off-by: Daniel Calviño Sánchez --- src/views/PDFView.vue | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/views/PDFView.vue b/src/views/PDFView.vue index 9b3477c8..72f39e4a 100644 --- a/src/views/PDFView.vue +++ b/src/views/PDFView.vue @@ -3,9 +3,14 @@ - SPDX-License-Identifier: AGPL-3.0-or-later -->