1 /**
  2  * Created by lukemallory on 11/18/14.
  3  *
  4  * Workflow to create folders given paths using a REST call
  5  */
  6 var myData = {};
  7 library.REST;
  8 /**
  9  *  Creates a new folder with the given path and adds an object to myData with the path
 10  * @param theSource the path to the new folder
 11  * @returns {*}
 12  */
 13 function createFolder(theSource)
 14 {
 15 	var aFolder;
 16 	try
 17 	{
 18 		aFolder = new ManagedFolder(theSource);
 19 		REST.submitError(aFolder.path, "Folder already exists");
 20 		return JSON.stringify(myData, null, 2);
 21 	} catch (err)
 22 	{
 23 		var aIndex = new ManagedFolder("/")
 24 		var aCreatedFolder = fileManager.folderNew(aIndex, theSource);
 25 		myData[theSource] = aCreatedFolder.path;
 26 	}
 27 }
 28 
 29 function createFile(theSource)
 30 {
 31 	if (fileManager.isFile(theSource))
 32 	{
 33 		REST.submitError(theSource,"File already exists");
 34 	} else
 35 	{
 36 		var aNewFile = fileManager.fileNewByPath(theSource);
 37 		myData[aNewFile.assetId] = theSource;
 38 	}
 39 }
 40 
 41 function create(thePath)
 42 {
 43 	if (thePath.indexOf(".") >= 0)
 44 	{
 45 		createFile(thePath);
 46 	} else
 47 	{
 48 		createFolder(thePath);
 49 	}
 50 }
 51 /**
 52  * Creates new folders and returns the paths to them.
 53  * @description  Creates new folders for the list of paths in 'src' and returns the paths to the new folders. These path values 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.
 54  * @example 'MBurl'/wf/create?src=['FolderPath/FilePath']
 55  * @example <a target="_blank" href=http://127.0.0.1:55555/wf/create?src=["Assets/Library/test.txt"]>http://127.0.0.1:55555/wf/create?src=["Assets/Library/test.txt"]</a>
 56  * @class Creates new folders and returns the paths to them.
 57  * @name Create
 58  * @param src Path or paths of the new folders.
 59  * @returns ( {'AssetID': "path",...} )
 60  */
 61 function main()
 62 {
 63 	var aParameters = REST.getParametersToIterate("src");
 64 	if (myData.error != null)
 65 	{
 66 		return REST.formatResponse();
 67 	}
 68 	REST.iterateThroughParameters(aParameters, create);
 69 	return REST.formatResponse();
 70 }
 71 main();