1 /**
  2  * Workflow return the contents of a folder using a REST call
  3  */
  4 var REST = library.REST.REST();
  5 var kHierarchy = false;
  6 var kJustChildren = false;
  7 var kDepth;
  8 
  9 /**
 10  * Pushes directories to the response based upon the filters applied
 11  */
 12 function folderContents(thePath) {
 13   var aRootPath = REST.getFullPath(thePath);
 14   if (!fileManager.isManagedFolderPath(aRootPath) && aRootPath !== "/") {
 15     REST.pushError(REST.errors.e404, "Not a managed folder: " + thePath);
 16     return;
 17   }
 18   var aRootFolder = new ManagedFolder(aRootPath);
 19   var aRootFolderJSON = REST.buildFolderObject(aRootFolder);
 20   var aChildren = REST.getChildDirectories(
 21     aRootFolder,
 22     kDepth,
 23     kHierarchy,
 24     aRootFolderJSON
 25   );
 26 
 27   if (!kJustChildren) {
 28     REST.push(null, aRootFolderJSON);
 29   } else if (!!aRootFolderJSON.children) {
 30     for (var i in aRootFolderJSON.children) {
 31       REST.push(null, aRootFolderJSON.children[i]);
 32     }
 33   }
 34   if (!kHierarchy) {
 35     for (var i in aChildren) {
 36       var aChild = aChildren[i];
 37       REST.push(null, aChild);
 38     }
 39   }
 40 }
 41 
 42 /**
 43  * @name Directories
 44  * @class Shows the directory objects
 45  * @description Displays managed directories in MediaBeacon.
 46  * @param [verbose=false] Setting this to true will collect a variety of default values for each asset.
 47  * @param [hierarchy=false] Setting this to true will nest the directories in their parent directory object as a children array
 48  * @param [depth=0] The max depth of the children directories to fetch.
 49  * @param [paths] A directory path or an array of directory paths to collect.
 50  * @param [justChildren=false] When set to true, a directory with a path in the paths parameter won't be included in the response, just its children.
 51  * @returns [{"name":"theDirectoryName", "path": "theDirectoryPath", "resolver": "directoryResolver, "assets":[anArrayOfAssetsInfo]}, ... ]
 52  * @example /wf/restapi/v2/directories
 53  *
 54  * Parameters: none
 55  *
 56  Response:
 57  [
 58 	 {
 59 	    "path": "/",
 60 	    "id": 101,
 61 	    "hasChildren": true
 62 	  }
 63  ]
 64 
 65  * @example Verbose response
 66  * /wf/restapi/v2/directories
 67  *
 68  * Parameters:
 69  * verbose=true
 70  [
 71 	 {
 72 	    "path": "/",
 73 	    "id": 101,
 74 	    "name": "Index",
 75 	    "resolver": "directory://101",
 76 	    "hasChildren": true
 77 	  }
 78  ]
 79 
 80  * @example Increased Depth
 81  * /wf/restapi/v2/directories
 82  *
 83  * Parameters:
 84  * depth=2
 85  [
 86 	 {
 87 	    "path": "/",
 88 	    "id": 101
 89 	  },
 90 	 {
 91 	    "path": "upload/",
 92 	    "id": 139,
 93 	    "parentId": 101
 94 	  },
 95 	 {
 96 	    "path": "upload/root/",
 97 	    "id": 140,
 98 	    "parentId": 139,
 99 	    "hasChildren": true
100 	  },
101 	 {
102 	    "path": "Videos/",
103 	    "id": 161,
104 	    "parentId": 101
105 	  }
106  ]
107 
108  * @example Hierarchy enabled
109  * /wf/restapi/v2/directories
110  *
111  * Parameters:
112  * depth=2
113  * hierarchy=true
114  [
115 	 {
116 	    "path": "/",
117 	    "id": 101,
118 	    "children": [
119 	      {
120 	        "path": "upload/",
121 	        "id": 139,
122 	        "parentId": 101,
123 	        "children": [
124 	          {
125 	            "path": "upload/root/",
126 	            "id": 140,
127 	            "parentId": 139,
128 	            "hasChildren": true
129 	          }
130 	        ]
131 	      },
132 	      {
133 	        "path": "Videos/",
134 	        "id": 161,
135 	        "parentId": 101,
136 	        "children": []
137 	      }
138 	    ]
139 	  }
140  ]
141 
142  * @example Return specific directory by path
143  * /wf/restapi/v2/directories
144  *
145  * Parameters:
146  * depth=2
147  * hierarchy=true
148  * paths=upload/
149  [
150 	 {
151 	    "path": "upload/",
152 	    "id": 139,
153 	    "parentId": 101,
154 	    "children": [
155 	      {
156 	        "path": "upload/root/",
157 	        "id": 140,
158 	        "parentId": 139,
159 	        "children": [
160 	          {
161 	            "path": "upload/root/2018.11.06 11.37.49/",
162 	            "id": 141,
163 	            "parentId": 140
164 	          }
165 	        ]
166 	      }
167 	    ]
168 	  }
169  ]
170 
171  * @example Return multiple specific directories by path
172  * /wf/restapi/v2/directories
173  *
174  * Parameters:
175  * depth=2
176  * hierarchy=true
177  * paths=["upload/","Videos"]
178  [
179 	 {
180 	    "path": "upload/",
181 	    "id": 139,
182 	    "parentId": 101,
183 	    "children": [
184 	      {
185 	        "path": "upload/root/",
186 	        "id": 140,
187 	        "parentId": 139,
188 	        "children": [
189 	          {
190 	            "path": "upload/root/2018.11.06 11.37.49/",
191 	            "id": 141,
192 	            "parentId": 140
193 	          }
194 	        ]
195 	      }
196 	    ]
197 	  },
198 	 {
199 	    "path": "Videos/",
200 	    "id": 161,
201 	    "parentId": 101,
202 	    "children": []
203 	 }
204  ]
205 
206  * @example Fetch just the children directories
207  * /wf/restapi/v2/directories
208  *
209  * Parameters:
210  * depth=2
211  * hierarchy=true
212  * paths=upload/
213  [
214 	 {
215 	    "path": "upload/root/",
216 	    "id": 140,
217 	    "parentId": 139,
218 	    "children": [
219 	      {
220 	        "path": "upload/root/2018.11.06 11.37.49/",
221 	        "id": 141,
222 	        "parentId": 140
223 	      }
224 	    ]
225 	 }
226  ]
227 
228  */
229 (function main(theHierarchy, theDepth, thePaths, theJustChildren) {
230   kHierarchy = theHierarchy === true;
231   kJustChildren = theJustChildren === true;
232   kDepth = theDepth;
233   kDepth = kDepth !== undefined ? kDepth : 0;
234   if (thePaths === undefined) {
235     //Run on root directory by default if no paths were given
236     folderContents("/");
237     return REST.execute();
238   } else {
239     REST.setCallback(folderContents);
240     return REST.execute("paths");
241   }
242 })(
243   context.getParameter("hierarchy"),
244   context.getParameter("depth"),
245   context.getParameter("paths"),
246   context.getParameter("justChildren")
247 );
248