Exemple #1
0
 public void UploadPhotoToWall(long userOrGroupId, bool isGroup, byte[] photoData, Action <BackendResult <Photo, ResultCode> > callback, Action <double> progressCallback = null, Cancellation c = null)
 {
     this.GetPhotoUploadServerWall(userOrGroupId, isGroup, (Action <BackendResult <UploadServerAddress, ResultCode> >)(getUplResp =>
     {
         if (getUplResp.ResultCode != ResultCode.Succeeded)
         {
             callback(new BackendResult <Photo, ResultCode>(getUplResp.ResultCode));
         }
         else
         {
             JsonWebRequest.Upload(getUplResp.ResultData.upload_url, (Stream) new MemoryStream(photoData), "file1", "image", (Action <JsonResponseData>)(jsonResult =>
             {
                 if (!jsonResult.IsSucceeded)
                 {
                     callback(new BackendResult <Photo, ResultCode>(ResultCode.UnknownError));
                 }
                 else
                 {
                     this.SaveWallPhoto(userOrGroupId, isGroup, JsonConvert.DeserializeObject <UploadPhotoResponseData>(jsonResult.JsonString), callback);
                 }
             }), "MyImage.jpg", progressCallback, c);
         }
     }));
 }
Exemple #2
0
        public void UploadVideo(Stream stream, bool isPrivate, long albumId, long groupId, string name, string description, Action <BackendResult <SaveVideoResponse, ResultCode> > callback, Action <double> progressCallback = null, Cancellation c = null, PrivacyInfo privacyViewInfo = null, PrivacyInfo privacyCommentInfo = null)
        {
            Dictionary <string, string> parameters = new Dictionary <string, string>();

            parameters["is_private"] = isPrivate ? "1" : "0";
            if (groupId != 0L)
            {
                parameters["group_id"] = groupId.ToString();
            }
            if (albumId != 0L)
            {
                parameters["album_id"] = albumId.ToString();
            }
            if (!string.IsNullOrEmpty(name))
            {
                parameters["name"] = name;
            }
            if (!string.IsNullOrEmpty(description))
            {
                parameters["description"] = description;
            }
            if (privacyViewInfo != null && groupId == 0L)
            {
                parameters["privacy_view"] = privacyViewInfo.ToString();
            }
            if (privacyCommentInfo != null && groupId == 0L)
            {
                parameters["privacy_comment"] = privacyCommentInfo.ToString();
            }
            VKRequestsDispatcher.DispatchRequestToVK <SaveVideoResponse>("video.save", parameters, (Action <BackendResult <SaveVideoResponse, ResultCode> >)(res =>
            {
                if (res.ResultCode != ResultCode.Succeeded)
                {
                    callback(new BackendResult <SaveVideoResponse, ResultCode>(res.ResultCode));
                }
                else
                {
                    SaveVideoResponse svr = res.ResultData;
                    JsonWebRequest.Upload(svr.upload_url, stream, "video_file", "video", (Action <JsonResponseData>)(uploadRes =>
                    {
                        if (uploadRes.IsSucceeded)
                        {
                            callback(new BackendResult <SaveVideoResponse, ResultCode>(ResultCode.Succeeded, svr));
                        }
                        else
                        {
                            callback(new BackendResult <SaveVideoResponse, ResultCode>(ResultCode.UnknownError));
                        }
                    }), null, progressCallback, c);
                }
            }), null, false, true, new CancellationToken?(), null);
        }
