private async void UploadPhotoAttachment(PhotoAttachmentUpload attachment) { attachment.IsUploading = true; _uploadsEvent.Reset(); try { var uploadServer = await ServiceLocator.Vkontakte.Photos.GetMessagesUploadServer(); attachment.Stream.Seek(0, SeekOrigin.Begin); if (attachment.CancellationToken.IsCancellationRequested) { return; } var uploadResponse = await ServiceLocator.Vkontakte.Photos.UploadPhoto(uploadServer, attachment.FileName, attachment.Stream); if (attachment.CancellationToken.IsCancellationRequested) { return; } if (uploadResponse != null) { var photo = await ServiceLocator.Vkontakte.Photos.SaveMessagePhoto(uploadResponse.Server, uploadResponse.Photo, uploadResponse.Hash); if (attachment.CancellationToken.IsCancellationRequested) { return; } if (photo != null) { attachment.VkPhoto = photo; attachment.IsUploaded = true; } } } catch (VkAccessDeniedException) { Messenger.Default.Send(new LoginStateChangedMessage() { IsLoggedIn = false }); Navigator.Main.Navigate(typeof(LoginView), clearHistory: true); } catch (Exception ex) { Logger.Error(ex, "Unable to upload photo attachment"); } finally { attachment.IsUploading = false; UpdatePendingAttachments(); } }
public async void AttachPhotoFromFile(StorageFile file) { bool notify = AttachmentUploads.Count == 0; // Application now has read/write access to the picked file var photoAttachment = new PhotoAttachmentUpload(); var fileStream = await file.OpenReadAsync(); var stream = fileStream.CloneStream(); fileStream.Dispose(); photoAttachment.Stream = stream.AsStreamForRead(); photoAttachment.FileName = file.Name; var bi = new BitmapImage(); await bi.SetSourceAsync(stream); photoAttachment.Photo = bi; AttachmentUploads.Add(photoAttachment); UploadPhotoAttachment(photoAttachment); if (notify) { RaisePropertyChanged("AttachmentUploads"); } }
public async void AttachPhotoFromStream(IRandomAccessStream imageStream, string contentType) { bool notify = AttachmentUploads.Count == 0; // Application now has read/write access to the picked file var photoAttachment = new PhotoAttachmentUpload(); photoAttachment.Stream = imageStream.AsStreamForRead(); photoAttachment.FileName = "Image." + contentType.Substring(contentType.IndexOf("/") + 1); var bi = new BitmapImage(); await bi.SetSourceAsync(imageStream); photoAttachment.Photo = bi; AttachmentUploads.Add(photoAttachment); UploadPhotoAttachment(photoAttachment); if (notify) { RaisePropertyChanged("AttachmentUploads"); } }