googleinterns / filesystem-in-appsscript

Apache License 2.0
5 stars 1 forks source link

[Ready - 1] File system - file handling #49

Closed satviksr closed 4 years ago

satviksr commented 4 years ago

Fixes #56, Fixes #79 Implement File Handling APIs. Documentation: https://github.com/googleinterns/filesystem-in-appsscript/blob/master/docs/FileSystem.md

Helper functions and ApiUtil Class

Across the file handling component, APIs for copy, move, delete were available in every class for both files and folders. The implementation logic and design for copy and move was similar. Similarly the implementation difference between files and folders was mostly the different API calls. In order to make design, implementation and testing easier, generic helper functions was created.

One such API is the cloneEntity API which can emulate copy and move for both files and folders.

This was possible because of ApiUtil class which generates an object according to the type. This design is based on abstract factory pattern.

/**
 * Drive App and FileMapper expose different APIs for file and folder
 * operations. However, the APIs have similar interfaces and functionality. In
 * order to achieve reuse in code, ApiUtil is a helper class which returns File
 * or Folder library object for both DriveApp and FileMapper classes. Abstract
 * Factory Pattern has been used here.
 */
ApiUtil = {
    /**
    * Helper function to get File or Folder FileMapper library object
    * @param {boolean} isFile Flag indicating if the required library is for file entity
    * @return {object} Library object for FileMapper
    */
    getFileMapper(isFile)
    /**
    * Helper function to get File or Folder DriveApp library object
    * @param {boolean} isFile Flag indicating if the required library is for file entity
    * @return {object} Library object for DriveApp
    */
    getDriveApi(isFile)
};

The factory can be used as follows to generate the required object.

var fileMapperApi = ApiUtil.getFileMapper(true); // If we are working with files
var driveApi = ApiUtil.getDriveApi(false); // If we are working with folders

The API interface of FileMapper and DriveApi are as follows:

FileMapperApi = {
    hasEntity(localPath)
    getEntityId(localPath)
    deleteEntity(localPath)
    copyEntity(localPath, target)
    moveEntity(localPath, target)
    findByPattern(pattern)
};

DriveAppFileApi = {
    getEntityById(id)
    makeCopy(source, name, target)
    makeMove(source, name, target)
};

APIs Implemented

Test