public async Task <Guid> SendFileTransferOffer(FileOfferModel fileOffer)
        {
            try
            {
                if (string.IsNullOrEmpty(_accessToken))
                {
                    throw new Exception("Not logged in");
                }

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    var url = string.Format("{0}/api/file/offer", _configuration.ServerBaseUrl);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
                    var response = await client.PostAsJsonAsync <FileOfferModel>(url, fileOffer);

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

                    return(await response.Content.ReadAsAsync <Guid>());
                }
            }
            catch (Exception ex)
            {
                if (ex is WebAPIException)
                {
                    throw;
                }
                throw new Exception("An error occurred sending file information. " + ex.Message);
            }
        }
        public Guid FileOffer([FromBody] FileOfferModel model)
        {
            var details = new FileTransferDetails()
            {
                Date                = DateTime.Now,
                Description         = model.Description,
                FileName            = model.FileName,
                Size                = model.Size,
                SourceDeviceId      = model.OfferedFromDeviceId,
                SourceUserName      = model.OfferedByUserName,
                DestinationDeviceId = null,
                DestinationUserName = model.OfferedToUserName
            };

            QueueInstances.FileTransfersQueue.Add(details);
            return(details.Id);
        }
Exemple #3
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);
            }
        }
        public Guid OfferFile([FromBody] FileOfferModel model)
        {
            if ((model.OfferedToUserName != UserName) && (!Program.ConnectionsManager.ConnectionExists(UserName, model.OfferedToUserName)))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized)
                {
                    ReasonPhrase = "You are not authorised to send files to the specified user"
                });
            }

            if ((model.NumberOfFileParts == 0) || (model.NumberOfFileParts > 5000))
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    ReasonPhrase = "Number of file parts is invalid."
                });
            }

            var details = new FileDetails()
            {
                Date                = DateTime.Now,
                Description         = model.Description,
                FileName            = model.FileName,
                Size                = model.Size,
                Hash                = model.Hash,
                NumberOfFileParts   = model.NumberOfFileParts,
                SourceDeviceId      = DeviceId,
                SourceUserName      = UserName,
                DestinationDeviceId = null,
                DestinationUserName = model.OfferedToUserName
            };

            QueueInstances.FileTransfersQueue.Add(details);
#if DEBUG
            Console.WriteLine(string.Format("OfferFile : Created FileTransfer offer. File Name '{0}, from user '{1}' to '{2}'", model.FileName, UserName, model.OfferedToUserName));
#endif
            UserCommandsLock.Wake(UserName);
            return(details.Id);
        }