/// <summary> /// /// </summary> /// <returns>Request string to send attachments with API method messages.Send.</returns> private void _UploadCurrentPhotoAttachments() { if (App.Current.EntityService.AttachmentPhotos.Count == 0) _UploadCompleted(); else { //////////////////////////////////// GET UPLOAD SERVER if (string.IsNullOrEmpty(_uploadServer)) { var getUploadServer = new PhotosGetMessagesUploadServer(uploadServer => { _uploadServer = uploadServer; _UploadCurrentPhotoAttachments(); }); getUploadServer.Execute(); } else { foreach (var photo in App.Current.EntityService.AttachmentPhotos) { /////////////////////////// READ FILE DATA var data = new byte[photo.Value.Length]; photo.Value.Read(data, 0, (int)photo.Value.Length); if (data != null) { //////////////////////////////////// SEND DATA var client = new VKWebClient(); var handler = new ManualResetEvent(false); client.SendPhoto(_uploadServer, "photo", data, response => { try { SentPhoto sentPhoto = SerializeHelper.Deserialise<SentPhoto>(response); //////////////////////////////////// SAVE ATTACHMENT PHOTO PhotosSaveMessagesPhoto op = new PhotosSaveMessagesPhoto(sentPhoto.Server, sentPhoto.Photo, sentPhoto.Hash, att => { if (att != null) { //////////////////////////////////// REMEMBER ATTACHMENT ID _currentAttachmentsRequest += att.Id.ToString() + ","; } handler.Set(); }); op.Execute(); } catch (Exception ex) { _UploadCurrentPhotoAttachments(); _uploadServer = string.Empty; //handler.Set(); Debug.WriteLine("Parse response from _SavePhotoAttach failed." + ex.Message); } }); handler.WaitOne(); } } //_uploadServer = string.Empty; _UploadCompleted(); } } }
private void PhotoGallery_Completed(object sender, PhotoResult e) { try { if (e.ChosenPhoto != null) { GlobalIndicator.Instance.Text = AppResources.UploadingPhoto; GlobalIndicator.Instance.IsLoading = true; //////////////////////////////////// GET UPLOAD SERVER var getUploadServer = new PhotosGetProfileUploadServer(uploadServer => { if (!string.IsNullOrEmpty(uploadServer)) { /////////////////////////// READ FILE DATA var data = new byte[e.ChosenPhoto.Length]; e.ChosenPhoto.Read(data, 0, (int)e.ChosenPhoto.Length); if (data != null) { //////////////////////////////////// SEND DATA var client = new VKWebClient(); var handler = new ManualResetEvent(false); client.SendPhoto(uploadServer, "photo", data, response => { try { var sentPhoto = SerializeHelper.Deserialise<SentPhoto>(response); //////////////////////////////////// SAVE ATTACHMENT PHOTO var op = new PhotosSaveProfilePhoto(sentPhoto.Server, sentPhoto.Photo, sentPhoto.Hash, photo => { if (photo != null) { //////////////////////////////////// RELOAD AVATAR App.Current.EntityService.CurrentUser.UserInfo.Photo = photo.SourceSmall; App.Current.EntityService.CurrentUser.UserInfo.PhotoMedium = photo.Source; App.Current.EntityService.CurrentUser.UserInfo.PhotoBig = photo.SourceBig; App.Current.EntityService.LoadAvatars(); } else { MessageBox.Show(AppResources.UploadPhotoError); } handler.Set(); }); op.Execute(); } catch (Exception) { Debug.WriteLine("Parse response from PhotosSaveMessagesPhoto failed."); handler.Set(); } }); handler.WaitOne(); Dispatcher.BeginInvoke(() => { GlobalIndicator.Instance.Text = string.Empty; GlobalIndicator.Instance.IsLoading = false; }); } else { Dispatcher.BeginInvoke(() => { GlobalIndicator.Instance.Text = string.Empty; GlobalIndicator.Instance.IsLoading = false; }); } } }); getUploadServer.Execute(); } } catch (Exception ex) { GlobalIndicator.Instance.Text = string.Empty; GlobalIndicator.Instance.IsLoading = false; Debug.WriteLine("PhotoGallery_Completed failed and couched in UI! WTF! " + ex.Message); } }