Skip to content

Commit 2c8d727

Browse files
authored
Merge pull request #459 from nextcloud/fix/hide-download-print
2 parents 0448619 + 015b467 commit 2c8d727

9 files changed

+46
-139
lines changed

js/files_pdfviewer-public.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/files_pdfviewer-public.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/files_pdfviewer-workersrc.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/files_pdfviewer-workersrc.js.LICENSE.txt

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,3 @@
44
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
55
* @license MIT
66
*/
7-
8-
/**
9-
* @copyright Copyright (c) 2020 Daniel Calviño Sánchez <danxuliu@gmail.com>
10-
*
11-
* @author Daniel Calviño Sánchez <danxuliu@gmail.com>
12-
* @author John Molakvoæ <skjnldsv@protonmail.com>
13-
*
14-
* @license GNU AGPL version 3 or any later version
15-
*
16-
* This program is free software: you can redistribute it and/or modify
17-
* it under the terms of the GNU Affero General Public License as
18-
* published by the Free Software Foundation, either version 3 of the
19-
* License, or (at your option) any later version.
20-
*
21-
* This program is distributed in the hope that it will be useful,
22-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24-
* GNU Affero General Public License for more details.
25-
*
26-
* You should have received a copy of the GNU Affero General Public License
27-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
28-
*
29-
*/
30-
31-
/**
32-
* @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
33-
*
34-
* @author John Molakvoæ <skjnldsv@protonmail.com>
35-
*
36-
* @license GNU AGPL version 3 or any later version
37-
*
38-
* This program is free software: you can redistribute it and/or modify
39-
* it under the terms of the GNU Affero General Public License as
40-
* published by the Free Software Foundation, either version 3 of the
41-
* License, or (at your option) any later version.
42-
*
43-
* This program is distributed in the hope that it will be useful,
44-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
45-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46-
* GNU Affero General Public License for more details.
47-
*
48-
* You should have received a copy of the GNU Affero General Public License
49-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
50-
*
51-
*/

