/// <summary> /// 获取二维码图片 /// </summary> /// <param name="Id">个人或者群组编号</param> /// <param name="userOrgroup">1:个人;2:群</param> /// <returns></returns> public static string GetQrCodeImg(string Id, string userOrgroup) { if (string.IsNullOrEmpty(SDKClient.Instance.property.CurrentAccount.token)) { var res = WebAPICallBack.GetQrCodeImg(); SDKClient.Instance.property.CurrentAccount.token = res.token; logger.Error($"获取token:token:{res.token}"); } //获取二维码 var server = Util.Tools.QrCode.QrCodeFactory.Create(SDKClient.Instance.property.CurrentAccount.qrCodePath); var response = WebAPICallBack.GetQrCode(Id, userOrgroup); if (response.success) { return(server.Size(Util.Tools.QrCode.QrSize.Middle).Save(response.qrCode)); } else { logger.Error($"获取二维码错误:imei:{SDKClient.Instance.property.CurrentAccount.imei}," + $"token:{SDKClient.Instance.property.CurrentAccount.token}," + $"signature:{Util.Helpers.Encrypt.Md5By32(SDKClient.Instance.property.CurrentAccount.lastlastLoginTime.Value.Ticks + ProtocolBase.ImLinkSignUri)}," + $"timeStamp:{SDKClient.Instance.property.CurrentAccount.lastlastLoginTime.Value.Ticks},code:{response.code}"); logger.Error($"获取二维码错误:{response.error},code:{response.code}"); return(null); } }
/// <summary> /// 查找资源是否存在 /// </summary> /// <param name="resourceName">资源全路径</param> /// <returns></returns> public static async Task <fileInfo> IsFileExist(string resourceFullName) { try { //验证资源是否存在 // var filedata = File.ReadAllBytes(resourceFullName); // var name = Util.Helpers.Encrypt.Md5By32(filedata); using (FileStream fs = File.OpenRead(resourceFullName)) { long len = fs.Length; string md5 = Util.Helpers.Encrypt.Md5By32(fs); var file = await WebAPICallBack.FindResource($"{md5}{Path.GetExtension(resourceFullName)}"); if (file == null || !file.isExist) { file.fileSize = len; file.fileCode = $"{md5}{Path.GetExtension(resourceFullName)}"; } return(file); } } catch (Exception ex) { logger.Error(ex.Message); return(null); } }
/// <summary> /// 查找资源是否存在 /// </summary> /// <param name="resourceName">资源全路径</param> /// <returns></returns> public static async Task <(bool existed, string resourceId, long fileSize)> FindResource(string resourceFullName) { try { //验证资源是否存在 // var filedata = File.ReadAllBytes(resourceFullName); // var name = Util.Helpers.Encrypt.Md5By32(filedata); using (FileStream fs = File.OpenRead(resourceFullName)) { string md5 = Util.Helpers.Encrypt.Md5By32(fs); var t = await WebAPICallBack.FindResource($"{md5}{Path.GetExtension(resourceFullName)}"); if (t.isExist) { return(t.isExist, $"{md5}{Path.GetExtension(resourceFullName)}", fs.Length); } else { return(false, $"{md5}{Path.GetExtension(resourceFullName)}", fs.Length); } } } catch (Exception) { return(false, null, 0); } }
public static async void ResumeUpload(string resourceFullName, string md5, Action <long> UploadProgressChanged, Action <bool, string, SDKProperty.ErrorState> UploadDataCompleted, List <int> blockNum, System.Threading.CancellationToken?cancellationToken = null) { int blocklen = 1024 * 1024 * 2; using (FileStream fs = new FileStream(resourceFullName, FileMode.Open)) { long totalCount = fs.Length; long totalnum = totalCount / blocklen; if (totalCount % blocklen != 0) { totalnum += 1; } fs.Seek(0, SeekOrigin.Begin); SDKProperty.ErrorState errorState = ErrorState.None; for (int i = 1; i < (totalnum + 1); i++) { if (blockNum != null && blockNum.Contains(i)) { continue; } if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested) { errorState = ErrorState.Cancel; break; } fs.Seek((i - 1) * blocklen, SeekOrigin.Begin); byte[] buff = new byte[blocklen]; int len = fs.Read(buff, 0, buff.Length); if (len == blocklen) { await WebAPICallBack.ResumeUpload(buff, i, blocklen, md5, totalCount, totalnum).ContinueWith(async t => { if (t.IsFaulted)//服务不可用 { //TODO: // UploadDataCompleted?.Invoke(false, md5, ErrorState.NetworkException); errorState = errorState == ErrorState.Cancel ? errorState : ErrorState.NetworkException; } else { var obj = t.Result; if (!obj.Success) { if (obj.code == "-999") { errorState = errorState == ErrorState.Cancel ? errorState : ErrorState.NetworkException; return; } bool isOk = false; for (int j = 0; j < 5; j++) { var r = await WebAPICallBack.ResumeUpload(buff, i, blocklen, md5, totalCount, totalnum); isOk = r.Success; if (isOk) { break; } } if (!isOk) { errorState = errorState == ErrorState.Cancel ? errorState : ErrorState.NetworkException; } } else { UploadProgressChanged?.Invoke(len); } } }); } else { byte[] temp = new byte[len]; Buffer.BlockCopy(buff, 0, temp, 0, len); await WebAPICallBack.ResumeUpload(temp, i, len, md5, totalCount, totalnum).ContinueWith(async t => { if (t.IsFaulted) { //TODO: errorState = errorState == ErrorState.Cancel ? errorState : ErrorState.NetworkException; } else { var obj = t.Result; if (!obj.Success) { if (obj.code == "-999") { errorState = errorState == ErrorState.Cancel ? errorState : ErrorState.NetworkException; return; } bool isOk = false; for (int j = 0; j < 5; j++) { var r = await WebAPICallBack.ResumeUpload(buff, i, blocklen, md5, totalCount, totalnum); isOk = r.Success; if (isOk) { break; } } if (!isOk) { errorState = errorState == ErrorState.Cancel ? errorState : ErrorState.NetworkException; } } else { UploadProgressChanged?.Invoke(len); } } }); } } if (errorState != ErrorState.None) { UploadDataCompleted?.Invoke(false, md5, errorState); } else { UploadDataCompleted?.Invoke(true, md5, ErrorState.None); } } }