1 /**
  2  * Workflow return the contents of a folder using a REST call
  3  */
  4 var REST = library.REST.REST();
  5 var myIndex;
  6 var myPageSize;
  7 var myData = {};
  8 var myCurrentSrcFolder = "";
  9 /**
 10  *  adds aFiles name to the array of files in the return object in myData
 11  * @param theFile file to add to the list of files
 12  */
 13 function addFile(theFile) {
 14   myData[myCurrentSrcFolder].files[myData[myCurrentSrcFolder].files.length] =
 15     theFile.assetId;
 16 }
 17 
 18 /**
 19  *  adds a Folder name to the array of folders in the return object in myData
 20  * @param theFolder folder to add to the list of files
 21  */
 22 function addFolder(theFolder) {
 23   //exclude renditions folder from list
 24   if (theFolder.path.indexOf("Renditions") != 0) {
 25     myData[myCurrentSrcFolder].folders[
 26       myData[myCurrentSrcFolder].folders.length
 27     ] = theFolder.name;
 28   }
 29 }
 30 
 31 /**
 32  * Inits JSON for the current src folder
 33  * @param theSrcPath
 34  */
 35 function initReturnObject(theUsersRootPath) {
 36   myCurrentSrcFolder = theUsersRootPath;
 37   myData[myCurrentSrcFolder] = {};
 38   if (myData[myCurrentSrcFolder].folders == null) {
 39     myData[myCurrentSrcFolder].folders = [];
 40   }
 41   if (myData[myCurrentSrcFolder].files == null) {
 42     myData[myCurrentSrcFolder].files = [];
 43   }
 44 }
 45 
 46 /**
 47  * Adds an object to myData containing an array of files and an array of folders
 48  * @param theSource the path of the folder to get the contents of
 49  */
 50 function folderContents(theSource) {
 51   var aPath = REST.getFullPath(theSource);
 52   var aFolder = new ManagedFolder(aPath);
 53 
 54   if (aFolder.path.indexOf("Renditions") == 0) {
 55     //if user is asking for renditions folder fail with same error as if it didn't exist
 56     throw "folderContents failed to execute: JavaException: com.brightech.mb.workflow.js.InvalidManagedFolderPathException: ManagedFolder: no folder with the path Renditions/ (relative to the current asset root folder) exists. ";
 57   }
 58   initReturnObject(REST.getACLRelativePath(aPath));
 59 
 60   var aFileAndFolderIterator = aFolder.getContents(1);
 61   aFileAndFolderIterator.setIndex(myIndex);
 62   var i = 0;
 63   while (aFileAndFolderIterator.hasNext()) {
 64     if (i >= myPageSize) {
 65       break;
 66     }
 67     var aFileOrFolder = aFileAndFolderIterator.next();
 68     if (fileManager.isManaged(aFileOrFolder)) {
 69       if (fileManager.isFile(aFileOrFolder.path)) {
 70         addFile(aFileOrFolder);
 71       } else if (fileManager.isFolder(aFileOrFolder.path)) {
 72         addFolder(aFileOrFolder);
 73       }
 74     }
 75     i++;
 76   }
 77 }
 78 
 79 /**
 80  * Returns the names of the files and sub-folders contained in a folder specified by path.
 81  * @description  Discovers the names of the files and folders contained in a folder specified in 'src'. These names are
 82  * parsed into a text object keyed by AssetID from the JSON file in which they are contained, and by default are
 83  * displayed to the user.
 84  * @example 'MBurl'/wf/restapi/1/folderContents?src=["FolderPath"]
 85  * @example <a target="_blank" href=http://127.0.0.1:55555/wf/restapi/1/folderContents?src=["Assets/"]>http://127.0.0.1:55555/wf/restapi/1/folderContents?src=["Assets/"]</a>
 86  * @class Returns the names of the files and sub-folders contained in a folder specified by path.
 87  * @name FolderContents
 88  * @param src Path/list of paths to folder or folders for content reading.
 89  * @param page_size The number of results to return - optional
 90  * @param index The starting index - optional
 91  * @returns ( {"AssetID": {"files":["TheFiles"],"folders":["TheFolders"]},...} )
 92  */
 93 function main() {
 94   myPageSize = REST.getParameter("page_size", false);
 95   myIndex = REST.getParameter("index", false);
 96   myPageSize =
 97     myPageSize === "" || isNaN(myPageSize)
 98       ? Number.MAX_VALUE
 99       : parseInt(myPageSize);
100   myIndex = myIndex === "" || isNaN(myIndex) ? 0 : parseInt(myIndex);
101   myData.page_size = myPageSize;
102   myData.index = myIndex;
103   var aParameters = REST.getParametersToIterate("src");
104   if (myData.error != null) {
105     return REST.formatResponse();
106   }
107   REST.iterateThroughParameters(aParameters, folderContents);
108   return REST.formatResponse();
109 }
110 main();
111