Пример #1
0
        public async Task StartUpload()
        {
            try
            {
                NotifyUploadStatus(FileUploadCommand, FileTransferCompletionStatus.Started);
                DateTime startTime         = DateTime.Now;
                int      numberOfFileParts = FileTransferHelper.GetNumberOfFileParts(FilePath, Program.FilePartSize);
                progress.Maximum = numberOfFileParts;

                int partIndex = 0;
                while (partIndex < numberOfFileParts)
                {
                    var data = FileTransferHelper.GetFilePartData(FilePath, partIndex, Program.FilePartSize);
                    await _communications.UploadFilePart(FileUploadCommand, data, partIndex);

                    partIndex++;
                    progress.Value++;
                }

                _logger.LogInformation(string.Format("File '{0}' uploaded successfully. Duration : {1}", FilePath, DateTime.Now.Subtract(startTime)));
                ShowFinishedStatus("Upload Completed", true);
                NotifyUploadStatus(FileUploadCommand, FileTransferCompletionStatus.Successful);
                return;
            }
            catch (Exception ex)
            {
                _logger.LogInformation(string.Format("An error occurred uploading file '{0}' (Id : {1}). Error : {2}", FilePath, FileUploadCommand.FileId, ex.Message));
                ShowFinishedStatus("Upload failed. " + ex.Message, false);
            }
            NotifyUploadStatus(FileUploadCommand, FileTransferCompletionStatus.Error);
        }
Пример #2
0
        private async void cmdSendFile_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboUser.SelectedItem == null)
                {
                    MessageBox.Show("A User must be selected", "Information Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (txtSendFilePath.Text == "")
                {
                    MessageBox.Show("A file must be specified", "Information Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (!File.Exists(txtSendFilePath.Text))
                {
                    MessageBox.Show("The specified File is invalid", "Information Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                FileInfo fileInfo = new FileInfo(txtSendFilePath.Text);

                FileOfferModel fileOffer = new FileOfferModel()
                {
                    OfferedToUserName = comboUser.SelectedItem.ToString(),
                    Description       = txtSendFileDescription.Text,
                    FileName          = Path.GetFileName(txtSendFilePath.Text),
                    Size = fileInfo.Length,
                    NumberOfFileParts = FileTransferHelper.GetNumberOfFileParts(txtSendFilePath.Text, Program.FilePartSize),
                    Hash = FileTransferHelper.GetSHA1Hash(txtSendFilePath.Text)
                };
                Guid fileId = await _communications.SendFileTransferOffer(fileOffer);

                _filePaths.Add(fileId, txtSendFilePath.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred sending file. " + ex.Message);
            }
        }
        async Task StartDownload()
        {
            try
            {
                SetDownloadStatus(FileTransferCompletionStatus.Started);

                lblFinishedStatus.Visible = false;
                DateTime startTime = DateTime.Now;

                var    filePartPaths  = new List <string>();
                string downloadFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "HIIT", FileDownloadCommand.FileId.ToString());
                if (!Directory.Exists(downloadFolder))
                {
                    Directory.CreateDirectory(downloadFolder);
                }

                progress.Value   = 0;
                progress.Maximum = FileDownloadCommand.NumberParts;
                for (int i = 0; i < FileDownloadCommand.NumberParts; i++)
                {
                    if (CancelDownload)
                    {
                        break;
                    }

                    string tempFile = Path.Combine(downloadFolder, i + ".data");
                    await _communications.DownloadFilePart(FileDownloadCommand, tempFile, i);

                    filePartPaths.Add(tempFile);
                    progress.Value++;
                }

                if (CancelDownload)
                {
                    _logger.LogInformation(string.Format("File '{0}' download cancelled.", _destinationPath));

                    // clean up the downloaded files.
                    filePartPaths.ForEach(x => File.Delete(x));
                    Directory.Delete(downloadFolder, true);

                    ShowFinishedStatus("Download Cancelled", false);
                    SetDownloadStatus(FileTransferCompletionStatus.Cancelled);
                }
                else
                {
                    _destinationPath = Path.Combine(_configuration.DownloadFolder, FileDownloadCommand.FileName);
                    await FileTransferHelper.CombineFileParts(_destinationPath, filePartPaths);

                    _logger.LogInformation(string.Format("File '{0}' downloaded successfully. Duration : {1}", _destinationPath, DateTime.Now.Subtract(startTime)));

                    // clean up the downloaded files.
                    filePartPaths.ForEach(x => File.Delete(x));
                    Directory.Delete(downloadFolder, true);


                    ShowFinishedStatus("Download Completed", true);
                    SetDownloadStatus(FileTransferCompletionStatus.Successful);
                }

                return;
            }
            catch (Exception ex)
            {
                _logger.LogInformation(string.Format("An error occurred downloading file '{0}' (Id : {1}). Error : {2}", FileDownloadCommand.FileName, FileDownloadCommand.FileId, ex.Message));
                ShowFinishedStatus("Download failed. " + ex.Message, false);
            }

            SetDownloadStatus(FileTransferCompletionStatus.Error);
        }