1 var REST = library.REST.REST(); 2 3 function downloadFile(theAssetIds, theKeepDirectoryStructure, theNoZip) { 4 var aFileName = "MediaBeaconFiles.zip"; 5 if (theAssetIds.length === 1) { 6 aFileName = fileManager.getFileObjectById(theAssetIds[0]).name; 7 if (!theNoZip) 8 { 9 aFileName += ".zip"; 10 } 11 logger.warning("downloadFile: noZip: " + theNoZip); 12 } 13 try { 14 var aDloadUrl = null; 15 if (theAssetIds.length === 1 && theNoZip) { 16 var anAsset = fileManager.getFileObjectById(theAssetIds[0]); 17 logger.warning("found: " + anAsset); 18 var anAssetId = (anAsset !== null) ? anAsset.encodedAssetId : theAssetIds[0]; 19 aDloadUrl = context.getServerURL() + 20 "/servlet/dload/" + 21 aFileName.replace(/ /g, "%20") + 22 "?id=" + 23 anAssetId; 24 } else { 25 aDloadUrl = context.getServerURL() + 26 "/servlet/d/" + 27 aFileName + 28 "?mime=application/zip&type=zip&r=asset://" + 29 theAssetIds.join(",") + 30 "&IP=false" + 31 "&forceFlatten=" + 32 !theKeepDirectoryStructure; 33 } 34 logger.warning("location: " + aDloadUrl); 35 return "<header>status:302\nLocation:" + aDloadUrl + "</header>\n"; 36 } catch (anError) { 37 REST.pushError( 38 REST.errors.e500, 39 "Failed to download compressed file: " + theCompressedFile.path, 40 anError 41 ); 42 } 43 } 44 45 /** 46 * @name Download 47 * @class Downloads assets 48 * @description This endpoint compresses the assets into a zip file and returns a 302 Redirect response to the downloaded file 49 * @param ids The asset id's to zip and download 50 * @param [keepDirectoryStructure=false] if true the zipped assets will maintain the folder structure of the assets in MediaBeacon 51 * @param [noZip=false] if true and a single asset is specified it will be downloaded directly without being zipped. 52 * @returns 302 Redirect to the download file 53 * 54 * @example /wf/restapi/v2/download 55 * 56 * Parameters: 57 * ids=[201624269,201624270] 58 * 59 * Response: 60 * 302 Redirect to the download URL 61 * 62 * @returns {*} 63 */ 64 function main() { 65 var myKeepDirectoryStructure = !!context.getParameter( 66 "keepDirectoryStructure" 67 ); 68 var aNoZip = !!context.getParameter( 69 "noZip" 70 ); 71 try { 72 var anOriginalAssetIds = context.getParameter("ids"); 73 anOriginalAssetIds = Array.isArray(anOriginalAssetIds) 74 ? anOriginalAssetIds 75 : [anOriginalAssetIds]; 76 for (var i in anOriginalAssetIds) { 77 if (!fileManager.getFileObjectById(anOriginalAssetIds[i])) { 78 REST.pushError( 79 REST.errors.e404, 80 "Failed to find file:" + anOriginalAssetIds[i] 81 ); 82 return REST.execute(); 83 } 84 } 85 return downloadFile(anOriginalAssetIds, myKeepDirectoryStructure, aNoZip); 86 } catch (anE) { 87 REST.pushError(REST.errors.e500, "Failed to download assets", anE); 88 } 89 90 //Run execute without a callback so we return the formatted errors 91 return REST.execute(); 92 } 93 main(); 94