Skip to content
Merged
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
26 changes: 4 additions & 22 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ jobs:
with:
repository: nextcloud/server
ref: ${{ github.base_ref }}

- name: Checkout submodules
shell: bash
run: |
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
git submodule sync --recursive
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
submodules: true

- name: Checkout app
uses: actions/checkout@v2
Expand Down Expand Up @@ -84,7 +78,7 @@ jobs:

services:
mysql:
image: mariadb
image: mariadb:10.5
ports:
- 4444:3306/tcp
env:
Expand All @@ -97,13 +91,7 @@ jobs:
with:
repository: nextcloud/server
ref: ${{ github.base_ref }}

- name: Checkout submodules
shell: bash
run: |
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
git submodule sync --recursive
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
submodules: true

- name: Checkout app
uses: actions/checkout@v2
Expand Down Expand Up @@ -168,13 +156,7 @@ jobs:
with:
repository: nextcloud/server
ref: ${{ github.base_ref }}

- name: Checkout submodules
shell: bash
run: |
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
git submodule sync --recursive
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
submodules: true

- name: Checkout app
uses: actions/checkout@v2
Expand Down
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.

27 changes: 3 additions & 24 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.

27 changes: 3 additions & 24 deletions js/files_pdfviewer-workersrc.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions js/files_pdfviewer-workersrc.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
* @license MIT
*/
2 changes: 1 addition & 1 deletion js/files_pdfviewer-workersrc.js.map

Large diffs are not rendered by default.

