Пример #1
0
        public async Task DownLoad_File_Command_Handler()
        {
            //Arrange
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync",
                                                 ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent("{'name':dummy&,'city':'Lisbon'}"),
            });

            var client = new HttpClient(mockHttpMessageHandler.Object);

            _httpClientFactory.Setup(x => x
                                     .CreateClient(It.IsAny <string>())).Returns(client);
            _command = new FileDownloadCommand()
            {
                Id = "00000ZZZZZXXX"
            };
            _handler = new FileDownloadHandler(_httpClientFactory.Object);

            //Act
            var result = await _handler.Handle(_command, new CancellationToken());

            //Assert
            Assert.NotNull(result);
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
        }
Пример #2
0
        public void AddFileDownload(FileDownloadCommand model, FileTransferCompletionStatus status)
        {
            string message = "";

            if (status == FileTransferCompletionStatus.Successful)
            {
                message = string.Format("Successfully download file '{0}' from user {1}", model.FileName, model.SourceUserName);
            }
            else if (status == FileTransferCompletionStatus.Error)
            {
                message = string.Format("An error occurred downloading file '{0}' from user {1}", model.FileName, model.SourceUserName);
            }
            else if (status == FileTransferCompletionStatus.Cancelled)
            {
                message = string.Format("Download of file '{0}' from user {1} was cancelled", model.FileName, model.SourceUserName);
            }
            else
            {
                return;
            }

            var pnl = AddSectionPanel(40);

            pnlMessages.ScrollControlIntoView(pnl);
            _prevControl = pnl;

            Label lbl = new Label();

            lbl.Text = message;
            pnl.Controls.Add(lbl);
            lbl.AutoSize = true;
            lbl.Location = new Point(5, 5);
        }
Пример #3
0
        private async Task ExecuteConsoleCommandFileDownload(Options options)
        {
            var fileDownloadCommand = new FileDownloadCommand(options);
            await _mediator.Send(fileDownloadCommand);

            LogInformationMessage($"File \"{fileDownloadCommand.FilePath}\" has been downloaded to {fileDownloadCommand.DestinationPath}");
            _consolePrinter.PrintFileDownloadedSuccessful(fileDownloadCommand.FilePath);
        }
Пример #4
0
        private void NotifyFileDownloadStatus(FileDownloadCommand model, FileTransferCompletionStatus status)
        {
            var messageCtrl = GetMessageHistoryCtrlForUser(model.SourceUserName);

            if (messageCtrl == null)
            {
                return;
            }

            messageCtrl.AddFileDownload(model, status);
        }
Пример #5
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var id = new FileDownloadCommand
            {
                Id = "1AJ7icRJ5dfbWlQORocfrLhVyMOd242sm"
            };

            var result = await _mediator.Send(id, cancellationToken);

            await _file.Extract(await result.Content.ReadAsByteArrayAsync());

            var collections = new CollectionCommand()
            {
                AlbumCollection = _fileHelper.ParseFile()
            };

            await _mediator.Send(collections, cancellationToken);
        }
        public async Task DownloadFilePart(FileDownloadCommand downloadCommand, string filePartPath, int filePartIndex)
        {
            try
            {
                if (string.IsNullOrEmpty(_accessToken))
                {
                    throw new Exception("Not logged in");
                }

                using (var client = new HttpClient())
                {
                    var url = string.Format("{0}/api/file/download/{1}/{2}", _configuration.ServerBaseUrl, downloadCommand.FileId, filePartIndex);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
                    var response = await client.GetAsync(url);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new WebAPIException("An error occurred downloading file", response.ReasonPhrase, response.StatusCode);
                    }

                    using (Stream contentStream = await response.Content.ReadAsStreamAsync())
                    {
                        using (var stream = new FileStream(filePartPath, FileMode.Create, FileAccess.Write, FileShare.None, 10000, true))
                        {
                            contentStream.CopyTo(stream);
                            stream.Flush();
                            stream.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is WebAPIException)
                {
                    throw;
                }
                throw new Exception("An error occurred downloading file. " + ex.Message);
            }
        }