public static Http.Operation DownLoadBean(string _uuid, OnSuccessFinishDelegate _onFinish, OnErrorDelegate _onError) { string uri = Settings.fangs_domain + "/storage/pullstring"; Log.Debug("HttpMgr", "DownloadBean: {0}", uri); Dictionary <string, object> paramAry = new Dictionary <string, object>(0); paramAry.Add("bucket", HttpMgr.Base64Encode(Settings.beans_bucket)); paramAry.Add("key", HttpMgr.Base64Encode(_uuid)); Http.Operation operation = http.AsyncPOST(uri, paramAry, (_data) => { Log.Debug("HttpMgr", "DownloadBean Finish: {0}", uri); JSONNode json = JSON.Parse(System.Text.Encoding.UTF8.GetString(_data)); string bean = HttpMgr.Base64Decode(json["data"]["value"].Value); File.WriteAllText(Path.Combine(beansPath, _uuid + ".json"), bean); _onFinish(); }, (_err) => { Log.Error("HttpMgr", _err); _onError(_err.errmessage); }); return(operation); }
public static void DownloadManifest(OnSuccessFinishDelegate _onFinish, OnErrorDelegate _onError) { string uri = Settings.fangs_domain + "/storage/list"; Log.Debug("HttpMgr", "DownloadMinifest: {0}", uri); Dictionary <string, object> paramAry = new Dictionary <string, object>(0); paramAry.Add("bucket", HttpMgr.Base64Encode(Settings.beans_bucket)); paramAry.Add("sort", HttpMgr.Base64Encode("time")); paramAry.Add("from", 0); paramAry.Add("to", 32); Http.Operation operation = http.AsyncPOST(uri, paramAry, (_data) => { Log.Debug("HttpMgr", "DownloadMinifest Finish: {0}", uri); // parse the response JSONNode json = JSON.Parse(System.Text.Encoding.UTF8.GetString(_data)); int errcode = json["errcode"].AsInt; // 0 is OK if (0 != errcode) { _onError(string.Format("Download manifest has error, the errorcode is {0}", errcode)); return; } // take all the guid of bean and put them to a array. int count = json["data"]["keys:length"].AsInt; string[] beans = new string[count]; for (int i = 0; i < count; ++i) { string field = string.Format("keys:{0}", i); string value = json["data"][field].Value; beans[i] = Base64Decode(value); } // save the minifest file with beans JSONArray beanAry = new JSONArray(); foreach (string bean in beans) { beanAry.Add(bean); } JSONClass root = new JSONClass(); root.Add("beans", beanAry); string manifestJson = root.ToJSON(0); File.WriteAllText(Path.Combine(beansPath, "manifest.txt"), manifestJson); _onFinish(); }, (_err) => { Log.Error("HttpMgr", _err); _onError(_err.errmessage); }); }
public static void BatchDownloadRes(List <string> _list, OnSuccessFinishDelegate _onFinish) { operations.Clear(); List <string> tasks = new List <string>(); foreach (string task in _list) { string file = Path.GetFileName(task); // All files will store in the persistentDataPath ResourceMgr.UseExternalBundle(file); if (!File.Exists(resPath + file)) { tasks.Add(task); } } Log.Info("HttpMgr", "BatchDownload [{0}] tasks", tasks.Count); if (0 == tasks.Count) { _onFinish(); return; } int count = 0; foreach (string task in tasks) { string file = Path.GetFileName(task); Log.Debug("HttpMgr", "Async Get: {0}", task); Http.Operation operation = http.AsyncGET(task, (_data) => { Log.Debug("HttpMgr", "Get Finish: {0}", task); System.IO.File.WriteAllBytes(resPath + file, _data); onFinish(ref count, tasks.Count, _onFinish); }, (_err) => { Log.Error("HttpMgr", _err); }); operations.Add(task, operation); } }