js/files_pdfviewer-workersrc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/public.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@ window.addEventListener('DOMContentLoaded', function() {
3838
const page = location.hash.split('page=')[1] || 0
3939
const contentElmt = document.getElementById('files-public-content')
4040
const sharingTokenElmt = document.getElementById('sharingToken')
41-
const footerElmt = document.querySelector('#app-content > footer')
41+
const footerElmt = document.querySelector('body > footer')
4242

4343
const sharingToken = sharingTokenElmt.value
4444
const downloadUrl = generateUrl('/s/{token}/download', { token: sharingToken })
4545
const viewerUrl = generateUrl('/apps/files_pdfviewer/?file={downloadUrl}#page={page}', { downloadUrl, page })
4646

4747
// Create viewer frame
4848
const viewerNode = document.createElement('iframe')
49-
viewerNode.src = viewerUrl
5049
viewerNode.style.height = '100%'
5150
viewerNode.style.width = '100%'
5251
viewerNode.style.position = 'absolute'
@@ -55,10 +54,45 @@ window.addEventListener('DOMContentLoaded', function() {
5554
if (contentElmt) {
5655
contentElmt.innerHTML = ''
5756
contentElmt.appendChild(viewerNode)
57+
viewerNode.src = viewerUrl
5858
footerElmt.style.display = 'none'
5959
} else {
6060
logger.error('Unable to inject the PDF Viewer')
6161
}
62+
63+
// When pdf viewer is loaded
64+
addEventListener('load', function() {
65+
// If we forbid download, prevent interaction
66+
if (!canDownload()) {
67+
const pdfViewer = viewerNode.contentDocument.querySelector('.pdfViewer')
68+
const PDFViewerApplication = viewerNode.contentWindow.PDFViewerApplication
69+
70+
if (pdfViewer) {
71+
pdfViewer.classList.add('disabledTextSelection')
72+
}
73+
74+
if (PDFViewerApplication) {
75+
// Disable printing service when downloads are hidden, as even if the
76+
// buttons in the UI are hidden the printing could still be triggered
77+
// with Ctrl|Meta+P.
78+
// Abuse the "supportsPrinting" parameter, which signals that the
79+
// browser does not fully support printing, to make PDFViewer disable
80+
// the printing service.
81+
// "supportsPrinting" is a getter function, so it needs to be deleted
82+
// before replacing it with a simple value.
83+
delete PDFViewerApplication.supportsPrinting
84+
PDFViewerApplication.supportsPrinting = false
85+
86+
// When printing is not supported a warning is shown by the default
87+
// "beforePrint" function when trying to print. That function needs to
88+
// be replaced with an empty one to prevent that warning to be shown.
89+
PDFViewerApplication.beforePrint = function() {
90+
}
91+
}
92+
93+
logger.info('Download, printing and user interaction disabled')
94+
}
95+
})
6296
} else {
6397
logger.error('But this does not appear to be a public page')
6498
}

src/utils/canDownload.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,4 @@
2222
*/
2323

2424
const hideDownloadElmt = document.getElementById('hideDownload')
25-
// true = hidden download
2625
export default () => !hideDownloadElmt || (hideDownloadElmt && hideDownloadElmt.value !== 'true')

src/utils/isSecureViewerAvailable.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2121
*
2222
*/
23+
2324
import canDownload from './canDownload'
2425

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

src/workersrc.js

Lines changed: 3 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
*
2323
*/
2424

25-
import canDownload from './utils/canDownload'
25+
import logger from './services/logger'
2626
import redirectIfNotIframe from './utils/redirectIfNotIframe'
2727

28-
/**
29-
* Checks if the page is displayed in an iframe. If not redirect to /.
30-
**/
28+
// Checks if the page is displayed in an iframe. If not redirect to /.
3129
redirectIfNotIframe()
3230

3331
// When "PDFViewerApplication.webViewerInitialized" is executed (once
@@ -38,7 +36,6 @@ redirectIfNotIframe()
3836
// "PDFViewerApplication" and "PDFViewerApplicationOptions" are globally set and
3937
// before "PDFViewerApplication.initialize" is executed.
4038
function initializeCustomPDFViewerApplication() {
41-
4239
// Preferences override options, so they must be disabled for
4340
// "externalLinkTarget" to take effect.
4441
PDFViewerApplicationOptions.set('disablePreferences', true)
@@ -48,86 +45,7 @@ function initializeCustomPDFViewerApplication() {
4845
PDFViewerApplicationOptions.set('cMapUrl', document.getElementsByTagName('head')[0].getAttribute('data-cmapurl'))
4946
PDFViewerApplicationOptions.set('enablePermissions', true)
5047

51-
console.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll())
52-
53-
// The download has to be forced to use the URL of the file; by default
54-
// "PDFViewerApplication.download" uses a blob, but this causes a CSP error
55-
// (at least, in Firefox) when trying to download it.
56-
PDFViewerApplication.download = function() {
57-
// "isDataSchema()" and "getPDFFileNameFromURL()" are copied from
58-
// "vendor/pdfjs/web/viewer.js", as the functions defined in that file
59-
// can not be accessed from the outside.
60-
function isDataSchema(url) {
61-
let i = 0
62-
const ii = url.length
63-
while (i < ii && url[i].trim() === '') {
64-
i++
65-
}
66-
return url.substr(i, 5).toLowerCase() === 'data:'
67-
}
68-
69-
function getPDFFileNameFromURL(url) {
70-
const defaultFilename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'document.pdf'
71-
72-
if (isDataSchema(url)) {
73-
console.warn('getPDFFileNameFromURL: ' + 'ignoring "data:" URL for performance reasons.')
74-
return defaultFilename
75-
}
76-
const reURI = /^(?:(?:[^:]+:)?\/\/[^/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/
77-
const reFilename = /[^/?#=]+\.pdf\b(?!.*\.pdf\b)/i
78-
const splitURI = reURI.exec(url)
79-
let suggestedFilename = reFilename.exec(splitURI[1]) || reFilename.exec(splitURI[2]) || reFilename.exec(splitURI[3])
80-
if (suggestedFilename) {
81-
suggestedFilename = suggestedFilename[0]
82-
if (suggestedFilename.indexOf('%') !== -1) {
83-
try {
84-
suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0]
85-
} catch (e) {
86-
console.debug(e)
87-
}
88-
}
89-
}
90-
return suggestedFilename || defaultFilename
91-
}
92-
93-
const url = decodeURIComponent(window.location.search.substr(6))
94-
95-
this.downloadManager.downloadUrl(url, getPDFFileNameFromURL(url))
96-
}
97-
98-
if (!canDownload()) {
99-
// Disable download function when downloads are hidden, as even if the
100-
// buttons in the UI are hidden the download could still be triggered
101-
// with Ctrl|Meta+S.
102-
PDFViewerApplication.download = function() {
103-
}
104-
const downloadButton = document.getElementById('toolbarViewerRight').querySelector('button.download')
105-
if (downloadButton) {
106-
downloadButton.style.display = 'none'
107-
}
108-
109-
// Disable printing service when downloads are hidden, as even if the
110-
// buttons in the UI are hidden the printing could still be triggered
111-
// with Ctrl|Meta+P.
112-
// Abuse the "supportsPrinting" parameter, which signals that the
113-
// browser does not fully support printing, to make PDFViewer disable
114-
// the printing service.
115-
// "supportsPrinting" is a getter function, so it needs to be deleted
116-
// before replacing it with a simple value.
117-
delete PDFViewerApplication.supportsPrinting
118-
PDFViewerApplication.supportsPrinting = false
119-
120-
// When printing is not supported a warning is shown by the default
121-
// "beforePrint" function when trying to print. That function needs to
122-
// be replaced with an empty one to prevent that warning to be shown.
123-
PDFViewerApplication.beforePrint = function() {
124-
}
125-
126-
// For css properties
127-
document.getElementById('viewer').classList.add('disabledTextSelection')
128-
129-
console.debug('Files_PDFViewer, download and print disabled')
130-
}
48+
logger.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll())
13149
}
13250

13351
document.addEventListener('DOMContentLoaded', initializeCustomPDFViewerApplication, true)

0 commit comments

Comments
 (0)