diff options
author | John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> | 2020-08-13 17:18:30 +0300 |
---|---|---|
committer | John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> | 2020-08-13 17:20:38 +0300 |
commit | c775e153b7fbe765a9d2b405d8716caffc059bc1 (patch) | |
tree | aadc3917e1ed8bb0506095323af024349c73a49a /src | |
parent | 6954a8d2498299a5ecd415653dfb42d8104a37f8 (diff) |
Public pages
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/main.js | 14 | ||||
-rw-r--r-- | src/public.js | 62 |
2 files changed, 62 insertions, 14 deletions
diff --git a/src/main.js b/src/main.js index d86bb38..ce26dc7 100644 --- a/src/main.js +++ b/src/main.js @@ -20,22 +20,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ -import { generateFilePath } from '@nextcloud/router' -import { getRequestToken } from '@nextcloud/auth' - import PDFView from './views/PDFView' -// CSP config for webpack dynamic chunk loading -// eslint-disable-next-line -__webpack_nonce__ = btoa(getRequestToken()) - -// Correct the root of the app for chunk loading -// OC.linkTo matches the apps folders -// OC.generateUrl ensure the index.php (or not) -// We do not want the index.php since we're loading files -// eslint-disable-next-line -__webpack_public_path__ = generateFilePath('files_pdfviewer', '', 'js/') - window.addEventListener('DOMContentLoaded', function() { OCA.Viewer.registerHandler({ id: 'pdf', diff --git a/src/public.js b/src/public.js new file mode 100644 index 0000000..a61de50 --- /dev/null +++ b/src/public.js @@ -0,0 +1,62 @@ + +/** + * @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com> + * + * @author John Molakvoæ <skjnldsv@protonmail.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +import { generateUrl } from '@nextcloud/router' + +const hideDownloadElmt = document.getElementById('hideDownload') +const isPublicElmt = document.getElementById('isPublic') +const mimetypeElmt = document.getElementById('mimetype') + +const isSecureViewerAvailable = function() { + return hideDownloadElmt && hideDownloadElmt.value === 'true' && typeof OCA.RichDocuments !== 'undefined' +} + +window.addEventListener('DOMContentLoaded', function() { + if (isPublicElmt && isPublicElmt.value && mimetypeElmt && mimetypeElmt.value === 'application/pdf' && !isSecureViewerAvailable()) { + console.debug('Files_PDFViewer initiamized for public page') + + 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 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' + + // Inject viewer + if (contentElmt) { + contentElmt.innerHTML = '' + contentElmt.appendChild(viewerNode) + footerElmt.style.display = 'none' + } else { + console.error('Unable to inject the PDF Viewer') + } + } +}) |