1 /**
  2  * Workflow to compress files using a REST call
  3  */
  4 var REST = library.REST.REST();
  5 var myData = {};
  6 var myDest = "";
  7 var myKeepDirectoryStructure;
  8 
  9 /**
 10  * Compresses a list of assets given their ID's and adds an object to myData containing the path to the compressed file
 11  * @param theSource - list of asset Ids
 12  * @returns {*}
 13  */
 14 function compressFilesInternal(theSource) {
 15   var aDestination;
 16   var aCreatedFolder = false;
 17 
 18   //create default destination folder else see if given folder exists
 19   if (myDest == null || myDest == "") {
 20     try {
 21       aDestination = new ManagedFolder("Compressed");
 22     } catch (err) {
 23       var aIndex = new ManagedFolder("/");
 24       aDestination = fileManager.folderNew(aIndex, "Compressed");
 25       aCreatedFolder = true;
 26     }
 27   } else {
 28     try {
 29       aDestination = new ManagedFolder(myDest);
 30     } catch (err) {
 31       REST.submitError(myDest, "The file or folder does not exist");
 32       return JSON.stringify(myData, null, 2);
 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 < theSource.length; i++) {
 40     var anId = theSource[i];
 41     aTempFile = fileManager.getFileObjectById(anId);
 42     if (aTempFile == null) {
 43       if ((aCreatedFolder = true)) {
 44         fileManager.folderDelete(aDestination);
 45       }
 46       REST.submitError(anId, "The asset is not a file");
 47       return JSON.stringify(myData, null, 2);
 48     } else {
 49       aPathsList = aPathsList.concat([aTempFile.path]);
 50     }
 51   }
 52   var i = 0;
 53   var aNameOfCompressed = "mbfiles.zip";
 54   var aFileExist = fileManager.getFile(aDestination, aNameOfCompressed);
 55   while (aFileExist != null) {
 56     i++;
 57     aNameOfCompressed = "mbfiles" + i + ".zip";
 58     aFileExist = fileManager.getFile(aDestination, "mbfiles" + i + ".zip");
 59   }
 60 
 61   var aFile = fileManager.compressFiles(
 62     aPathsList,
 63     aNameOfCompressed,
 64     1,
 65     !myKeepDirectoryStructure
 66   );
 67   if (aFile == null) {
 68     REST.submitError(theSource, "Failed to compress");
 69     return;
 70   }
 71   aFile = fileManager.fileImport(aFile);
 72 
 73   aFile = fileManager.fileMove(aFile, aDestination);
 74   myData[aFile.assetId] = aFile.path;
 75 }
 76 
 77 /**
 78  * Compresses a list of assets based on assetIDs and returns their paths.
 79  * @description  Compresses files corresponding to the list of assetIDs in 'src' and returns paths to the compressed
 80  * files. The path values are parsed into a text object keyed by AssetID from the JSON file in which they are
 81  * contained, and by default are displayed to the user.
 82  * @example 'MBurl'/wf/restapi/1/compressFiles?src=["AssetID"]&dest=["PathToDestination"]
 83  * @example <a target="_blank" href=http://127.0.0.1:55555/wf/restapi/1/compressFiles?src=["12345"]&dest=["Assets/Library/"]>http://127.0.0.1:55555/wf/restapi/1/compressFiles?src=["12345"]&dest=["Assets/Library/"]</a>
 84  * @class Compresses a list of assets based on assetID's and returns their paths.
 85  * @name CompressFiles
 86  * @param src AssetID/list of assetIDs to get the paths from and to compress.
 87  * @param [keepDirectoryStructure=false] if true the zipped assets will maintain the folder structure of the assets in MediaBeacon
 88  * @param dest where to place compressed file. if blank default is /Compressed/
 89  * @returns ( {'AssetID':  "path",...} )
 90  */
 91 function main() {
 92   var aSrc = REST.getParameter("src", true);
 93   myKeepDirectoryStructure = !!context.getParameter("keepDirectoryStructure");
 94   myDest = REST.getParameter("dest", false);
 95   if (myData.error != null) {
 96     return REST.formatResponse();
 97   }
 98   compressFilesInternal(aSrc);
 99   return REST.formatResponse();
100 }
101 main();
102