diff --git a/appinfo/app.php b/appinfo/app.php index 2115b2ffc..90b40faf4 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -37,27 +37,16 @@ $eventDispatcher->addListener( 'OCA\Files::loadAdditionalScripts', function () { - \OCP\Util::addScript('officeonline', 'files'); + \OCP\Util::addScript('officeonline', 'files', 'viewer'); } ); $eventDispatcher->addListener( 'OCA\Files_Sharing::loadAdditionalScripts', function () { - \OCP\Util::addScript('officeonline', 'files'); + \OCP\Util::addScript('officeonline', 'files', 'viewer'); } ); -if (class_exists('\OC\Files\Type\TemplateManager')) { - $manager = \OC_Helper::getFileTemplateManager(); - - $manager->registerTemplate('application/vnd.openxmlformats-officedocument.wordprocessingml.document', dirname(__DIR__) . '/assets/docxtemplate.docx'); - $manager->registerTemplate('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', dirname(__DIR__) . '/assets/xlsxtemplate.xlsx'); - $manager->registerTemplate('application/vnd.openxmlformats-officedocument.presentationml.presentation', dirname(__DIR__) . '/assets/pptxtemplate.pptx'); - $manager->registerTemplate('application/vnd.oasis.opendocument.presentation', dirname(__DIR__) . '/assets/template.odp'); - $manager->registerTemplate('application/vnd.oasis.opendocument.text', dirname(__DIR__) . '/assets/template.odt'); - $manager->registerTemplate('application/vnd.oasis.opendocument.spreadsheet', dirname(__DIR__) . '/assets/template.ods'); -} - $app = \OC::$server->query(Application::class); $app->registerProvider(); $app->updateCSP(); diff --git a/assets/docxtemplate.docx b/assets/docxtemplate.docx deleted file mode 100644 index 05411cbf5..000000000 Binary files a/assets/docxtemplate.docx and /dev/null differ diff --git a/assets/pptxtemplate.pptx b/assets/pptxtemplate.pptx deleted file mode 100644 index 066d34dd6..000000000 Binary files a/assets/pptxtemplate.pptx and /dev/null differ diff --git a/assets/template.docx b/assets/template.docx new file mode 100644 index 000000000..1b8fdf5a4 Binary files /dev/null and b/assets/template.docx differ diff --git a/assets/template.pptx b/assets/template.pptx new file mode 100644 index 000000000..e93a532d5 Binary files /dev/null and b/assets/template.pptx differ diff --git a/assets/template.xlsx b/assets/template.xlsx new file mode 100644 index 000000000..42e73eca0 Binary files /dev/null and b/assets/template.xlsx differ diff --git a/assets/xlsxtemplate.xlsx b/assets/xlsxtemplate.xlsx deleted file mode 100644 index b6040379c..000000000 Binary files a/assets/xlsxtemplate.xlsx and /dev/null differ diff --git a/lib/Controller/DocumentController.php b/lib/Controller/DocumentController.php index 67fcea3d8..d8b47ecd4 100644 --- a/lib/Controller/DocumentController.php +++ b/lib/Controller/DocumentController.php @@ -542,23 +542,11 @@ public function create($mimetype, $content = $manager->getTemplate($mimetype); } - if (!$content) { - $content = file_get_contents(dirname(dirname(__DIR__)) . self::ODT_TEMPLATE_PATH); - } - - if ($content) { - $file->putContent($content); - - return new JSONResponse([ - 'status' => 'success', - 'data' => \OCA\Files\Helper::formatFileInfo($file->getFileInfo()) - ]); - } - + $file->putContent($content); return new JSONResponse([ - 'status' => 'error', - 'message' => $this->l10n->t('Can\'t create document') + 'status' => 'success', + 'data' => \OCA\Files\Helper::formatFileInfo($file->getFileInfo()) ]); } diff --git a/lib/TokenManager.php b/lib/TokenManager.php index 8d2dfef19..3926031a8 100755 --- a/lib/TokenManager.php +++ b/lib/TokenManager.php @@ -205,7 +205,7 @@ public function getToken($fileId, $shareToken = null, $editoruid = null, $direct try { return [ - $this->wopiParser->getUrlSrcForFile($file, $updatable)['urlsrc'], // url src might not be found ehre + $this->wopiParser->getUrlSrcForFile($file, $updatable), $wopi->getToken(), $wopi ]; @@ -264,7 +264,7 @@ public function getTokenForTemplate(File $templateFile, $userId, $targetFileId, } return [ - $this->wopiParser->getUrlSrcForFile($targetFile, $updatable)['urlsrc'], + $this->wopiParser->getUrlSrcForFile($targetFile, $updatable), $wopi ]; } diff --git a/lib/WOPI/Parser.php b/lib/WOPI/Parser.php index 15fea7e12..c15548960 100755 --- a/lib/WOPI/Parser.php +++ b/lib/WOPI/Parser.php @@ -28,6 +28,9 @@ use SimpleXMLElement; class Parser { + public const ACTION_EDIT = 'edit'; + public const ACTION_VIEW = 'view'; + public const ACTION_EDITNEW = 'editnew'; // https://wopi.readthedocs.io/en/latest/faq/languages.html public const SUPPORTED_LANGUAGES = [ @@ -146,27 +149,6 @@ public function __construct(DiscoveryManager $discoveryManager, IRequest $reques $this->l10n = $l10n; } - /** - * @param $mimetype - * @return array - * @throws Exception - */ - public function getUrlSrc($mimetype) { - $discoveryParsed = $this->getParsed(); - - $result = $discoveryParsed->xpath(sprintf('/wopi-discovery/net-zone/app[@name=\'%s\']/action', $mimetype)); - if ($result && count($result) > 0) { - $urlSrc = $result[0]['urlsrc']; - $urlSrc = preg_replace('//', 'ui=' . $this->getLanguageCode() . '&', $urlSrc); - return [ - 'urlsrc' => preg_replace('/<.+>/', '', $urlSrc), - 'action' => (string)$result[0]['name'], - ]; - } - - throw new Exception('Could not find urlsrc in WOPI'); - } - /** * @return SimpleXMLElement|bool * @throws Exception @@ -189,45 +171,74 @@ public function getParsed() { return $discoveryParsed; } - /** - * @param File $file - * @param bool $edit - * @return array - * @throws Exception - */ - public function getUrlSrcForFile(File $file, $edit) { - try { - $result = $this->getUrlSrc($file->getMimeType()); - return $result; - } catch (Exception $e) { - } - // FIXME: we might want to support different action types here as well like imagepreview - $actionName = $edit ? 'edit' : 'view'; - $discoveryParsed = $this->getParsed(); - $result = $discoveryParsed->xpath(sprintf('/wopi-discovery/net-zone[@name=\'external-https\']/app/action[@ext=\'%s\' and @name=\'%s\']', $file->getExtension(), $actionName)); - if (!$result || count($result) === 0) { - $result = $discoveryParsed->xpath(sprintf('/wopi-discovery/net-zone[@name=\'external-https\']/app/action[@ext=\'%s\' and @name=\'%s\']', $file->getExtension(), 'view')); - } + public function getUrlSrcForFile(File $file, bool $edit): string { + $protocol = $this->request->getServerProtocol(); + $fallbackProtocol = $protocol === 'https' ? 'http' : 'https'; - if ($this->request->getServerProtocol() === 'http') { - if (!$result || count($result) === 0) { - $result = $discoveryParsed->xpath(sprintf('/wopi-discovery/net-zone[@name=\'external-http\']/app/action[@ext=\'%s\' and @name=\'%s\']', $file->getExtension(), $actionName)); - } - if (!$result || count($result) === 0) { - $result = $discoveryParsed->xpath(sprintf('/wopi-discovery/net-zone[@name=\'external-http\']/app/action[@ext=\'%s\' and @name=\'%s\']', $file->getExtension(), 'view')); + $netZones = [ + 'external-' . $protocol, + 'internal-' . $protocol, + 'external-' . $fallbackProtocol, + 'internal-' . $fallbackProtocol, + ]; + + $actions = [ + $edit && $file->getSize() === 0 ? self::ACTION_EDITNEW : null, + $edit ? self::ACTION_EDIT : null, + self::ACTION_VIEW, + ]; + $actions = array_filter($actions); + + foreach ($netZones as $netZone) { + foreach ($actions as $action) { + $result = $this->getUrlSrcByExtension($netZone, $file->getExtension(), $action); + if ($result) { + return $this->replaceUrlSrcParams($result); + } } } - if ($result && count($result) > 0) { - $urlSrc = $result[0]['urlsrc']; - $urlSrc = preg_replace('//', 'ui=' . $this->getLanguageCode() . '&', $urlSrc); - return [ - 'urlsrc' => preg_replace('/<.+>/', '', $urlSrc), - 'action' => (string)$result[0]['name'], - ]; + + foreach ($netZones as $netZone) { + $result = $this->getUrlSrcByMimetype($netZone, $file->getMimeType()); + if ($result) { + return $this->replaceUrlSrcParams($result); + } } + throw new Exception('Could not find urlsrc in WOPI'); } + private function getUrlSrcByExtension(string $netZoneName, string $actionExt, $actionName): ?string { + $result = $this->getParsed()->xpath(sprintf( + '/wopi-discovery/net-zone[@name=\'%s\']/app/action[@ext=\'%s\' and @name=\'%s\']', + $netZoneName, $actionExt, $actionName + )); + + if (!$result || count($result) === 0) { + return null; + } + + return (string)current($result)->attributes()['urlsrc']; + } + + private function getUrlSrcByMimetype(string $netZoneName, string $mimetype): ?string { + $result = $this->getParsed()->xpath(sprintf( + '/wopi-discovery/net-zone[@name=\'%s\']/app[@name=\'%s\']/action', + $netZoneName, $mimetype + )); + + if (!$result || count($result) === 0) { + return null; + } + + return (string)current($result)->attributes()['urlsrc']; + } + + private function replaceUrlSrcParams(string $urlSrc): string { + $urlSrc = preg_replace('//', 'ui=' . $this->getLanguageCode() . '&', $urlSrc); + return preg_replace('/<.+>/', '', $urlSrc); + } + private function getLanguageCode(): string { $languageCode = $this->l10n->getLanguageCode(); $localeCode = $this->l10n->getLocaleCode(); diff --git a/src/files.js b/src/files.js index 2960e2263..18a0c8426 100644 --- a/src/files.js +++ b/src/files.js @@ -1,11 +1,12 @@ import Types from './helpers/types' import axios from '@nextcloud/axios' import { getCapabilities } from '@nextcloud/capabilities' -import './viewer' +import './viewer.js' import Vue from 'vue' import Office from './view/Office' import './css/icons.css' +import { getCurrentDirectory } from './helpers/index.js' // eslint-disable-next-line __webpack_nonce__ = btoa(window.OC.requestToken) @@ -60,7 +61,7 @@ const NewFilePlugin = { }, _createDocument: function(mimetype, filename) { - const dir = document.getElementById('dir').value + const dir = getCurrentDirectory() try { OCA.Files.Files.isFileNameValid(filename) } catch (e) { diff --git a/src/helpers/index.js b/src/helpers/index.js index 57af45d15..4ba5335cc 100644 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -53,7 +53,20 @@ const getNextcloudVersion = () => { return parseInt(OC.config.version.split('.')[0]) } +const getCurrentDirectory = () => { + if (OCA.Sharing?.PublicApp?.fileList) { + return OCA.Sharing.PublicApp.fileList.getCurrentDirectory() + } + + if (OCA?.Files?.App?.currentFileList) { + return OCA?.Files?.App?.currentFileList.getCurrentDirectory() + } + + return '' +} + export { languageToBCP47, getNextcloudVersion, + getCurrentDirectory, } diff --git a/src/view/Office.vue b/src/view/Office.vue index e13cea599..619f6885b 100644 --- a/src/view/Office.vue +++ b/src/view/Office.vue @@ -23,23 +23,13 @@