1 /**
  2  * Workflow to rename files or folders usin a REST call
  3  */
  4 var REST = library.REST.REST();
  5 var myData = {};
  6 
  7 /**
  8  * Renames the source file or folder and adds an object to myData containing its assetID/folderPath
  9  * @param theSource assetID/folderPath
 10  * @param aName the new name of the asset/folder
 11  */
 12 function rename(theSource, aName) {
 13   var aFile = fileManager.getFileObjectById(theSource);
 14   var aFolder;
 15   if (aFile == null) {
 16     aFolder = new ManagedFolder(theSource);
 17     aFolder = fileManager.folderRename(aFolder, aName);
 18     myData[theSource] = aFolder.path;
 19     return;
 20   }
 21   if (aName.indexOf(".") >= 0) {
 22     aFile = fileManager.fileRename(aFile, aName);
 23   } else {
 24     aFile = fileManager.fileRename(
 25       aFile,
 26       aName + "." + aFile.fileNameExtension
 27     );
 28   }
 29   myData[theSource] = aFile.path;
 30 }
 31 
 32 /**
 33  * Renames folders and files.
 34  * @description Locates assets by AssetID's and folders by Path in 'src', renames them with the matching names specified in the 'name' parameter, and returns their new Path values. These results are parsed into a text object keyed by AssetID from the JSON file in which they are contained, and by default are displayed to the user.
 35  * @example 'MBurl'/wf/restapi/1/rename?src=["Path"]&name=["New Name"]
 36  * @example <a target="_blank" href=http://127.0.0.1:55555/wf/restapi/1/rename?src="12345"&name="TestingRename">http://127.0.0.1:55555/wf/restapi/1/rename?src="12345"&name="TestingRename"</a>
 37  * @class Renames folders and files.
 38  * @name Rename
 39  * @param src AssetID/list and/or Path/list of folders to be renamed.
 40  * @param name Name/list of new names for the files to be renamed.  The new names must be in the same order as their correlating src
 41  * @returns ( {'AssetID': {"success": "true","return": "AssetID"},...} )
 42  */
 43 function main() {
 44   var aParameters = REST.getParametersToIterate(["src", "name"]);
 45   if (myData.error != null) {
 46     return REST.formatResponse();
 47   }
 48   REST.iterateThroughParameters(aParameters, rename);
 49   return REST.formatResponse();
 50 }
 51 main();
 52