#! /usr/bin/env runjs var RFile = Packages.me.hatter.tools.commons.io.RFile; var REPO = 'https://repo.examp1e.org/'; var HPKP = 'YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg='; var main = () => { var libDir = $$.file('lib').exists()? $$.file('lib'): $$.file('libs'); if (!libDir.exists()) { xprintln('[ERROR] Dir lib or libs not exists!'); return; } println('- Load file: ' + REPO + 'files.js'); var filesJS = $STR($$.httpTool().hpkp(HPKP).url(REPO + 'files.js').read()); var filesJSON = JSON.parse(filesJS.substring(filesJS.indexOf('['))); var filesSha1Map = filesJSON.toMap((e) => { return [e[2], e[3]]; }); var filesSizeMap = filesJSON.toMap((e) => { return [e[2], e[0]]; }); var fileUpdated = []; $ARRAY(libDir.list()).forEach((f) => { var fn = $STR(f); var remoteFnSha1 = $STR(filesSha1Map.get(fn)); if (remoteFnSha1 == null) { println('- SKIP file: ' + f); return; } var fi = $$.file(libDir, f); var fiSha1 = $STR($$.digests().sha1().digest(RFile.from(fi).bytes()).asHex()); if (fiSha1 == remoteFnSha1) { println('- SHA1 check success: ' + f); } else { println('+ SHA1 check failed: ' + f); println('++ REMOTE:' + remoteFnSha1 + ' LOCAL: ' + fiSha1); println('++ Load file from: ' + REPO + f + ', size: ' + filesSizeMap.get(fn)); var counter = new Packages.me.hatter.tools.commons.io.DefaultRollCounter().prefix('++ Downloading: '); var fBytes = $$.httpTool().hpkp(HPKP).url(REPO + f).readBytes(counter); var downloadSha1 = $STR($$.digests().sha1().digest(fBytes).asHex()); if (downloadSha1 == remoteFnSha1) { fileUpdated.push(fn + ' - ' + remoteFnSha1); RFile.from(fi).write(fBytes); } else { println('+ ERROR! SHA1 mis-match, Should be: ' + remoteFnSha1 + ', Actural is: ' + downloadSha1); } } }); if (fileUpdated.length > 0) { println(); println('Total ' + fileUpdated.length + ' file(s) updated:'); fileUpdated.forEach((f) => { println('- ' + f); }); } }; main();