Exemple #3
0
 public static void Download(string uri, Stream destinationStream, Action <bool> resultCallback, Action <double> progressCallback, Cancellation c)
 {
     if (string.IsNullOrWhiteSpace(uri))
     {
         resultCallback(false);
     }
     else
     {
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
         request.AllowReadStreamBuffering = false;
         request.BeginGetResponse((AsyncCallback)(asyncRes =>
         {
             bool flag = true;
             try
             {
                 HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncRes);
                 using (Stream responseStream = ((WebResponse)response).GetResponseStream())
                     StreamUtils.CopyStream(responseStream, destinationStream, progressCallback, c, response.ContentLength);
             }
             catch (Exception)
             {
                 flag = false;
             }
             resultCallback(flag);
         }), null);
     }
 }
 public void UploadAudio(Stream stream, string artist, string title, Action <BackendResult <AudioObj, ResultCode> > callback, Action <double> progressCallback = null, Cancellation cancellation = null)
 {
     VKRequestsDispatcher.DispatchRequestToVK <VKClient.Common.Backend.DataObjects.UploadServerAddress>("audio.getUploadServer", new Dictionary <string, string>(), (Action <BackendResult <VKClient.Common.Backend.DataObjects.UploadServerAddress, ResultCode> >)(result =>
     {
         if (result.ResultCode != ResultCode.Succeeded)
         {
             callback(new BackendResult <AudioObj, ResultCode>(result.ResultCode));
         }
         else
         {
             JsonWebRequest.Upload(result.ResultData.upload_url, stream, "file", "audio", (Action <JsonResponseData>)(uploadResult =>
             {
                 if (!uploadResult.IsSucceeded)
                 {
                     callback(new BackendResult <AudioObj, ResultCode>(ResultCode.UploadingFailed));
                 }
                 else
                 {
                     UploadResponseData uploadResponseData = JsonConvert.DeserializeObject <UploadResponseData>(uploadResult.JsonString);
                     VKRequestsDispatcher.DispatchRequestToVK <AudioObj>("audio.save", new Dictionary <string, string>()
                     {
                         {
                             "server",
                             uploadResponseData.server
                         },
                         {
                             "audio",
                             uploadResponseData.audio
                         },
                         {
                             "hash",
                             uploadResponseData.hash
                         },
                         {
                             "artist",
                             artist
                         },
                         {
                             "title",
                             title
                         }
                     }, callback, null, false, true, new CancellationToken?(), null);
                 }
             }), "track.mp3", progressCallback, cancellation);
         }
     }), (Func <string, VKClient.Common.Backend.DataObjects.UploadServerAddress>)null, false, true, new CancellationToken?(), null);
 }
Exemple #5
0
 public static void UploadVoiceMessage(string uri, Stream data, string paramName, string uploadContentType, List <int> waveform, Action <JsonResponseData> resultCallback, string fileName = null, Action <double> progressCallback = null, Cancellation c = null)
 {
     JsonWebRequest.RequestState rState = new JsonWebRequest.RequestState()
     {
         resultCallback = resultCallback
     };
     try
     {
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
         request.AllowWriteStreamBuffering = false;
         request.UserAgent = AppInfo.AppVersionForUserAgent;
         rState.request    = request;
         request.Method    = "POST";
         string str1 = string.Format("----------{0:N}", Guid.NewGuid());
         string str2 = "multipart/form-data; boundary=" + str1;
         request.ContentType     = str2;
         request.CookieContainer = new CookieContainer();
         string headerWaveform = "";
         if (waveform != null && waveform.Count > 0)
         {
             headerWaveform += string.Format("--{0}\r\nContent-Disposition: form-data; name=\"waveform\"\r\n\r\n", str1);
             headerWaveform += string.Format("[{0}]\r\n", string.Join <int>(",", (IEnumerable <int>)waveform));
         }
         string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n", str1, paramName, (fileName ?? "myDataFile"), uploadContentType);
         string footer = string.Format("\r\n--{0}--\r\n", str1);
         request.ContentLength = ((long)Encoding.UTF8.GetByteCount(header) + data.Length + (long)Encoding.UTF8.GetByteCount(footer));
         if (!string.IsNullOrEmpty(headerWaveform))
         {
             HttpWebRequest httpWebRequest = request;
             long           num            = httpWebRequest.ContentLength + (long)Encoding.UTF8.GetByteCount(headerWaveform);
             httpWebRequest.ContentLength = num;
         }
         request.BeginGetRequestStream((AsyncCallback)(ar =>
         {
             try
             {
                 Stream requestStream = request.EndGetRequestStream(ar);
                 if (!string.IsNullOrEmpty(headerWaveform))
                 {
                     requestStream.Write(Encoding.UTF8.GetBytes(headerWaveform), 0, Encoding.UTF8.GetByteCount(headerWaveform));
                 }
                 requestStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header));
                 StreamUtils.CopyStream(data, requestStream, progressCallback, c, 0L);
                 requestStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer));
                 requestStream.Close();
                 request.BeginGetResponse(new AsyncCallback(JsonWebRequest.RespCallback), rState);
             }
             catch (Exception ex)
             {
                 Logger.Instance.Error("Upload failed to write data to request stream.", ex);
                 JsonWebRequest.SafeClose(rState);
                 JsonWebRequest.SafeInvokeCallback(rState.resultCallback, false, null);
             }
         }), null);
     }
     catch (Exception ex)
     {
         Logger.Instance.Error("Upload failed.", ex);
         JsonWebRequest.SafeClose(rState);
         JsonWebRequest.SafeInvokeCallback(rState.resultCallback, false, null);
     }
 }