Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable29] Show error when trying to open a shared PDF without download permissions #1097

Draft
wants to merge 3 commits into
base: stable29
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,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 @@ -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,
})
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 @@ -21,4 +21,4 @@
*/

const hideDownloadElmt = document.getElementById('hideDownload')
export default () => !hideDownloadElmt || (hideDownloadElmt && hideDownloadElmt.value !== 'true')
export default () => hideDownloadElmt && hideDownloadElmt.value === 'true'
2 changes: 1 addition & 1 deletion src/utils/isSecureViewerAvailable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
*
*/

import canDownload from './canDownload.js'

Check failure on line 23 in src/utils/isSecureViewerAvailable.js

View workflow job for this annotation

GitHub Actions / NPM lint

'canDownload' is defined but never used

Check failure on line 23 in src/utils/isSecureViewerAvailable.js

View workflow job for this annotation

GitHub Actions / NPM lint

Unable to resolve path to module './canDownload.js'

Check failure on line 23 in src/utils/isSecureViewerAvailable.js

View workflow job for this annotation

GitHub Actions / NPM lint

"./canDownload.js" is not found

export default () => !canDownload() && typeof OCA.RichDocuments !== 'undefined'
export default () => hideDownload() && typeof OCA.RichDocuments !== 'undefined'

Check failure on line 25 in src/utils/isSecureViewerAvailable.js

View workflow job for this annotation

GitHub Actions / NPM lint

'hideDownload' is not defined
39 changes: 35 additions & 4 deletions src/views/PDFView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@
-
-->
<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 @@ -32,7 +37,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 @@ -48,8 +53,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 @@ -59,12 +64,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 @@ -204,6 +229,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 @@ -27,13 +27,13 @@
// 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() {

Check warning on line 36 in src/workersrc.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc comment
const head = document.getElementsByTagName('head')[0]

// Preferences override options, so they must be disabled for
Expand All @@ -49,7 +49,7 @@
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
Loading