text: function ensureDirectoryExistence(e){var r=path.dirname(e);return directoryExists(r)?!0:(ensureDirectoryExistence(r),void fs.mkdirSync(r))}function directoryExists(e){try{return fs.statSync(e).isDirectory()}catch(r){return!1}}function isBase64Code(e){return/^data:image\/.*;base64,/.test(e)}function saveBase64ToImgSync(e,r){if(!/^data:image\/.*;base64,/.test(r))throw Error("Bad base64 code format");var t=r.match(/^data:image\/(.*);base64,/)[1],i=r.replace(/^data:image\/.*;base64,/,""),s=e.lastIndexOf(".");return e=0>s?e+"."+t:e.substr(0,s+1)+t,fs.writeFileSync(e,i,"base64"),e}function visitObject(e,r){for(var t in e)e[t]instanceof Array?visitArray(e[t],r):"object"==typeof e[t]?visitObject(e[t],r):r(e,t)}function visitArray(e,r){e.forEach(function(t,i){t instanceof Array?visitArray(t,r):"object"==typeof t?visitObject(t,r):r(e,i)})}function visitFolderFiles(e,r){var t=fs.readdirSync(e);t.forEach(function(t){var i=path.normalize(e+"/"+t);fs.statSync(i).isDirectory()?r(i,t,!0)||visitFolderFiles(i,r):r(i,t,!1)})}function visitSubFolders(e,r){var t=fs.readdirSync(e);t.forEach(function(t){var i=path.normalize(e+"/"+t);fs.statSync(i).isDirectory()&&(r(i,t,fs.readdirSync(i))||visitSubFolders(i,r))})}function findMaxFileIndexInFolder(e,r){var t=fs.readdirSync(e),i=-1;return t.forEach(function(e){if(e.substring(0,e.lastIndexOf("."))===r)i=0;else if(e.startWith(r)){var t=e.substring((r+"_").length,e.lastIndexOf("."));t=parseInt(t,10),Number.isNaN(t)||(i=Math.max(i,t))}}),i}function getTokenFromRequest(e,r){var t=r.query.token;return t||(t=getTokenFromCookie(e,r)),t}function getTokenFromCookie(e,r){var t=r.cookies;if(!t)return null;for(var i in t)if("wab_auth"===i)try{var s=JSON.parse(t[i]),n=portalUrlUtils.getServerByUrl(s.server)===portalUrlUtils.getServerByUrl(e);if(!n)return null;s.expires=parseInt(s.expires,10);var a=new Date,o=a.getTime(),c=s.expires>o;if(!c)return null;var l=r.headers.host===s.referer;return l?s.token:null}catch(f){return logger.error(f),null}return null}function docopy(e,r,t){t?fs.existsSync(e)&&(logger.debug("copy",e),fse.copySync(e,r)):(logger.debug("copy",e),fse.copySync(e,r))}function digest(e){return crypto.createHash("sha1").update(e).digest("hex")}function checkFileExistInZip(e,r){var t=new JSZip(fs.readFileSync(e));for(var i in t.files){var s=t.files[i];if(s.name===r)return!0}return!1}function checkFolderExistInZip(e,r){var t=new JSZip(fs.readFileSync(e)),i=t.folder(new RegExp(r));return i&&i.length&&i.length>0?!0:!1}var path=require("path"),fs=require("fs"),fse=require("fs-extra"),JSZip=new require("jszip"),crypto=require("crypto"),Jimp=require("jimp"),log4js=require("log4js"),logger=log4js.getLogger("utils"),pako=require("pako"),requirejs=require("requirejs"),portalUrlUtils=requirejs("jimu/shared/basePortalUrlUtils");JSZip.compressions.DEFLATE.compress=function(e){return pako.deflateRaw(e)},exports.zipFolderSync=function(e,r){var t=new JSZip;e=path.normalize(e),visitFolderFiles(e,function(r,i,s){if(s)t.folder(r.substr(e.length,r.length));else{var n;n=fs.readFileSync(r),t.file(r.substr(e.length,r.length),n)}});var i=t.generate({type:"nodebuffer",compression:"DEFLATE"});fs.writeFileSync(r,i,"binary")},exports.unZipToFolderSync=function(e,r){var t=new JSZip(fs.readFileSync(e));for(var i in t.files){var s=t.files[i],n=path.join(r,s.name);ensureDirectoryExistence(n),!0===s.dir?fs.mkdirSync(n):fs.writeFileSync(n,new Buffer(s.asNodeBuffer()),"binary")}},exports.cropImage=function(e,r){var t=e.body.imageData;/^data:image\/.*;base64,/.test(t)||r.send({success:!1,message:"Bad base64 code format"});var i=t.match(/^data:image\/(.*);base64,/)[1],s=t.replace(/^data:image\/.*;base64,/,""),n=e.body.imageDisplaySize;n=n.split(",");var a=e.body.cropRectangle;a=a.split(",");try{var o=new Buffer(s,"base64");new Jimp(o,"image/"+i,function(){var e=parseInt(n[0],10),t=parseInt(n[1],10),s=parseInt(a[0],10),o=parseInt(a[1],10),c=parseInt(a[2],10),l=parseInt(a[3],10);try{this.resize(e,t).crop(l,c,s,o),this.getBuffer("image/"+i,function(e){var t="data:image/"+i+";base64,"+e.toString("base64");r.send({success:!0,imageData:t})})}catch(f){logger.error(f),r.send({success:!1,message:"unable to complete operations"})}})}catch(c){logger.error(c),r.send({success:!1,message:"unable to complete operations"})}},exports.visitFolderFiles=visitFolderFiles,exports.visitSubFolders=visitSubFolders,exports.visitObject=visitObject,exports.saveBase64ToImgSync=saveBase64ToImgSync,exports.isBase64Code=isBase64Code,exports.findMaxFileIndexInFolder=findMaxFileIndexInFolder,exports.getTokenFromRequest=getTokenFromRequest,exports.docopy=docopy,exports.digest=digest,exports.checkFileExistInZip=checkFileExistInZip,exports.checkFolderExistInZip=checkFolderExistInZip;