1 /**
  2  * Workflow to decompress files using a REST call
  3  */
  4 
  5 var myData = {};
  6 var REST = library.REST.REST();
  7 var kS3Enabled = args("aws.defaultBucket", "") !== "";
  8 
  9 /**
 10  *   decompresses the given asset and adds an object to myData with the path of the decompressed asset
 11  *
 12  * @param theSource
 13  */
 14 function decompressFiles(theSource, theDestination) {
 15   var aFile = fileManager.getFileObjectById(theSource);
 16   var aFolder;
 17   if (aFile == null) {
 18     REST.submitError(theSource, "The file id does not exist");
 19     return;
 20   }
 21 
 22   if (theDestination == null) {
 23     theDestination = aFile.parent.path;
 24   }
 25 
 26   var aFolderName = aFile.name.substr(0, aFile.name.indexOf(".zip"));
 27   var aTestName = aFolderName;
 28   var i = 0;
 29   while (fileManager.isFolder(theDestination + "/" + aTestName)) {
 30     aTestName = aFolderName + "_" + i;
 31     i++;
 32   }
 33 
 34   var aDestFolder = fileManager.folderNewByPath(
 35     theDestination + "/" + aTestName
 36   );
 37 
 38   if (aFile.fileNameExtension != "zip") {
 39     REST.submitError(
 40       theSource,
 41       "The file has extension " + aFile.fileNameExtension + "not .zip"
 42     );
 43     return;
 44   }
 45   var aRootFolder = fileManager.decompressFiles(
 46     aFile,
 47     aDestFolder,
 48     "UTF-8",
 49     false,
 50     "",
 51     true
 52   );
 53   fileManager.fileDelete(aFile);
 54   if (aRootFolder instanceof UnmanagedFile) {
 55     processFile(aRootFolder, aDestFolder, myData);
 56   } else {
 57     var anIterator = aRootFolder.getContents();
 58     while (anIterator.hasNext()) {
 59       var aFileOrFolder = anIterator.next();
 60       if (fileManager.isFile(aFileOrFolder.path)) {
 61         processFile(aFileOrFolder, aDestFolder, myData, true);
 62       }
 63     }
 64   }
 65 }
 66 
 67 function processFile(theUploadedFile, theUploadPath, theData, theShouldMove) {
 68   var anImportedFile = null;
 69   if (kS3Enabled) {
 70     var aAssetID = s3Manager.doS3Upload(
 71       context.getUser(),
 72       theUploadedFile,
 73       theUploadPath.path
 74     );
 75     anImportedFile = fileManager.getFileObjectById(aAssetID);
 76   } else {
 77     if (!!theShouldMove) {
 78       anImportedFile = fileManager.fileMove(
 79         theUploadedFile,
 80         new ManagedFolder(theUploadPath.path)
 81       );
 82     } else {
 83       anImportedFile = fileManager.fileImport(theUploadedFile);
 84     }
 85   }
 86   workflowManager.triggerAssetAdded(anImportedFile);
 87   theData[anImportedFile.assetId] = anImportedFile.path;
 88 }
 89 
 90 /**
 91  * Decompresses assets to a destination folder and returns their paths.
 92  * @description  Decompresses the assets listed by AssetID in 'src' into the specified 'dest' destination folder and
 93  * returns the path of the folder. This path value is parsed into a text object keyed by AssetID from the JSON file in
 94  * which path values are contained, and by default is displayed to the user.
 95  * @example 'MBurl'/wf/restapi/1/decompressFiles?src=["AssetId"]&dest="/path/to/destination/folder/"
 96  * @example <a target="_blank" href=http://127.0.0.1:55555/wf/restapi/1/decompressFiles?src=["12345"]&dest="Assets/Library/">http://127.0.0.1:55555/wf/restapi/1/decompressFiles?src=["12345"]&dest="Assets/Library/"</a>
 97  * @class Decompresses assets to a destination folder and returns their paths.
 98  * @name DecompressFiles
 99  * @param src AssetID/list of AssetIDs to decompress.
100  * @param dest Path to the destination folder where the src should be decompressed. If this parameter is not given,
101  * files are decompressed in the current directory.
102  * @returns ( {'AssetID': "path",...} )
103  */
104 function main() {
105   var aParameters = REST.getParametersToIterate(["src", "dest"]);
106   if (myData.error != null) {
107     return REST.formatResponse();
108   }
109   REST.iterateThroughParameters(aParameters, decompressFiles);
110   return REST.formatResponse();
111 }
112 main();
113