From 6175a0b2661e64b65c66eccb461ddb4085f76d35 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 | 2 +- src/views/PDFView.vue | 6 +++--- src/workersrc.js | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) rename src/utils/{canDownload.js => hideDownload.js} (90%) diff --git a/src/public.js b/src/public.js index 39827f9a..45f6b993 100644 --- a/src/public.js +++ b/src/public.js @@ -22,7 +22,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' @@ -30,7 +30,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(), }) @@ -53,8 +53,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 90% rename from src/utils/canDownload.js rename to src/utils/hideDownload.js index 5b30f759..2e88f2ea 100644 --- a/src/utils/canDownload.js +++ b/src/utils/hideDownload.js @@ -21,4 +21,4 @@ */ 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 11bae7a9..9f214a27 100644 --- a/src/utils/isSecureViewerAvailable.js +++ b/src/utils/isSecureViewerAvailable.js @@ -22,4 +22,4 @@ import canDownload from './canDownload.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 ff013885..2d61857e 100644 --- a/src/views/PDFView.vue +++ b/src/views/PDFView.vue @@ -30,7 +30,7 @@ import { showError } from '@nextcloud/dialogs' 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' @@ -46,8 +46,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 d002c6b1..4613bfc3 100644 --- a/src/workersrc.js +++ b/src/workersrc.js @@ -27,11 +27,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] @@ -49,7 +49,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 7515b813bd04e7e1fddb7eb55176105c8eb481d3 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 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/views/PDFView.vue b/src/views/PDFView.vue index 2d61857e..34e8a3f4 100644 --- a/src/views/PDFView.vue +++ b/src/views/PDFView.vue @@ -57,12 +57,32 @@ export default { return this.fileList.find((file) => file.fileid === this.fileid) }, + isDownloadable() { + if (!this.file.shareAttributes) { + return true + } + + const shareAttributes = JSON.parse(this.file.shareAttributes) + const downloadPermissions = shareAttributes.find(({ scope, key }) => scope === 'permissions' && key === 'download') + if (downloadPermissions) { + return downloadPermissions.value + } + + return true + }, + isEditable() { return this.file?.permissions?.indexOf('W') >= 0 }, }, async mounted() { + if (!this.isDownloadable) { + this.doneLoading() + + return + } + document.addEventListener('webviewerloaded', this.handleWebviewerloaded) if (isPublicPage() && isPdf()) { @@ -184,6 +204,12 @@ export default {