38 changes: 36 additions & 2 deletions src/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ window.addEventListener('DOMContentLoaded', function() {
const page = location.hash.split('page=')[1] || 0
const contentElmt = document.getElementById('files-public-content')
const sharingTokenElmt = document.getElementById('sharingToken')
const footerElmt = document.querySelector('#app-content > footer')
const footerElmt = document.querySelector('body > footer')

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

// Create viewer frame
const viewerNode = document.createElement('iframe')
viewerNode.src = viewerUrl
viewerNode.style.height = '100%'
viewerNode.style.width = '100%'
viewerNode.style.position = 'absolute'
Expand All @@ -55,10 +54,45 @@ window.addEventListener('DOMContentLoaded', function() {
if (contentElmt) {
contentElmt.innerHTML = ''
contentElmt.appendChild(viewerNode)
viewerNode.src = viewerUrl
footerElmt.style.display = 'none'
} else {
logger.error('Unable to inject the PDF Viewer')
}

// When pdf viewer is loaded
addEventListener('load', function() {
// If we forbid download, prevent interaction
if (!canDownload()) {
const pdfViewer = viewerNode.contentDocument.querySelector('.pdfViewer')
const PDFViewerApplication = viewerNode.contentWindow.PDFViewerApplication

if (pdfViewer) {
pdfViewer.classList.add('disabledTextSelection')
}

if (PDFViewerApplication) {
// Disable printing service when downloads are hidden, as even if the
// buttons in the UI are hidden the printing could still be triggered
// with Ctrl|Meta+P.
// Abuse the "supportsPrinting" parameter, which signals that the
// browser does not fully support printing, to make PDFViewer disable
// the printing service.
// "supportsPrinting" is a getter function, so it needs to be deleted
// before replacing it with a simple value.
delete PDFViewerApplication.supportsPrinting
PDFViewerApplication.supportsPrinting = false

// When printing is not supported a warning is shown by the default
// "beforePrint" function when trying to print. That function needs to
// be replaced with an empty one to prevent that warning to be shown.
PDFViewerApplication.beforePrint = function() {
}
}

logger.info('Download, printing and user interaction disabled')
}
})
} else {
logger.error('But this does not appear to be a public page')
}
Expand Down
1 change: 0 additions & 1 deletion src/utils/canDownload.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@
*/

const hideDownloadElmt = document.getElementById('hideDownload')
// true = hidden download
export default () => !hideDownloadElmt || (hideDownloadElmt && hideDownloadElmt.value !== 'true')
1 change: 1 addition & 0 deletions src/utils/isSecureViewerAvailable.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

import canDownload from './canDownload'

export default () => !canDownload() && typeof OCA.RichDocuments !== 'undefined'
89 changes: 6 additions & 83 deletions src/workersrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
*
*/

import canDownload from './utils/canDownload'
import logger from './services/logger'
import redirectIfNotIframe from './utils/redirectIfNotIframe'

/**
* Checks if the page is displayed in an iframe. If not redirect to /.
**/
// Checks if the page is displayed in an iframe. If not redirect to /.
redirectIfNotIframe()

// When "PDFViewerApplication.webViewerInitialized" is executed (once
Expand All @@ -38,92 +36,17 @@ redirectIfNotIframe()
// "PDFViewerApplication" and "PDFViewerApplicationOptions" are globally set and
// before "PDFViewerApplication.initialize" is executed.
function initializeCustomPDFViewerApplication() {

// Preferences override options, so they must be disabled for
// "externalLinkTarget" to take effect.
PDFViewerApplicationOptions.set('disablePreferences', true)
PDFViewerApplicationOptions.set('externalLinkTarget', pdfjsLib.LinkTarget.BLANK)
PDFViewerApplicationOptions.set('isEvalSupported', false)
PDFViewerApplicationOptions.set('workerSrc', document.getElementsByTagName('head')[0].getAttribute('data-workersrc'))
PDFViewerApplicationOptions.set('cMapUrl', document.getElementsByTagName('head')[0].getAttribute('data-cmapurl'))
PDFViewerApplicationOptions.set('enablePermissions', true)

// The download has to be forced to use the URL of the file; by default
// "PDFViewerApplication.download" uses a blob, but this causes a CSP error
// (at least, in Firefox) when trying to download it.
PDFViewerApplication.download = function() {
// "isDataSchema()" and "getPDFFileNameFromURL()" are copied from
// "vendor/pdfjs/web/viewer.js", as the functions defined in that file
// can not be accessed from the outside.
function isDataSchema(url) {
let i = 0
const ii = url.length
while (i < ii && url[i].trim() === '') {
i++
}
return url.substr(i, 5).toLowerCase() === 'data:'
}

function getPDFFileNameFromURL(url) {
const defaultFilename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'document.pdf'

if (isDataSchema(url)) {
console.warn('getPDFFileNameFromURL: ' + 'ignoring "data:" URL for performance reasons.')
return defaultFilename
}
const reURI = /^(?:(?:[^:]+:)?\/\/[^/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/
const reFilename = /[^/?#=]+\.pdf\b(?!.*\.pdf\b)/i
const splitURI = reURI.exec(url)
let suggestedFilename = reFilename.exec(splitURI[1]) || reFilename.exec(splitURI[2]) || reFilename.exec(splitURI[3])
if (suggestedFilename) {
suggestedFilename = suggestedFilename[0]
if (suggestedFilename.indexOf('%') !== -1) {
try {
suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0]
} catch (e) {
console.debug(e)
}
}
}
return suggestedFilename || defaultFilename
}

const url = decodeURIComponent(window.location.search.substr(6))

this.downloadManager.downloadUrl(url, getPDFFileNameFromURL(url))
}

if (!canDownload()) {
// Disable download function when downloads are hidden, as even if the
// buttons in the UI are hidden the download could still be triggered
// with Ctrl|Meta+S.
PDFViewerApplication.download = function() {
}
const downloadButton = document.getElementById('toolbarViewerRight').querySelector('button.download')
if (downloadButton) {
downloadButton.style.display = 'none'
}

// Disable printing service when downloads are hidden, as even if the
// buttons in the UI are hidden the printing could still be triggered
// with Ctrl|Meta+P.
// Abuse the "supportsPrinting" parameter, which signals that the
// browser does not fully support printing, to make PDFViewer disable
// the printing service.
// "supportsPrinting" is a getter function, so it needs to be deleted
// before replacing it with a simple value.
delete PDFViewerApplication.supportsPrinting
PDFViewerApplication.supportsPrinting = false

// When printing is not supported a warning is shown by the default
// "beforePrint" function when trying to print. That function needs to
// be replaced with an empty one to prevent that warning to be shown.
PDFViewerApplication.beforePrint = function() {
}

// For css properties
document.getElementById('viewer').classList.add('disabledTextSelection')

console.debug('Files_PDFViewer, download and print disabled')
}
logger.debug('Initialized files_pdfviewer', PDFViewerApplicationOptions.getAll())
}

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