1 var REST = library.REST.REST();
  2 var kS3Enabled = args("aws.defaultBucket", "") !== "";
  3 
  4 /**
  5  *  Decompresses the file into the destination
  6  *
  7  * @param theJSON {"id": 123456, "dest": "destination/path/"}
  8  */
  9 function decompressFiles(theJSON) {
 10   var aFile = fileManager.getFileObjectById(theJSON.id);
 11   var aDestination = theJSON.dest;
 12   if (aFile == null) {
 13     REST.pushError(REST.errors.e404, "The file does not exist: " + theJSON.id);
 14     return;
 15   }
 16   if (!aDestination) {
 17     aDestination = !aFile.parent.path ? "" : aFile.parent.path;
 18   } else {
 19     aDestination = REST.getFullPath(aDestination);
 20   }
 21 
 22   var aDestFolder = fileManager.folderNewByPath(aDestination);
 23   var aRootFolder = fileManager.decompressFiles(
 24     aFile,
 25     aDestFolder,
 26     "UTF-8",
 27     false,
 28     "",
 29     true
 30   );
 31   fileManager.fileDelete(aFile);
 32   if (fileManager.isFile(aRootFolder.path)) {
 33     processFile(aRootFolder, aDestFolder);
 34   } else {
 35     var anIterator = aRootFolder.getContents();
 36     while (anIterator.hasNext()) {
 37       var aFileOrFolder = anIterator.next();
 38       if (fileManager.isFile(aFileOrFolder.path)) {
 39         processFile(aFileOrFolder, aDestFolder, true);
 40       }
 41     }
 42   }
 43 }
 44 
 45 function processFile(theUploadedFile, theUploadPath, theShouldMove) {
 46   var anImportedFile = null;
 47   if (kS3Enabled) {
 48     var aAssetID = s3Manager.doS3Upload(
 49       context.getUser(),
 50       theUploadedFile,
 51       theUploadPath.path
 52     );
 53     anImportedFile = fileManager.getFileObjectById(aAssetID);
 54   } else {
 55     if (!!theShouldMove) {
 56       anImportedFile = fileManager.fileMove(
 57         theUploadedFile,
 58         new ManagedFolder(theUploadPath.path)
 59       );
 60     } else {
 61       anImportedFile = fileManager.fileImport(theUploadedFile);
 62     }
 63   }
 64   workflowManager.triggerAssetAdded(anImportedFile);
 65   REST.push(anImportedFile);
 66 }
 67 
 68 /**
 69  * @name DecompressFiles
 70  * @class Decompresses compressed files such as .zip files
 71  * @description  For every object in  "data", this endpoint decompresses the file into the destination folder and returns the decompressed content info
 72  *
 73  * @param data An array of object that define the assets to decompress and where to place contents. The default destination is in the same directory as the zip
 74  * [{"id": 123456, "dest": "destination/path/"}, ...]
 75  * @param [verbose=false] Setting this to true will collect a variety of default values for each asset.
 76  * @param [fields] An array of field id's to collect the values for each asset
 77  * @returns [{theDecompressedItemInfo}, ... ]
 78  *
 79  * @example unzip two files. 201629752 has one file (asset1.xmp) and 201629753 has two (asset1.xmp, asset2.xmp)
 80  * /wf/restapi/v2/decompressFiles
 81  *
 82  * Parameters:
 83  * data=[{"id":201629752,"dest":"decompressFiles/dest1/"},{"id":201629753,"dest":"decompressFiles/dest2/"}],"verbose":true}
 84  * verbose=true
 85  *
 86  Response:
 87  [
 88 	 {
 89 	   "id": 201629754,
 90 	   "name": "asset1.xmp",
 91 	   "path": "decompressFiles/dest1/asset1.xmp",
 92 	   "height": 1,
 93 	   "width": 1,
 94 	   "bytes": 4096,
 95 	   "lastModified": 1508284375000,
 96 	   "mimeType": "application/octet-stream",
 97 	   "previews": {
 98 		 "thumbnail": "../servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363239373534",
 99 		 "viewex": "../servlet/jb.view?table=viewex&col=viewex&id=pe_323031363239373534",
100 		 "downloadUrl": "../servlet/dload?id=pe_323031363239373534"
101 	   }
102 	 },
103 	 {
104 	   "name": "zip2",
105 	   "path": "decompressFiles/dest2/zip2/",
106 	   "resolver": "directory://309",
107 	   "assets": [
108 		 {
109 		   "id": 201629756,
110 		   "name": "asset2.xmp",
111 		   "path": "decompressFiles/dest2/zip2/asset2.xmp",
112 		   "height": 1,
113 		   "width": 1,
114 		   "bytes": 4096,
115 		   "lastModified": 1508284376000,
116 		   "mimeType": "application/octet-stream",
117 		   "previews": {
118 			 "thumbnail": "../servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363239373536",
119 			 "viewex": "../servlet/jb.view?table=viewex&col=viewex&id=pe_323031363239373536",
120 			 "downloadUrl": "../servlet/dload?id=pe_323031363239373536"
121 		   }
122 		 },
123 		 {
124 		   "id": 201629755,
125 		   "name": "asset1.xmp",
126 		   "path": "decompressFiles/dest2/zip2/asset1.xmp",
127 		   "height": 1,
128 		   "width": 1,
129 		   "bytes": 4096,
130 		   "lastModified": 1508284376000,
131 		   "mimeType": "application/octet-stream",
132 		   "previews": {
133 			 "thumbnail": "../servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363239373535",
134 			 "viewex": "../servlet/jb.view?table=viewex&col=viewex&id=pe_323031363239373535",
135 			 "downloadUrl": "../servlet/dload?id=pe_323031363239373535"
136 		   }
137 		 }
138 	   ]
139 	 }
140  ]
141 
142  */
143 function main() {
144   REST.setCallback(decompressFiles);
145   return REST.execute("data");
146 }
147 main();
148