Sync AI2 Folder to Google Drive Folder

INTRO

Had this one on my long list for a while....

In simple terms, I have a folder on my device, in the application specific directory (ASD), and a folder on my Google Drive (it can be public or private). Whatever files are in either of these folders can be synchronised with the other, and I can add or delete files in the device folder, and this will also be synchronised.

This is an ideal example of making use of my GD Connector (MIT Forum Topic), which would allow any user to sync with a folder on their OWN google drive. All the functions required for this are there, just would need coding with blocks.  To keep things simple, the demonstration here uses the developer's google drive, which in itself could be useful for multiple users as a shared file repository.

Files can be added or removed from either folder "outside" of the app, if this is done, then I can synchronise the folders to restore the balance. I have used two way synchronisation, which means that both folders will be equivalent once synchronised. Other methods could be adopted.

You will see that for each routine involving the web component I use a variable "control" to tell the Web1.GotText what to do. For downloading files from google drive, the file is converted to base64 in the webapp, so that it can be downloaded as text. This alows me to use only one web component, and also allows for the google drive folder to remain private. 

Requirements:

Google account

Google Apps Script Web App (standalone)

Extensions: (credits to all extension creators listed below)


SETUP

SCRIPT

function doGet(e) {

  

  //return list of filenames and ids

  if ( e.parameter.func == "list" ) {

  return listFiles();

  

  //trash file in folder

  } else if (e.parameter.func == "del") {

   DriveApp.getFileById(e.parameter.fileID).setTrashed(true);

   return listFiles();

  

  // download file from google drive sync folder

  } else if ( e.parameter.func = "dn" ) {

    var imgID = e.parameter.imgID;

    var bytes = DriveApp.getFileById(imgID).getBlob().getBytes();

    return ContentService.createTextOutput(Utilities.base64Encode(bytes));

  }

}


function doPost(e) {

  

  // upload file to google drive sync folder

  if ( e.parameter.func = "up" ) {

    var data = Utilities.base64Decode(e.parameters.data);

    var blob = Utilities.newBlob(data, e.parameters.mimetype, e.parameters.filename);

    var fileID = DriveApp.getFolderById('<FOLDER ID HERE>').createFile(blob).getId();

    return listFiles();

  }

}



function listFiles() {

  var folderID = "<FOLDER ID HERE>";

  var folder = DriveApp.getFolderById(folderID);

  var files = folder.getFiles();

  var myList = [];

  while(files.hasNext()) {

    var file = files.next();

    myList.push( file.getName() + "," + file.getId() );

    }

  return ContentService.createTextOutput(JSON.stringify(myList)).setMimeType(ContentService.MimeType.JSON);

}

WORKFLOW

There are just over 600 blocks in this demonstration, I will attempt to show the most useful routines as I explain the workflow. Full view of all blocks is available below, along with the aia project.


BLOCKS

VIDEO

Well, that just about wraps this one up, I hope it helps you to see how to synchronise a device folder with a google drive folder, and the work required to setup a useable UI in the app to handle things.