Esempio n. 1
0
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="fileData">包含图片信息的对象</param>
        /// <param name="imgKind">照片类型</param>
        /// <returns></returns>
        public async static Task<CommonRespMsg> UploadImageAsnyc(ButterFileToUpload fileData, UploadImageKind imgKind,CancellationToken ctoken)
        {
            var kindstr = "1:1";
            if (fileData.ScaleKind == ScaleKind.Scale_3x4) kindstr = "3:4";
            else if (fileData.ScaleKind == ScaleKind.Scale_4x3) kindstr = "4:3";

            CommonRespMsg result = new CommonRespMsg();
            try
            {
                using (var client = new HttpClient())
                {
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    dic.Add("uid", LocalSettingHelper.GetValue("uid"));
                    dic.Add("access_token", LocalSettingHelper.GetValue("access_token"));
                    dic.Add("appkey", UrlHelper.APP_KEY);
                    dic.Add("img_info", "{\"scale\":\"" + kindstr + "\"}");
                    dic.Add("ps", fileData.PsData);
                    dic.Add("is_private", fileData.IsPrivate ? "1" : "0");
                    if (fileData.ActivityID != null) dic.Add("activity_id", fileData.ActivityID);
                    if (fileData.OriginalImageID != -1) dic.Add("ding_imgid", fileData.OriginalImageID.ToString());

                    var resp = await client.PostAsync(new Uri(imgKind==UploadImageKind.Photo?UrlHelper.UploadImageUrl:UrlHelper.UploadAvatarUrl), new HttpFormUrlEncodedContent(dic));
                    var json = JsonObject.Parse(await resp.Content.ReadAsStringAsync());

                    var token = JsonParser.GetStringFromJsonObj(json, "token");
                    var key = JsonParser.GetStringFromJsonObj(json, "key");

                    Settings setting = new Settings();
                    ResumablePutExtra extra = new ResumablePutExtra();
                    ResumablePut rclient = new ResumablePut(setting, extra);
                    extra.Notify += ((sendern, en) =>
                      {

                      });

                    ctoken.ThrowIfCancellationRequested();
                   
                    var ioresult = await Task.Run(async () =>
                      {
                          ctoken.ThrowIfCancellationRequested();

                          return await rclient.PutFile(token, fileData.File.Path, key,ctoken);
                      });

                    if (!ioresult.OK) throw new APIException(int.Parse(ioresult.StatusCode.ToString()), ioresult.Exception.Message);
                    else
                    {
                        return result;
                    }
                }
            }
            catch (APIException)
            {
                result.IsSuccessful = false;
                return result;
            }
            catch(OperationCanceledException)
            {
                throw;
            }
            catch (Exception ee)
            {
                var task = ExceptionHelper.WriteRecord(ee, nameof(CloudServices), nameof(UploadImageAsnyc));
                var resultMsg = new CommonRespMsg()
                {
                    IsSuccessful = false,
                    ErrorMsg = ee.Message
                };
                return resultMsg;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="fileData">包含图片信息的对象</param>
        /// <param name="imgKind">照片类型</param>
        /// <returns></returns>
        public async static Task <CommonRespMsg> UploadImageAsnyc(ButterFileToUpload fileData, UploadImageKind imgKind, CancellationToken ctoken)
        {
            var kindstr = "1:1";

            if (fileData.ScaleKind == ScaleKind.Scale_3x4)
            {
                kindstr = "3:4";
            }
            else if (fileData.ScaleKind == ScaleKind.Scale_4x3)
            {
                kindstr = "4:3";
            }

            CommonRespMsg result = new CommonRespMsg();

            try
            {
                using (var client = new HttpClient())
                {
                    Dictionary <string, string> dic = new Dictionary <string, string>();
                    dic.Add("uid", LocalSettingHelper.GetValue("uid"));
                    dic.Add("access_token", LocalSettingHelper.GetValue("access_token"));
                    dic.Add("appkey", UrlHelper.APP_KEY);
                    dic.Add("img_info", "{\"scale\":\"" + kindstr + "\"}");
                    dic.Add("ps", fileData.PsData);
                    dic.Add("is_private", fileData.IsPrivate ? "1" : "0");
                    if (fileData.ActivityID != null)
                    {
                        dic.Add("activity_id", fileData.ActivityID);
                    }
                    if (fileData.OriginalImageID != -1)
                    {
                        dic.Add("ding_imgid", fileData.OriginalImageID.ToString());
                    }

                    var resp = await client.PostAsync(new Uri(imgKind == UploadImageKind.Photo?UrlHelper.UploadImageUrl:UrlHelper.UploadAvatarUrl), new HttpFormUrlEncodedContent(dic));

                    var json = JsonObject.Parse(await resp.Content.ReadAsStringAsync());

                    var token = JsonParser.GetStringFromJsonObj(json, "token");
                    var key   = JsonParser.GetStringFromJsonObj(json, "key");

                    Settings          setting = new Settings();
                    ResumablePutExtra extra   = new ResumablePutExtra();
                    ResumablePut      rclient = new ResumablePut(setting, extra);
                    extra.Notify += ((sendern, en) =>
                    {
                    });

                    ctoken.ThrowIfCancellationRequested();

                    var ioresult = await Task.Run(async() =>
                    {
                        ctoken.ThrowIfCancellationRequested();

                        return(await rclient.PutFile(token, fileData.File.Path, key, ctoken));
                    });

                    if (!ioresult.OK)
                    {
                        throw new APIException(int.Parse(ioresult.StatusCode.ToString()), ioresult.Exception.Message);
                    }
                    else
                    {
                        return(result);
                    }
                }
            }
            catch (APIException)
            {
                result.IsSuccessful = false;
                return(result);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception ee)
            {
                var task      = ExceptionHelper.WriteRecord(ee, nameof(CloudServices), nameof(UploadImageAsnyc));
                var resultMsg = new CommonRespMsg()
                {
                    IsSuccessful = false,
                    ErrorMsg     = ee.Message
                };
                return(resultMsg);
            }
        }