Skip to content

Commit

Permalink
Merge pull request #1077 from nextcloud/show-error-when-trying-to-ope…
Browse files Browse the repository at this point in the history
…n-a-shared-pdf-without-download-permissions

Show error when trying to open a shared PDF without download permissions
  • Loading branch information
Antreesy authored Dec 7, 2024
2 parents 662fdd5 + 8a307b7 commit 32aad47
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 23 deletions.
4 changes: 2 additions & 2 deletions js/files_pdfviewer-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/files_pdfviewer-main.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/files_pdfviewer-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/files_pdfviewer-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/files_pdfviewer-workersrc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/files_pdfviewer-workersrc.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
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'

window.addEventListener('DOMContentLoaded', function() {
logger.debug('Initializing for public page', {
isPublicPage: isPublicPage(),
canDownload: canDownload(),
hideDownload: hideDownload(),
isSecureViewerAvailable: isSecureViewerAvailable(),
})

Expand All @@ -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,
})
Expand Down
2 changes: 1 addition & 1 deletion src/utils/canDownload.js → src/utils/hideDownload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 2 additions & 2 deletions src/utils/isSecureViewerAvailable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
39 changes: 35 additions & 4 deletions src/views/PDFView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<iframe ref="iframe"
<iframe v-if="isDownloadable"
ref="iframe"
:src="iframeSrc"
@load="onIFrameLoaded" />
<div v-else id="emptycontent">
<div class="icon-error" />
<h3>{{ t('files_pdfviewer', 'To view a shared PDF file, the download needs to be allowed for this file share') }}</h3>
</div>
</template>

<script>
Expand All @@ -14,7 +19,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'

Expand All @@ -30,8 +35,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,
})
},
Expand All @@ -41,12 +46,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()) {
Expand Down Expand Up @@ -186,6 +211,12 @@ export default {
</script>

<style lang="scss" scoped>
#emptycontent {
margin: 0;
padding: 10% 5%;
background-color: var(--color-main-background);
}

iframe {
width: 100%;
height: calc(100vh - var(--header-height));
Expand Down
6 changes: 3 additions & 3 deletions src/workersrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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) {
Expand Down

0 comments on commit 32aad47

Please sign in to comment.