Skip to content

Commit 65f579f

Browse files
authored
Merge pull request #460 from nextcloud/backport/459/stable22
2 parents d547df8 + 32aed4a commit 65f579f

10 files changed

+72
-116
lines changed

.github/workflows/phpunit.yml

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,7 @@ jobs:
3030
with:
3131
repository: nextcloud/server
3232
ref: ${{ matrix.server-versions }}
33-
34-
- name: Checkout submodules
35-
shell: bash
36-
run: |
37-
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
38-
git submodule sync --recursive
39-
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
33+
submodules: true
4034

4135
- name: Checkout app
4236
uses: actions/checkout@v2
@@ -86,7 +80,7 @@ jobs:
8680

8781
services:
8882
mysql:
89-
image: mariadb
83+
image: mariadb:10.5
9084
ports:
9185
- 4444:3306/tcp
9286
env:
@@ -171,13 +165,7 @@ jobs:
171165
with:
172166
repository: nextcloud/server
173167
ref: ${{ matrix.server-versions }}
174-
175-
- name: Checkout submodules
176-
shell: bash
177-
run: |
178-
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
179-
git submodule sync --recursive
180-
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
168+
submodules: true
181169

182170
- name: Checkout app
183171
uses: actions/checkout@v2
@@ -238,13 +226,7 @@ jobs:
238226
with:
239227
repository: nextcloud/server
240228
ref: ${{ matrix.server-versions }}
241-
242-
- name: Checkout submodules
243-
shell: bash
244-
run: |
245-
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
246-
git submodule sync --recursive
247-
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
229+
submodules: true
248230

249231
- name: Checkout app
250232
uses: actions/checkout@v2

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,25 @@
4949
* along with this program. If not, see <http://www.gnu.org/licenses/>.
5050
*
5151
*/
52+
53+
/**
54+
* @copyright Copyright (c) 2021 John Molakvoæ <skjnldsv@protonmail.com>
55+
*
56+
* @author John Molakvoæ <skjnldsv@protonmail.com>
57+
*
58+
* @license GNU AGPL version 3 or any later version
59+
*
60+
* This program is free software: you can redistribute it and/or modify
61+
* it under the terms of the GNU Affero General Public License as
62+
* published by the Free Software Foundation, either version 3 of the
63+
* License, or (at your option) any later version.
64+
*
65+
* This program is distributed in the hope that it will be useful,
66+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
67+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
68+
* GNU Affero General Public License for more details.
69+
*
70+
* You should have received a copy of the GNU Affero General Public License
71+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
72+
*
73+
*/

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)