📜 ⬆️ ⬇️

How we integrated with ownCloud and Nextcloud: pitfalls and how to overcome them

One day, one of our users wrote that ONLYOFFICE and ownCloud is a marriage made in heaven (sorry, it was match made in heaven in the original) and it’s strange that no one has thought of writing any plugin. We figured and thought he was right. Moreover, we love to integrate! In terms of what, of course, we want our editors to use always, wherever and wherever it is convenient.

In general, we ourselves have made an application for integrating our document editors with ownCloud (and then Nextcloud), and in this article we will tell a little about it. There were no insurmountable obstacles on our way, but some difficulties have arisen and we will share with you our experience in overcoming them. First, it may be useful to you, and secondly, we can no longer keep it to ourselves! That is, for a long time nothing was written to the blog.



')

Creating an application is simple; make it work with your ownCloud - no


So, the application for integration, it’s the connector, it’s our sweetheart - just a bridge between the ownCloud document management web service and our Document Server (Document server, our editors, in general). ownCloud graciously provides an API for writing and embedding applications in your workspace, and we took advantage of this.

The official ownCloud documentation helped us a lot in creating the application, which also prevented it . Having executed the steps specified there, we received a ready template of the application. It was easy, but then the difficulty began. The internal API is documented, to put it mildly, not too detailed.

For example, to save a file, we needed to get authorization for a specific user. To perform an authorization action, you need to run certain commands: which commands? Where to find them? How to execute them? What do you think the documentation says? The correct answer is nothing.

Creative method


Difficulties without documentation began at the very first stage. In order to create a binding to ownCloud on one server and our Document Server on another and to organize data exchange between them, you must first specify the web address of the Document Server. This address is stored in the database for the application and then used when opening the editor or converting documents.

In general, we need to specify the address, but where? Where are we going to point it? First we need the ONLYOFFICE settings page in the ownCloud administration panel, but for some reason it didn’t appear there by itself (although we really wanted to). In general, this page must somehow write.

Here other applications for ownCloud with open source came to the rescue. I had to spy how they coped with this task. Specifically, the official application of ownCloud Antivirus App for files helped us with the settings page.

Here's what and where we added, guided by the experience of the developers of this application:

/appinfo/app.php
App::registerAdmin("onlyoffice", "settings") 

/settings.php
 User::checkAdminUser(); return new Application() -> getContainer() -> query("\OCA\Onlyoffice\Controller\SettingsController") -> index() -> render(); 

/controller/settingscontroller.php
 class SettingsController extends Controller { private $config; public function __construct($AppName, IRequest $request, AppConfig $config) { parent::__construct($AppName, $request); $this->config = $config; } public function index() { $data = ["documentserver" => $this->config->GetDocumentServerUrl()]; return new TemplateResponse($this->appName, "settings", $data, "blank"); } } 

Information on how to get access to the file system is in the documentation , but in order to learn how to get the content of a file by reference, we needed to examine the official Gallery application.

Here is how we did it:

/lib/downloadresponse.php
 class DownloadResponse extends Response { private $content; public function __construct(File $file) { $this->setStatus(Http::STATUS_OK); $this->content = $file->getContent(); $this->addHeader("Content-type", $file->getMimeType() . "; charset=utf-8"); $this->addHeader("Content-Disposition", "attachment; filename*=UTF-8''" . rawurlencode($file->getName()) . "; filename=\"" . rawurlencode($file->getName()) . "\""); } public function render() { return $this->content; } } 


We learned how to add our action to the menu of creating a new file and to the context menu of the file using the official Text Editor application.

Here is how the file is created:

/js/main.js
 OCA.Onlyoffice.NewFileMenu = { attach: function (menu) { if (menu.fileList.id !== "files") { return; } menu.addMenuEntry({ id: "onlyofficeDocx", displayName: t(OCA.Onlyoffice.AppName, "Document"), iconClass: "icon-onlyoffice-new-docx", fileType: "docx", actionHandler: function (name) { OCA.Onlyoffice.CreateFile(name + ".docx", menu.fileList); } }); } }; OC.Plugins.register("OCA.Files.NewFileMenu", OCA.Onlyoffice.NewFileMenu); 


