Nextcloud Google Docs Redirect

Nextcloud plugin to open Google Doc files in the Google Doc web application in a new tab instead of downloading them.

Background

I technically support the Calluna Festival from Hamburg that deals with the topic of sustainability. The team wants to move away from Google and focus on self-hosted services to increase privacy. So I set up a Nextcloud instance and moved their Google Drive files there.

The data includes lots of Google Drive documents like *.gddoc, .gdsheets, etc. Unfortunately, Nextcloud has no clue what to do with these files. In contrast to Google Drive, these documents are not opened in a new tab in the Google Docs application but will be downloaded. This hardly is what users expect or want.

Solution

The Google Docs file format turns out to be a simple JSON object that contains the URL to edit the document:

{
  "url": "https://docs.google.com/document/d/5e4d819a/edit",
  "file_id": "5e4d819a", 
  "account_email": "some@email.com"
}

So the idea to improve UX is simple: Write a Nextcloud plugin that hooks into the on file view event to parse the JSON object of the given document and redirect to the contained Google Docs URL instead of downloading the file. Unfortunately, I was unable to find a suitable Nextcloud hook to accomplish this server-side with PHP. There is one to load additional JavaScript code, however. I used this event to add JavaScript to intercept clicks on Google Docs files, fetch and parse the JSON data client-side with window.fetch, and finally trigger the redirect with window.open. This approach is not particularly elegant, but it works.

Installation

I have uploaded this app to the Nextcloud "App Store" recently. So simply search for Google Docs Redirect to add it to your Nextcloud.