1 var REST = library.REST.REST();
  2 var myKeepDirectoryStructure;
  3 
  4 /**
  5  * Compresses the assets into the named zip file in the destination folder
  6  * @param theCompressionJSON {"ids": [123456,789012], "dest": "destination/path/", "name": "nameOfTheCompressed.zip"}
  7  */
  8 function compressFiles(theCompressionJSON) {
  9   var aJSONDest = theCompressionJSON.dest;
 10   var aJSONIds = theCompressionJSON.ids;
 11   var aDestination;
 12   var aDidCreateFolder = false;
 13 
 14   //create default destination folder else see if given folder exists
 15   if (aJSONDest == null || aJSONDest == "") {
 16     try {
 17       aDestination = new ManagedFolder("Compressed");
 18     } catch (err) {
 19       var aIndex = new ManagedFolder(REST.getACLRelativePath("/"));
 20       aDestination = fileManager.folderNew(aIndex, "Compressed");
 21       aDidCreateFolder = true;
 22     }
 23   } else {
 24     try {
 25       aDestination = new ManagedFolder(REST.getFullPath(aJSONDest));
 26     } catch (err) {
 27       REST.pushError(
 28         REST.errors.e404,
 29         "Directory does not exist: " + aJSONDest,
 30         err
 31       );
 32       return;
 33     }
 34   }
 35 
 36   var aPathsList = [];
 37   var aTempFile;
 38   //convert each id in aSrc to a path in aPathsList
 39   for (var i = 0; i < aJSONIds.length; i++) {
 40     var anId = aJSONIds[i];
 41     aTempFile = fileManager.getFileObjectById(anId);
 42     if (aTempFile == null) {
 43       if (aDidCreateFolder) {
 44         fileManager.folderDelete(aDestination);
 45       }
 46       REST.pushError(REST.errors.e404, "Asset doesn't exist: " + anId);
 47       return;
 48     } else {
 49       aPathsList = aPathsList.concat([aTempFile.path]);
 50     }
 51   }
 52   var i = 0;
 53   var aNameOfCompressed = !theCompressionJSON.name
 54     ? "mbfiles.zip"
 55     : theCompressionJSON.name;
 56   var aFileExist = fileManager.getFile(aDestination, aNameOfCompressed);
 57   var aBaseName = aNameOfCompressed.substring(
 58     0,
 59     aNameOfCompressed.lastIndexOf(".")
 60   );
 61   var anExt =
 62     !!aFileExist && aFileExist.name.indexOf(".") > 0
 63       ? aFileExist.fileNameExtension
 64       : "";
 65   while (aFileExist != null) {
 66     i++;
 67     aNameOfCompressed = aBaseName + " " + i + "." + anExt;
 68     aFileExist = fileManager.getFile(aDestination, aNameOfCompressed);
 69   }
 70 
 71   var aFile = fileManager.compressFiles(
 72     aPathsList,
 73     aNameOfCompressed,
 74     1,
 75     !myKeepDirectoryStructure
 76   );
 77   if (aFile == null) {
 78     REST.pushError(
 79       REST.errors.e500,
 80       "Failed to compress: " + JSON.stringify(aJSONIds)
 81     );
 82     return;
 83   }
 84   aFile = fileManager.fileImport(aFile);
 85   aFile = fileManager.fileMove(aFile, aDestination);
 86   REST.push(aFile, {});
 87 }
 88 
 89 /**
 90  * @class Compresses the given assets into a single zip asset
 91  * @description  For every object in "data", this endpoint compresses the assets into a zip file in the destination folder.
 92  * @name CompressFiles
 93  * @param data An array of objects [{"ids": [123456,789012], "dest": "destination/path/", "name": "nameOfTheCompressed.zip"}, ...]
 94  * @param [keepDirectoryStructure=false] if true the zipped assets will maintain the folder structure of the assets in MediaBeacon
 95  * @param [verbose=false] Setting this to true will collect a variety of default values for each asset.
 96  * @param [fields] An array of field id's to collect the values for each asset
 97  * @returns [{theCompressedFileInfo}, ... ]
 98  *
 99  * @example /wf/restapi/v2/compressFiles
100  *
101  * Parameters:
102  * data=[{"ids":[201629290,201629291],"dest":"destination/path/","name":"testZip.zip"}]
103  * verbose=true
104  *
105  Response:
106  [
107 	 {
108 	   "id": 201629292,
109 	   "name": "testZip.zip",
110 	   "path": "destination/path/testZip.zip",
111 	   "height": 0,
112 	   "width": 0,
113 	   "bytes": 8526,
114 	   "lastModified": 1507824864000,
115 	   "mimeType": "application/zip",
116 	   "previews": {
117 		 "thumbnail": "../servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363239323932",
118 		 "viewex": "../servlet/jb.view?table=viewex&col=viewex&id=pe_323031363239323932",
119 		 "downloadUrl": "../servlet/dload?id=pe_323031363239323932"
120 	   }
121 	 }
122  ]
123  * @example /wf/restapi/v2/compressFiles
124  *
125  * Parameters:
126  * data=[{"ids":[201629290,201629291],"dest":"destination/path/","name":"testZip1.zip"},{"ids":[201629290,201629291],"dest":"destination/path/","name":"testZip2.zip"}]
127  *
128  Response:
129  [
130 	 {
131 	   "id": 201629293
132 	   }
133 	 },
134 	 {
135 	   "id": 201629294
136 	 }
137  ]
138  */
139 function main() {
140   myKeepDirectoryStructure = !!context.getParameter("keepDirectoryStructure");
141   REST.setCallback(compressFiles);
142   return REST.execute("data");
143 }
144 main();
145