Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: File creation on Nextcloud 28 #519

Merged
merged 2 commits into from
Feb 15, 2024
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
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<screenshot>https://github.com/nextcloud/officeonline/raw/main/screenshot.png</screenshot>

<dependencies>
<nextcloud min-version="24" max-version="27"/>
<nextcloud min-version="24" max-version="28"/>
</dependencies>

<settings>
Expand Down
58 changes: 56 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@
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 +72,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 +168,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', [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need this still for 24 compatibility.

'exception' => $e,
'app' => 'officeonline'
]);
Expand All @@ -184,4 +190,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)
})
Loading