And here is how it opens in ONLYOFFICE:

 OCA.Onlyoffice.FileList = { attach: function (fileList) { if (fileList.id == "trashbin") { return; } $.each(OCA.Onlyoffice.mimes, function (ext, attr) { fileList.fileActions.registerAction({ name: "onlyofficeOpen", displayName: t(OCA.Onlyoffice.AppName, "Open in ONLYOFFICE"), mime: attr.mime, permissions: OC.PERMISSION_READ, icon: function () { return OC.imagePath(OCA.Onlyoffice.AppName, "btn-edit"); }, actionHandler: function (fileName, context) { OCA.Onlyoffice.FileClick(fileName, context, attr); } }); }); } }; OC.Plugins.register("OCA.Files.FileList", OCA.Onlyoffice.FileList); 


So, we are great - the Open in ONLYOFFICE action and the ability to create a new file appeared in the context menu of the files: a document, table or presentation (the file is created on the server as a copy into the specified folder with the specified OOXML file format - docx, xlsx, pptx) .

Additional format information


ONLYOFFICE editors work with the following formats (but differently): DOCX, XLSX, PPTX, PPSX, TXT, CSV, ODT, ODS, ODP, DOC, XLS, PPT, PPS, EPUB, RTF, MHT, HTML, XPS, PDF , DJVU.

When selecting an action for ODT, ODS, ODP, DOC, XLS, PPT, PPS, EPUB, RTF, MHT, the HTML file will first be sent for conversion to the appropriate OOXML format. For DOCX, XLSX, PPTX, PPSX, TXT, a new tab with an editor will open, and for CSV, XPS, PDF, DJVU a tab with an editor in view mode.

Now about Nextcloud


As soon as we informed the world that we were working with our ownCloud, the world immediately demanded that we work with Nextcloud too. We considered these requirements fair, but the meeting began to work and in this system did not work.

Here are the difficulties:

  1. Going icons (of course, fixing styles is not a problem).
  2. Content Security Policy was added to Nextcloud (the inline script for initialization of the editor was required to be signed).
  3. New requirements for the Settings page.

It was because of the third point that we thought for a while that we would have to make two separate applications for related systems. That's how it was - to implement a new interface in NextCloud, you had to register our settings class in the application description:

/appinfo/inxo.xml
 <settings> <admin>OCA\Onlyoffice\Controller\SettingsController</admin> </settings> 


And then in the class to implement the required ISettings interface:

/controller/settingscontroller.php
 class SettingsController extends Controller implements ISettings { public function getForm() { return $this-index(); } public function getSection() { return "server"; } public function getPriority() { return 50; } ... 


But here we have a little problem. Bumping into this line, ownCloud fell, since in its environment no ISettings interface exists:

 class SettingsController extends Controller implements ISettings 


I had to do everything in a clever way: the implementation of the ISettings interface was done in a separate class, leaving the layout for ownCloud and Nextcloud unified.

/appinfo/inxo.xml
 <settings> <admin>OCA\Onlyoffice\AdminSettings</admin> </settings> 


/lib/adminsettings.php
 class AdminSettings implements ISettings { public function getForm() { $app = new Application(); $container = $app->getContainer(); $response = $container->query("\OCA\Onlyoffice\Controller\SettingsController")->index(); return $response; } public function getSection() { return "server"; } public function getPriority() { return 50; } } 


Thus, the settings page for ownCloud connects according to the old algorithm, and for Nexcloud - the new one. By the way, there were initiative guys who pulled the fork from our application and finished it to Nextcloud themselves. On the forums they wrote that it works, and we naturally became interested in how everything was done there. But solutions to the problem with the settings were not found and did everything in their own way.

What happened?


We got an application that allows you to work with documents using ONLYOFFICE editors in the interface of the popular ownCloud and NextCloud. Officially, both companies cooperate with Collabora, but, as you know, it is not possible to expand it to everyone, but there is no full co-editing there. Plus, we chose different formats as the main ones, and if you like X-based ones, ONLYOFFICE is sharpened for them. With the connector, you can connect your ownCloud or NextCloud to both the free Community version and the Enterprise version.

Detailed instructions in our documentation .

PS If you delve into the documentation, you can also find plugins for Confluence and Alfresco there . The latter was not originally written by us, but in a fit of inspiration, we recently brought it to mind, what if you need it? In the near future, we will also please you with interesting integrations, undo in fast joint editing, and some other important and good news. Stay with us!

Source: https://habr.com/ru/post/324498/


All Articles