Beispiel #1
0
        private async void UploadDocumentAttachment(DocumentAttachmentUpload attachment)
        {
            attachment.IsUploading = true;

            _uploadsEvent.Reset();

            try
            {
                var uploadServer = await ServiceLocator.Vkontakte.Documents.GetUploadServer();

                attachment.Stream.Seek(0, SeekOrigin.Begin);

                if (attachment.CancellationToken.IsCancellationRequested)
                {
                    return;
                }

                var uploadResponse = await ServiceLocator.Vkontakte.Documents.Upload(uploadServer, attachment.FileName, attachment.Stream);

                if (attachment.CancellationToken.IsCancellationRequested)
                {
                    return;
                }

                if (uploadResponse != null)
                {
                    var document = await ServiceLocator.Vkontakte.Documents.Save(uploadResponse.File);

                    if (attachment.CancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    if (document != null)
                    {
                        attachment.VkDocument = document;
                        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 document attachment");
            }
            finally
            {
                attachment.IsUploading = false;

                UpdatePendingAttachments();
            }
        }
Beispiel #2
0
        public async void AttachDocumentFromFile(StorageFile file)
        {
            bool notify = AttachmentUploads.Count == 0;

            var docAttachment = new DocumentAttachmentUpload();
            var fileStream    = await file.OpenReadAsync();

            var stream = fileStream.CloneStream();

            fileStream.Dispose();

            docAttachment.Stream   = stream.AsStreamForRead();
            docAttachment.FileName = file.Name;

            AttachmentUploads.Add(docAttachment);

            UploadDocumentAttachment(docAttachment);

            if (notify)
            {
                RaisePropertyChanged("AttachmentUploads");
            }
        }