Skip to content

Commit 29cbb8b

Browse files
committed
Fix hide download and printing
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
1 parent c2f189d commit 29cbb8b

File tree

5 files changed

+49
-86
lines changed

5 files changed

+49
-86
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*!
2+
* The buffer module from node.js, for the browser.
3+
*
4+
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
5+
* @license MIT
6+
*/

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: 6 additions & 83 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,92 +36,17 @@ redirectIfNotIframe()
3836
// "PDFViewerApplication" and "PDFViewerApplicationOptions" are globally set and
3937
// before "PDFViewerApplication.initialize" is executed.
4038
function initializeCustomPDFViewerApplication() {
39+
4140
// Preferences override options, so they must be disabled for
4241
// "externalLinkTarget" to take effect.
4342
PDFViewerApplicationOptions.set('disablePreferences', true)
4443
PDFViewerApplicationOptions.set('externalLinkTarget', pdfjsLib.LinkTarget.BLANK)
4544
PDFViewerApplicationOptions.set('isEvalSupported', false)
4645
PDFViewerApplicationOptions.set('workerSrc', document.getElementsByTagName('head')[0].getAttribute('data-workersrc'))
4746
PDFViewerApplicationOptions.set('cMapUrl', document.getElementsByTagName('head')[0].getAttribute('data-cmapurl'))
47+
PDFViewerApplicationOptions.set('enablePermissions', true)
4848

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

129-
document.addEventListener('webviewerloaded', initializeCustomPDFViewerApplication, true)
52+
document.addEventListener('DOMContentLoaded', initializeCustomPDFViewerApplication, true)

0 commit comments

Comments
 (0)