public async Task <MessageResponse> SendToRabbitMessageQueue(STPBatch batch) { MessageResponse response = new MessageResponse() { Success = true }; ConnectionFactory factory = new ConnectionFactory { UserName = configurationPackage.Settings.Sections["RabbitMQConnection"].Parameters["Username"].Value, Password = configurationPackage.Settings.Sections["RabbitMQConnection"].Parameters["Password"].Value, VirtualHost = configurationPackage.Settings.Sections["RabbitMQConnection"].Parameters["VirtualHost"].Value, HostName = configurationPackage.Settings.Sections["RabbitMQConnection"].Parameters["HostName"].Value, Port = AmqpTcpEndpoint.UseDefaultPort }; try { using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { channel.ExchangeDeclare(exchange: exchangeName, type: "topic", durable: true); var ms = new MemoryStream(); using (var writer = new BsonWriter(ms)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(writer, batch); } var properties = channel.CreateBasicProperties(); properties.Persistent = true; channel.BasicPublish(exchange: exchangeName, routingKey: routingKey, basicProperties: null, body: ms.ToArray()); } } } catch (Exception ex) { response.Success = false; throw new Exception(ex.Message); } return(response); }
public async Task <HttpResponseMessage> PostFiles([FromForm] string value) { FormOptions _defaultFormOptions = new FormOptions(); DateTime methodStartTimeStamp = DateTime.Now; HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Created); if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) { response = GetResponseMessage(HttpStatusCode.BadRequest, MessageConstant.MimePartErrorMessage); return(response); } var formAccumulator = new KeyValueAccumulator(); var boundary = MultipartRequestHelper.GetBoundary( MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(boundary, HttpContext.Request.Body); var section = await reader.ReadNextSectionAsync(); string targetFilePath = string.Empty; var attachmentList = new List <Attachment>(); while (section != null) { ContentDispositionHeaderValue contentDisposition; var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition); if (hasContentDispositionHeader) { if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition)) { var rootPath = _configuration["SharedLocation:RootPath"].ToString(); try { var directoryInfo = Directory.CreateDirectory(Path.Combine(rootPath, Guid.NewGuid().ToString())); targetFilePath = directoryInfo.FullName + "//" + section.AsFileSection().FileName; var bodyStream = new MemoryStream(); section.Body.CopyTo(bodyStream); byte[] attachmentData = new byte[bodyStream.Length]; attachmentList.Add(new Attachment() { FileName = section.AsFileSection().FileName, FileData = bodyStream.ToArray() }); using (var targetStream = System.IO.File.Create(targetFilePath)) { bodyStream.Position = 0; await bodyStream.CopyToAsync(targetStream); } bodyStream.Close(); } catch (Exception ex) { response = GetResponseMessage(HttpStatusCode.BadRequest, ex.Message); return(response); } } else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition)) { var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name); var encoding = GetEncoding(section); using (var streamReader = new StreamReader( section.Body, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true)) { // The value length limit is enforced by MultipartBodyLengthLimit var attachmentValue = await streamReader.ReadToEndAsync(); if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase)) { value = String.Empty; } formAccumulator.Append(key.ToString(), attachmentValue); if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit) { response = GetResponseMessage(HttpStatusCode.BadRequest, MessageConstant.KeyCountLimit); return(response); } } } } // Drains any remaining section body that has not been consumed and // reads the headers for the next section. section = await reader.ReadNextSectionAsync(); } if (formAccumulator.GetResults().Count < 0) { response = GetResponseMessage(HttpStatusCode.BadRequest, MessageConstant.NoMetaData); return(response); } else if (formAccumulator.GetResults().Count > 1) { response = GetResponseMessage(HttpStatusCode.BadRequest, MessageConstant.MoreThanOneMetaData); return(response); } else { var batchMetaDataDictionary = formAccumulator.GetResults().FirstOrDefault(); if (batchMetaDataDictionary.Key != "batchMetaData") { response = GetResponseMessage(HttpStatusCode.BadRequest, MessageConstant.NoMetaData); return(response); } else { try { var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore }; var captureMetaData = JsonConvert.DeserializeObject <ApiMetaData>(batchMetaDataDictionary.Value, settings); STPBatch batch = new STPBatch() { Destination = "MyQ", Attachments = attachmentList, BatchMetaData = captureMetaData, FolderName = Guid.NewGuid().ToString(), ReceivedDateTime = DateTime.Now, Status = "Ready" }; var rabbitMqServiceInstance = ServiceProxy.Create <IMQService>(new Uri("fabric:/NextGenCapture_POC/NextGenCapture_RabbitMQService")); await rabbitMqServiceInstance.SendToRabbitMessageQueue(batch); } catch (Exception ex) { response = GetResponseMessage(HttpStatusCode.BadRequest, ex.Message); return(response); } } } return(response); }