Skip to content

Commit

Permalink
fix: File creation on Nextcloud 28
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Feb 14, 2024
1 parent 7d595ac commit 288f09a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 78 deletions.
59 changes: 57 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@
use OCA\Officeonline\Preview\OpenDocument;
use OCA\Officeonline\Preview\Pdf;
use OCA\Officeonline\Service\FederationService;
use OCA\Officeonline\Service\CapabilitiesService;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Template\ITemplateManager;
use OCP\Files\Template\TemplateFileCreator;
use OCP\IPreview;
use Psr\Log\LoggerInterface;
use OCP\IL10N;
use OCP\IConfig;

class Application extends App implements IBootstrap {
public const APP_ID = 'officeonline';
Expand All @@ -68,15 +73,17 @@ public function boot(IBootContext $context): void {
if (!$this->isEnabled()) {
return;
}

$this->registerProvider();
$this->updateCSP();
$this->registerNewFileCreators($context);
}

public function isEnabled(): bool {
$currentUser = \OC::$server->getUserSession()->getUser();
if ($currentUser !== null) {
/** @var PermissionManager $permissionManager */
$permissionManager = \OCP\Server::get(PermissionManager::class);
$permissionManager = \OC::$server->query(PermissionManager::class);
if (!$permissionManager->isEnabledForUser($currentUser)) {
return false;
}
Expand Down Expand Up @@ -162,7 +169,7 @@ public function updateCSP() {
}
}
} catch (\Throwable $e) {
\OCP\Server::get(LoggerInterface::class)->warning('Failed to gather federation hosts for CSP', [
\OC::$server->query(LoggerInterface::class)->warning('Failed to gather federation hosts for CSP', [
'exception' => $e,
'app' => 'officeonline'
]);
Expand All @@ -184,4 +191,52 @@ private function domainOnly(string $url): string {
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
return "$scheme$host$port";
}

private function registerNewFileCreators($context) {
$context->injectFn(function (ITemplateManager $templateManager, IL10N $l10n, IConfig $config) {
if (!$this->isEnabled()) {
return;
}
$ooxml = $config->getAppValue(self::APP_ID, 'doc_format', '') === 'ooxml';
$templateManager->registerTemplateFileCreator(function () use ($l10n, $ooxml) {
$odtType = new TemplateFileCreator('richdocuments', $l10n->t('New document'), ($ooxml ? '.docx' : '.odt'));
if ($ooxml) {
$odtType->addMimetype('application/msword');
$odtType->addMimetype('application/vnd.openxmlformats-officedocument.wordprocessingml.document');
} else {
$odtType->addMimetype('application/vnd.oasis.opendocument.text');
$odtType->addMimetype('application/vnd.oasis.opendocument.text-template');
}
$odtType->setIconClass('icon-filetype-document');
$odtType->setRatio(21 / 29.7);
return $odtType;
});
$templateManager->registerTemplateFileCreator(function () use ($l10n, $ooxml) {
$odsType = new TemplateFileCreator('richdocuments', $l10n->t('New spreadsheet'), ($ooxml ? '.xlsx' : '.ods'));
if ($ooxml) {
$odsType->addMimetype('application/vnd.ms-excel');
$odsType->addMimetype('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
} else {
$odsType->addMimetype('application/vnd.oasis.opendocument.spreadsheet');
$odsType->addMimetype('application/vnd.oasis.opendocument.spreadsheet-template');
}
$odsType->setIconClass('icon-filetype-spreadsheet');
$odsType->setRatio(16 / 9);
return $odsType;
});
$templateManager->registerTemplateFileCreator(function () use ($l10n, $ooxml) {
$odpType = new TemplateFileCreator('richdocuments', $l10n->t('New presentation'), ($ooxml ? '.pptx' : '.odp'));
if ($ooxml) {
$odpType->addMimetype('application/vnd.ms-powerpoint');
$odpType->addMimetype('application/vnd.openxmlformats-officedocument.presentationml.presentation');
} else {
$odpType->addMimetype('application/vnd.oasis.opendocument.presentation');
$odpType->addMimetype('application/vnd.oasis.opendocument.presentation-template');
}
$odpType->setIconClass('icon-filetype-presentation');
$odpType->setRatio(16 / 9);
return $odpType;
});
});
}
}
76 changes: 0 additions & 76 deletions src/files.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import Types from './helpers/types'
import axios from '@nextcloud/axios'
import { getCapabilities } from '@nextcloud/capabilities'
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)
Expand All @@ -19,77 +16,6 @@ Vue.prototype.n = window.n
Vue.prototype.OC = window.OC
Vue.prototype.OCA = window.OCA

const NewFilePlugin = {
attach: function(newFileMenu) {
const self = this
const document = Types.getFileType('document')
const spreadsheet = Types.getFileType('spreadsheet')
const presentation = Types.getFileType('presentation')

newFileMenu.addMenuEntry({
id: 'add-' + document.extension,
displayName: t('officeonline', 'New Document'),
templateName: t('officeonline', 'New Document') + '.' + document.extension,
iconClass: 'icon-filetype-document',
fileType: 'x-office-document',
actionHandler: function(filename) {
self._createDocument(document.mime, filename)
},
})

newFileMenu.addMenuEntry({
id: 'add-' + spreadsheet.extension,
displayName: t('officeonline', 'New Spreadsheet'),
templateName: t('officeonline', 'New Spreadsheet') + '.' + spreadsheet.extension,
iconClass: 'icon-filetype-spreadsheet',
fileType: 'x-office-spreadsheet',
actionHandler: function(filename) {
self._createDocument(spreadsheet.mime, filename)
},
})

newFileMenu.addMenuEntry({
id: 'add-' + presentation.extension,
displayName: t('officeonline', 'New Presentation'),
templateName: t('officeonline', 'New Presentation') + '.' + presentation.extension,
iconClass: 'icon-filetype-presentation',
fileType: 'x-office-presentation',
actionHandler: function(filename) {
self._createDocument(presentation.mime, filename)
},
})
},

_createDocument: function(mimetype, filename) {
const dir = getCurrentDirectory()
try {
OCA.Files.Files.isFileNameValid(filename)
} catch (e) {
window.OC.dialogs.alert(e, t('core', 'Could not create file'))
return
}
filename = FileList.getUniqueName(filename)
const path = dir + '/' + filename

const isPublic = document.getElementById('isPublic') ? document.getElementById('isPublic').value === '1' : false
if (isPublic) {
return window.FileList.createFile(filename).then(function() {
OCA.Viewer.open({ path })
})
}

axios.post(OC.generateUrl('apps/officeonline/ajax/documents/create'), { mimetype, filename, dir }).then(({ data }) => {
console.debug(data)
if (data && data.status === 'success') {
window.FileList.add(data.data, { animate: true, scrollTo: true })
window.OCA.Viewer.open({ path })
} else {
window.OC.dialogs.alert(data.data.message, t('core', 'Could not create file'))
}
})
},
}

document.addEventListener('DOMContentLoaded', () => {
// PUBLIC SHARE LINK HANDLING
const isPublic = document.getElementById('isPublic') ? document.getElementById('isPublic').value === '1' : false
Expand All @@ -103,6 +29,4 @@ document.addEventListener('DOMContentLoaded', () => {
render: h => h(Office, { props: { fileName: document.getElementById('filename').value } }),
}).$mount('#imgframe')
}
// new file menu
OC.Plugins.register('OCA.Files.NewFileMenu', NewFilePlugin)
})

0 comments on commit 288f09a

Please sign in to comment.