import { getDocumentUrlFromTemplate, getDocumentUrlForPublicFile, getDocumentUrlForFile, getSearchParam } from './helpers/url' import PostMessageService from './services/postMessage' import Config from './services/config' import Types from './helpers/types' import FilesAppIntegration from './view/FilesAppIntegration' import '../css/viewer.scss' const FRAME_DOCUMENT = 'FRAME_DOCUMENT' const PostMessages = new PostMessageService({ FRAME_DOCUMENT: () => document.getElementById('richdocumentsframe').contentWindow }) const preloadCreate = getSearchParam('richdocuments_create') const preloadOpen = getSearchParam('richdocuments_open') const Preload = {} if (preloadCreate) { Preload.create = { type: getSearchParam('richdocuments_create'), filename: getSearchParam('richdocuments_filename') } } if (preloadOpen) { Preload.open = { filename: preloadOpen, id: getSearchParam('richdocuments_fileId'), dir: getSearchParam('dir') } } const isDownloadHidden = document.getElementById('hideDownload') && document.getElementById('hideDownload').value === 'true' const isPublic = document.getElementById('isPublic') && document.getElementById('isPublic').value === '1' const odfViewer = { open: false, receivedLoading: false, isCollaboraConfigured: typeof OC.getCapabilities().richdocuments.collabora === 'object' && OC.getCapabilities().richdocuments.collabora.length !== 0, supportedMimes: OC.getCapabilities().richdocuments.mimetypes.concat(OC.getCapabilities().richdocuments.mimetypesNoDefaultOpen), excludeMimeFromDefaultOpen: OC.getCapabilities().richdocuments.mimetypesNoDefaultOpen, hideDownloadMimes: ['image/jpeg', 'image/svg+xml', 'image/cgm', 'image/vnd.dxf', 'image/x-emf', 'image/x-wmf', 'image/x-wpg', 'image/x-freehand', 'image/bmp', 'image/png', 'image/gif', 'image/tiff', 'image/jpg', 'image/jpeg', 'text/plain', 'application/pdf'], register() { const EDIT_ACTION_NAME = 'Edit with ' + OC.getCapabilities().richdocuments.productName for (let mime of odfViewer.supportedMimes) { OCA.Files.fileActions.register( mime, EDIT_ACTION_NAME, 0, OC.imagePath('core', 'actions/rename'), this.onEdit, t('richdocuments', 'Edit with {productName}', { productName: OC.getCapabilities().richdocuments.productName }) ) if (odfViewer.excludeMimeFromDefaultOpen.indexOf(mime) === -1 || isDownloadHidden) { OCA.Files.fileActions.setDefault(mime, EDIT_ACTION_NAME) } } }, onEdit: function(fileName, context) { if (!odfViewer.isCollaboraConfigured) { const setupUrl = OC.generateUrl('/settings/admin/richdocuments') const installHint = OC.isUserAdmin() ? `Collabora Online is not setup yet.
Click here to configure your own server or connect to a demo server.
` : t('richdocuments', 'Collabora Online is not setup yet. Please contact your administrator.') if (OCP.Toast) { OCP.Toast.error(installHint, { isHTML: true, timeout: 0 }) } else { OC.Notification.showHtml(installHint) } return } if (odfViewer.open === true) { return } odfViewer.open = true let fileList = null if (context) { fileList = context.fileList var fileDir = context.dir var fileId = context.fileId || context.$file.attr('data-id') var templateId = context.templateId if (context.fileList) { context.fileList.setViewerMode(true) context.fileList.setPageTitle(fileName) context.fileList.showMask() } } odfViewer.receivedLoading = false let documentUrl = getDocumentUrlForFile(fileDir, fileId) if (isPublic) { documentUrl = getDocumentUrlForPublicFile(fileName, fileId) } if (typeof (templateId) !== 'undefined') { documentUrl = getDocumentUrlFromTemplate(templateId, fileName, fileDir) } /** * We need to reload the page to set a proper CSP if the file is federated * and the reload didn't happen for the exact same file */ const canAccessCSP = (url, callback) => { let canEmbed = false let frame = document.createElement('iframe') frame.setAttribute('src', url) frame.setAttribute('onload', () => { canEmbed = true }) document.body.appendChild(frame) setTimeout(() => { if (!canEmbed) { callback() } document.body.removeChild(frame) }, 50) } const reloadForFederationCSP = (fileName) => { const preloadId = Preload.open ? parseInt(Preload.open.id) : -1 const fileModel = fileList.findFile(fileName) const shareOwnerId = fileModel.shareOwnerId if (typeof shareOwnerId !== 'undefined') { const lastIndex = shareOwnerId.lastIndexOf('@') // only redirect if remote file, not opened though reload and csp blocks the request if (shareOwnerId.substr(lastIndex).indexOf('/') !== -1 && fileModel.id !== preloadId) { canAccessCSP('https://' + shareOwnerId.substr(lastIndex) + '/status.php', () => { window.location = OC.generateUrl('/apps/richdocuments/open?fileId=' + fileId) }) } } return false } if (context) { reloadForFederationCSP(fileName) } OC.addStyle('richdocuments', 'mobile') var $iframe = $('