Update Any File on Google Drive with AI2

INTRO

We know how to upload a file to google drive, but there may be times when you want to keep the same fileID but change the file with a new version. You can do this using Google Drive itself by right clicking on the file and selecting Manage Versions, which allows you to upload a new version of the file whilst retaining the fileID. This guide will show how you can do the same using a google apps script web app. I have used images in the example for visual return, but this could apply to any file type that you can convert to base64 and store on google drive. My thanks to Juan Antonio for the KIO4 Base64 Extension which helps to make this all work. This demo has blocks set to work in development/Companion mode, but should work for all Android versions. May need adjustment for a compiled app. Tested in companion mode (v2.60u) on Android 7 and Android 10.

SCRIPT

How it all works. The ai2 app uploads a file as a base64 string, sending the filename, the mimetype, and the ID for the containing folder along too. The filename remains constant in the app and on google drive for updating to work, but you can change it in the app to create and then update a different file. When the script receives the base64 string, the is decoded and converted to a "blob". The script then checks the specified folder for the filename. If the filename is not found then a new file is created (from a slightly different blob) with the filename in the specified folder. If the filename is found then the existing file is updated with the new file in the blob. The script then returns the fileID to the app.

When creating the script, you will need to add the Advanced Drive Service. With the legacy editor you do this by going to Resources > Advanced Google Services, then setting "Drive API" to "On".


function doPost(e) {


var fileID = '';

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

var folder = DriveApp.getFolderById(e.parameters.folder);

var filename = e.parameters.filename;

var mimetype = e.parameters.mimetype;

var blob = Utilities.newBlob(data);

var existing = folder.getFilesByName(filename);

if (existing.hasNext()) {

var file = existing.next();

if (file.getName() == filename) {

fileID = file.getId();

Drive.Files.update({title: filename, mimeType: mimetype}, fileID, blob);

}

} else {

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

fileID = folder.createFile(blob).getId();

}


return ContentService.createTextOutput("File Updated with ID: " + fileID);


}

BLOCKS

The app allows the user to select a file from a list, this then displays the selected file. The user can then upload the file to google drive. If the file myImage.png does not exist then it is created using the uploaded file. If the file by that name does exist then the existing file is replaced with the uploaded file. The app then shows the responseContent with the fileID, and displays the file from Google Drive. This should be the same image as the one the user selected. The folder on google drive needs view permissions for everyone, in order to display the updated image in the app.

The blocks are setup to be used in development/Companion mode selecting png image files loaded in the assets.

VIDEO

updateGD.webm

RESOURCES & CREDITS

Thanks again to Juan antonio for the Base64 Extension