1 var REST = library.REST.REST(); 2 var myData = {}; 3 var myKeepDirectoryStructure = null; 4 5 function downloadFile(theAssetId) { 6 try { 7 var aZipName = "mb_files.zip"; 8 var aFileToDownload = fileManager.getFileObjectById(theAssetId); 9 if (aFileToDownload == null) { 10 REST.submitError(theAssetId, "File not found"); 11 return JSON.stringify(myData, null, 2); 12 } 13 var aDloadUrl = 14 context.getServerURL() + 15 "/servlet/d/" + 16 aZipName + 17 "?mime=application/zip&type=zip&r=asset://" + 18 aFileToDownload.assetId + 19 "&IP=false&" + 20 REST.getAuthParam(); 21 return "<header>Status:302\nLocation:" + aDloadUrl + "</header>\n"; 22 } catch (anError) { 23 REST.submitError(theAssetId, anError); 24 return REST.formatResponse(); 25 } 26 } 27 28 /** 29 * Compresses the assets and places the zip in Renditions/Workflows/_internal/restapi/tmp/ 30 * @param theSource array of asset ids 31 * @returns {*} 32 */ 33 function compressFiles(theSource) { 34 var aPathsList = []; 35 var aTempFile; 36 //convert each id in aSrc to a path in aPathsList 37 for (var i = 0; i < theSource.length; i++) { 38 var anId = theSource[i]; 39 aTempFile = fileManager.getFileObjectById(anId); 40 if (aTempFile == null) { 41 REST.submitError(anId, "The asset is not a file"); 42 return null; 43 } else { 44 aPathsList = aPathsList.concat([aTempFile.path]); 45 } 46 } 47 var i = 0; 48 var aNameOfCompressed = "restDload.zip"; 49 var aDestination = fileManager.folderNewByPath( 50 "Renditions/Workflows/_internal/restapi/tmp" 51 ); 52 while (fileManager.isFile(aDestination.path + aNameOfCompressed)) { 53 i++; 54 aNameOfCompressed = "restDload" + i + ".zip"; 55 } 56 var aFile = fileManager.compressFiles( 57 aPathsList, 58 aDestination.path + aNameOfCompressed, 59 1, 60 !myKeepDirectoryStructure 61 ); 62 if (aFile == null) { 63 REST.submitError("error", "Failed to compress"); 64 return null; 65 } 66 aFile = fileManager.fileImport(aFile); 67 return aFile; 68 } 69 70 /** 71 * Downloads the given assets zipped. 72 * @description Downloads an asset specified by AssetID 'src'. 73 * @example 'MBurl'/wf/restapi/1/download?src='AssetID' 74 * @example <a target="_blank" href=http://127.0.0.1:55555/wf/restapi/1/download?src=12345>http://127.0.0.1:55555/wf/restapi/1/download?src=12345</a> 75 * @class Downloads the given assets zipped. 76 * @name Download 77 * @param src AssetID or array of AssetIDs to download. 78 * @param [keepDirectoryStructure=false] if true the zipped assets will maintain the folder structure of the assets in MediaBeacon 79 * @returns ( The file zipped ) 80 */ 81 function main() { 82 var anId = REST.getParameter("src", true); 83 myKeepDirectoryStructure = !!context.getParameter("keepDirectoryStructure"); 84 if (myData.error != null) { 85 return REST.formatResponse(); 86 } 87 if (!(anId instanceof Array)) { 88 return downloadFile(anId); 89 } else { 90 var aZippedFile = compressFiles(anId); 91 if (aZippedFile == null) { 92 return REST.formatResponse(); 93 } 94 return downloadFile(aZippedFile.assetId); 95 } 96 } 97 main(); 98