1 var REST = library.REST.REST();
  2 
  3 /**
  4  * The user can pass in a directoryId as the root of whatever they are creating
  5  * @returns {*}
  6  */
  7 function getParentDirectory() {
  8   var aDirId = context.getParameter("directoryId");
  9   if (!!aDirId) {
 10     var aParentFolder = fileManager.getFolderObjectById(aDirId);
 11     if (!aParentFolder) {
 12       REST.pushError(
 13         REST.errors.e404,
 14         "The folder does not exist by id: " + aDirId
 15       );
 16       return;
 17     }
 18     return aParentFolder;
 19   }
 20   return;
 21 }
 22 
 23 function createFolder(thePath) {
 24   if (!context.getUser().isPermissionEnabled("folderAction")) {
 25     REST.pushError(REST.errors.e403, "folderAction permission is not enabled");
 26     return;
 27   }
 28   if (fileManager.isFolder(thePath)) {
 29     REST.pushError(REST.errors.e409, "Folder already exists: " + thePath);
 30     REST.push(new ManagedFolder(thePath));
 31   } else {
 32     var aCreatedFolder = fileManager.folderNewByPath(thePath);
 33     REST.push(aCreatedFolder);
 34   }
 35 }
 36 
 37 function createFile(thePath) {
 38   if (
 39     !context.getUser().isPermissionEnabled("newAssets") ||
 40     !context.getUser().isPermissionEnabled("fileAction")
 41   ) {
 42     REST.pushError(
 43       REST.errors.e403,
 44       "newAssets and fileAction permissions are not enabled"
 45     );
 46     return;
 47   }
 48   if (fileManager.isFile(thePath)) {
 49     REST.pushError(REST.errors.e409, "File already exists: " + thePath);
 50     REST.push(new ManagedFile(thePath));
 51   } else {
 52     var aNewFile = fileManager.fileNewByPath(thePath);
 53     REST.push(aNewFile);
 54   }
 55 }
 56 
 57 /**
 58  * Creates a file or folder from the path
 59  * @param thePath
 60  */
 61 function create(thePath) {
 62   var aParent = getParentDirectory();
 63   if (REST.getStopOnFirstError() && REST.hasErrors()) {
 64     return;
 65   }
 66   if (thePath[thePath.length - 1] !== "/") {
 67     createFile(!aParent ? REST.getFullPath(thePath) : aParent.path + thePath);
 68   } else {
 69     createFolder(!aParent ? REST.getFullPath(thePath) : aParent.path + thePath);
 70   }
 71 }
 72 
 73 /**
 74  * @name Create
 75  * @class Creates empty files and folders
 76  * @description  For every path "paths", this endpoint creates an empty file or folder.
 77  * @param paths An array of paths to create
 78  * [
 79  *  "destination/path/NewDirectory/",
 80  *  "destination/path/NewFile.txt"
 81  * ]
 82  * Note: This call will recursively create any missing directories.
 83  * @param directoryId anId of the root directory to create the paths in.
 84  * @param [verbose=false] Setting this to true will collect a variety of default values for each asset.
 85  * @param [fields] An array of field id's to collect the values for each asset
 86  * @returns [{theCreatedItemsInfo}, ... ]
 87  *
 88  * @example /wf/restapi/v2/create
 89  *
 90  * Parameters:
 91  * paths=["destination/path/NewDirectory/","destination/path/NewFile.txt"]
 92  *
 93  Response:
 94  [
 95 	 {
 96 	   "path": "destination/path/NewDirectory/"
 97 	 },
 98 	 {
 99 	   "id": 201629298
100 	 }
101  ]
102  * @example /wf/restapi/v2/create
103  *
104  * Parameters:
105  * paths=["destination/path/NewDirectory/","destination/path/NewFile.txt"]
106  * verbose=true
107  *
108  Response:
109  [
110 	 {
111 	   "path": "destination/path/NewDirectory/",
112 	   "name": "NewDirectory",
113 	   "resolver": "directory://247"
114 	 },
115 	 {
116 	   "id": 201629299,
117 	   "name": "NewFile.txt",
118 	   "path": "destination/path/NewFile.txt",
119 	   "height": 0,
120 	   "width": 0,
121 	   "bytes": 0,
122 	   "lastModified": 1507833926000,
123 	   "mimeType": "text/plain",
124 	   "previews": {
125 		 "thumbnail": "../servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363239323939",
126 		 "viewex": "../servlet/jb.view?table=viewex&col=viewex&id=pe_323031363239323939",
127 		 "downloadUrl": "../servlet/dload?id=pe_323031363239323939"
128 	   }
129 	 }
130  ]
131 
132  * @example /wf/restapi/v2/create
133  * In this example 123 is the directory id of the testFolder/ directory
134  *
135  * Parameters:
136  * paths=["destination/path/NewDirectory/","destination/path/NewFile.txt"]
137  * directoryId=123
138  * verbose=true
139  *
140  Response:
141  [
142 	 {
143 	    "path": "testFolder/destination/path/NewDirectory/",
144 	    "id": 404,
145 	    "parentId": 403,
146 	    "name": "NewDirectory",
147 	    "resolver": "directory://404"
148 	  },
149 	 {
150 	    "id": 202424640,
151 	    "name": "NewFile.txt",
152 	    "path": "testFolder/destination/path/NewFile.txt",
153 	    "directoryId": 403,
154 	    "height": 0,
155 	    "width": 0,
156 	    "bytes": 0,
157 	    "lastModified": 1551809299000,
158 	    "mimeType": "text/plain",
159 	    "previews": {
160 	      "thumbnail": "https://luke.brightech.com:55555/servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323032343234363430",
161 	      "viewex": "https://luke.brightech.com:55555/servlet/jb.view?table=viewex&col=viewex&id=pe_323032343234363430",
162 	      "downloadUrl": "https://luke.brightech.com:55555/servlet/dload?id=pe_323032343234363430"
163 	    }
164 	  }
165  ]
166  */
167 function main() {
168   REST.setCallback(create);
169   REST.setStopOnFirstError(false);
170   return REST.execute("paths");
171 }
172 main();
173