google-apps-scriptgoogle-drive-apigoogle-workspacegsuite-addons

Intercept or get callbacks on Google Drive file save and open in addon/apps script


I want to build and AddOn for Google Drive that does extra pre-processing/decoration of files when they are uploaded and before opened/downloaded. E.g. add/verify digital signature, call 3rd party service passing file metadata, convert to different mime type, encrypt/decrypt certain files with custom key that's generated by 3rd party service. I looked through all APIs and don't find any feature that allows to intercept file uploads/downloads and do pre-processing (e.g. intercept uploaded content, reading it as blob, do processing, then save it to Google Drive folder, later when user downloads file, do pre-processing/verification again on file contents and return end result) in a way that's transparent to user.

Another possibility in case intercepting the content would be to try intercept the file selection/upload event and change file extension or path to temp folder, then triggering processing script and saving result to original destination.

I prefer to avoid proxying file uploads/downloads via my custom backend, doing all required processing in context of addon/apps script.

I got a bit confused of how AppsScript GSUite Drive API (File, Folder, Drive classes) relates to Google Drive v2 API with Changes.watch(), Channels, etc... Is latter intended for backend use only? I can't see any notion of events/callbacks provided by AppsScript API, that in theory should be running my addon in context of current user.

I wish I would be abler to ask more concrete question here on API, but currently I struggling to understand how to do basic hooks for my addon to operate on files.


Solution

  • You can't intercept them. With a Drive Add-on you can only trigger functions when the user enters Drive's homepage or when the user select a file(s). With the API you can watch when a file is modified in order to get a notification, but this will be after the change is made without intercepting the change process.

    If you use a OnItemsSelectedTrigger, you need to prompt/return a Card when the user selects a file(s), in this card you can set buttons that when clicked by the user it can trigger different actions.