Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Setting Up a Private Google Drive Alternative Written on Golang

DZone 's Guide to

Setting Up a Private Google Drive Alternative Written on Golang

If making your own version of Google Drive sounds like a fun project, read on to learn how to use this open source API and the Go language.

· Web Dev Zone ·
Free Resource

Following the previous articles about integrating ONLYOFFICE online editors, today we are bringing over another integration case where the Sync&Share service is written in the Go language.

We are going to illustrate it with Pydio Cells, an example of the open-source file-sharing platform for enterprises that follows microservices architecture and is hosted either on-premises or on the cloud of your own choice. The combination with ONLYOFFICE basically transforms Pydio Cells into the live private cloud alternative to Google Drive.

Before You Start

As the resource for the development process, ONLYOFFICE API documentation is used, while the testing environment can be easily established with a free community version of ONLYOFFICE Document Server. It features up to 20 simultaneous editing sessions, which is enough for trying casual team-style collaboration in the integrated service.

To integrate ONLYOFFICE Document Server community version, you must first install it on your local machine. The easiest way to do this is by using the Docker container:

docker run -itd -p 8080:80 onlyoffice/documentserver

Read more about the licenses in ONLYOFFICE in the article about integrating ONLYOFFICE with Node.js apps.

Importantly, ONLYOFFICE has a number of prerequisites required for successful integration, namely:

  1. Anonymous access for downloading and saving files. No user authorization data on the server side is involved.
  2. Permission for adding new elements to the UI.
  3. Opening a new page (for executing the editor script).
  4. Permission for specifying the settings (at least the virtual address of ONLYOFFICE Document Server).

How the Integration Works in Pydio Cells

When a file is opened for editing in ONLYOFFICE on the Pydio Cells platform, a JavaScript script that loads the ONLYOFFICE api.js file is created via proxy. The proxy redirects to the location of ONLYOFFICE Document Server instance the admin user has set up on a server and configured via the Pydio Admin console.

const script = document.createElement("script");

script.src = "/ONLYOFFICE/web-apps/apps/api/documents/api.js";

script.async = false;

document.body.appendChild(script);

Example of ONLYOFFICE server configuration in Pydio Cells Console

Once the script is loaded, Pydio generates the file configuration data (document content, location, and permissions, mode, callback URL for the server) and, along with that, binds a DocsAPI.DocEditor to a container in the page.

script.addEventListener('load', function() {
            const user = Pydio.getInstance().user
            // Retrieving document presigned GET url
            PydioApi.getClient().buildPresignedGetUrl(node).then(urlGet => {
                // Retrieving pydio main user
                PydioApi.getRestClient().getOrUpdateJwt().then(jwt => {
                    user.getIdmUser().then(idmUser => {
                        const config = {
                            "document": {
                                "fileType": node.getAjxpMime(),
                                "title": node.getLabel(),
                                "key": node.getMetadata().get("etag") + node.getMetadata().get("uuid"),
                                "url": urlGet,
                                "permissions": {
                                    "comment": true,
                                    "download": true,
                                    "edit": readonly ? false : true,
                                    "fillForms": true,
                                    "print": true,
                                    "review": true
                                },
                            },
                            "editorConfig": {
                                "callbackUrl": window.location.protocol + "//" + window.location.host + "/onlyofficecb?access_token="+jwt+"&uuid="+node.getMetadata().get("uuid"),
                                "customization": {
                                    "autosave": true,
                                    "chat": true,
                                    "commentAuthorOnly": false,
                                    "compactToolbar": true,
                                    "help": true,
                                    "showReviewChanges": false,
                                    "zoom": 100
                                },
                                "lang": user.preferences.get("lang"),
                                "user": {
                                    "id": idmUser.Uuid,
                                    "name": idmUser.Login
                                },
                                "mode": readonly ? "view" : "edit"
                            }
                        };

                        const docEditor = new DocsAPI.DocEditor("onlyOfficeContainer", config);
                    })
                })
            })
        });

To retrieve the initial content of the file, Pydio generates a pre-signed URL. ONLYOFFICE Document Server interacts with S3 storage directly to retrieve the content without exposing the S3 credentials.

The callback URL contains the address of the callback handler and the user’s access JWT as a parameter. The ONLYOFFICE callback handler server runs as a microservice within the Pydio Cells architecture. This server handles GET or POST methods. Every time a request is made, Pydio verifies the validity of the access token given in the argument. When the request contains a link to a change in the content of the file, Pydio downloads the content and posts it to a pre-signed POST URL.

Reading permissions to the file for the user in the context of the current workspace are controlled before opening the JavaScript editor and when generating the pre-signed URL for the DocEditor config.

These permissions are checked when opening the Document Editor to determine the mode used (View or Edit) and the editing permission (true or false). It is also checked when generating the pre-signed POST URL.

General login permissions are checked for every request going through the Pydio Cells server.

Example of a file in Pydio Cells with ONLYOFFICE

Example of a file in Pydio Cells with OnlyOffice

ONLYOFFICE editors can be integrated into web applications written in any programming language. We hope you find this specific case of Go useful for your practice.

To have a look at integrating ONLYOFFICE with Nextcloud, see our previous article.

This article was written in collaboration with Pydio developers. Reach us via the comment section to ask us any questions and share your suggestions for new ONLYOFFICE integrations.

Topics:
integration ,api ,cloud application ,golang ,web dev

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}