public void SetConnectionRequestStatus([FromBody] ConnectionRequestUpdateStatusModel model) { var request = Program.ConnectionRequestsManager.Get(model.Id); if (request == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = "Invalid Id" }); } if (request.ConnectToUserName.ToLower() != UserName.ToLower()) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized) { ReasonPhrase = "You are not authorised to update the specified request" }); } Program.ConnectionRequestsManager.SetStatus(model.Id, model.Status); #if DEBUG Console.WriteLine(string.Format("SetConnectionRequestStatus : ConnectionRequest status update to '{0}' by User '{1}', connecting to user '{2}.", model.Status, UserName, request.ConnectToUserName)); #endif if (model.Status == ConnectionRequestStatusEnum.Approved) { Program.ConnectionsManager.Add(request.RequestedByUserName, UserName); } UserCommandsLock.Wake(request.RequestedByUserName); UserCommandsLock.Wake(UserName); }
public void RequestConnection([FromBody] CreateConnectionRequestModel model) { if (model.ConnectToUserName == UserName) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = "Invalid UserName" }); } Program.ConnectionRequestsManager.RequestConnection(UserName, model.ConnectToUserName, model.IntroductionMessage); #if DEBUG Console.WriteLine(string.Format("RequestConnection : ConnectionRequest created by User '{0}', connecting to user '{1}.", UserName, model.ConnectToUserName)); #endif UserCommandsLock.Wake(model.ConnectToUserName); UserCommandsLock.Wake(UserName); }
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); }
public Guid SendMessage([FromBody] SendMessageModel model, string messageType) { if ((model.DestinationUserName != UserName) && (!Program.ConnectionsManager.ConnectionExists(UserName, model.DestinationUserName))) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized) { ReasonPhrase = "You are not authorised to send message to the specified user" }); } MessageType type = MessageType.UserMessage; if (!string.IsNullOrEmpty(messageType) && !Enum.TryParse <MessageType>(messageType, out type)) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = "Invalid Message Type" }); } var details = new MessageDetails() { Date = DateTime.Now, SourceDeviceId = DeviceId, SourceUserName = UserName, DestinationUserName = model.DestinationUserName, Value = model.Message, MessageType = type }; QueueInstances.MessageQueue.Add(details); #if DEBUG Console.WriteLine(string.Format("SendMessage : Created message from '{0}' to '{1}'", UserName, model.DestinationUserName)); #endif UserCommandsLock.Wake(model.DestinationUserName); return(details.Id); }
public async Task <HttpResponseMessage> UploadFile(Guid fileId, int filePartNumber) { if (filePartNumber > 5000) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = "Invalid part number." }); } var fileDetails = QueueInstances.FileTransfersQueue.Get(fileId); if (fileDetails == null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = "Invalid file Id." }); } if (fileDetails.SourceUserName.ToLower() != UserName.ToLower()) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You are not authorised to download the specified file")); } if (fileDetails.NumberOfFileParts < filePartNumber) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = "Part number too large." }); } var filePart = fileDetails.FileParts.FirstOrDefault(x => x.PartNumber == filePartNumber); if (filePart != null) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = "File Part already uploaded." }); } // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } if (!File.Exists("c:\\uploadedFiles")) { Directory.CreateDirectory("c:\\uploadedFiles"); } var provider = new MultipartFormDataStreamProvider("c:\\uploadedFiles"); try { await Request.Content.ReadAsMultipartAsync(provider); if (provider.FileData.Count != 1) { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = "Invalid file data." }); } var fileData = provider.FileData[0]; if (fileDetails.FileParts.Count == 0) { fileDetails.UploadStartDate = DateTime.Now; } fileDetails.FileParts.Add(new FilePart() { FilePath = fileData.LocalFileName, PartNumber = filePartNumber }); #if DEBUG Console.WriteLine(string.Format("UploadFile : File '{0}', part : {1} uploaded by User '{1}'", fileDetails.FileName, filePartNumber, UserName)); #endif if (fileDetails.FileParts.Count >= fileDetails.NumberOfFileParts) { fileDetails.UploadCompletedDate = DateTime.Now; UserCommandsLock.Wake(fileDetails.DestinationUserName); } return(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK }); } catch (Exception ex) { if (ex is HttpResponseException) { throw; } return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }