Exemple #1
0
        //public async Task Consume(ConsumeContext<CalculateFeatureVectors> context)
        //{
        //    var resultName = $"{context.Message.CorrelationId}-csv";
        //    var fileBytes = File.ReadAllBytes("FocusSynthesis_InStock_071411.csv");
        //    _keyValueRepository.SaveData(resultName, fileBytes);
        //    _keyValueRepository.SetExpiration(resultName, TimeSpan.Parse(_sspSettings.RedisExpirationTime));

        //    await _bus.Publish(new FeatureVectorsCalculated { Structures = 15, Columns = 18, Failed = 0, CorrelationId = context.Message.CorrelationId });
        //}

        private void SaveMessage(string key, object message)
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream    ms = new MemoryStream();

            bf.Serialize(ms, message);
            _keyValueRepository.SaveData(key, ms.ToArray());
            _keyValueRepository.SetExpiration(key, TimeSpan.Parse(_sspSettings.RedisExpirationTime));
        }
        public async Task <IActionResult> CalculateFeatureVector()
        {
            if (!IsMultipartContentType(Request.ContentType))
            {
                return(BadRequest());
            }

            var correlationId = NewId.NextGuid();

            Log.Information("Saving file for feature vector calculating...");

            var boundary = HeaderUtilities.RemoveQuotes(MediaTypeHeaderValue.Parse(Request.ContentType).Boundary);
            var reader   = new MultipartReader(boundary.Value, Request.Body);

            MultipartSection section;

            IDictionary <string, object> metadata     = new Dictionary <string, object>();
            IEnumerable <Fingerprint>    fingerprints = null;
            bool   isFileLoaded  = false;
            string fileExtension = "";

            while ((section = await reader.ReadNextSectionAsync()) != null)
            {
                var contentDisposition = section.GetContentDispositionHeader();

                if (contentDisposition.IsFormDisposition())
                {
                    var formDataSection = section.AsFormDataSection();

                    if (formDataSection.Name.ToLower() == "fingerprints")
                    {
                        string fpString = await formDataSection.GetValueAsync();

                        fingerprints = JsonConvert.DeserializeObject <IEnumerable <Fingerprint> >(fpString);
                    }
                }

                if (contentDisposition.IsFileDisposition())
                {
                    var fileSection = section.AsFileSection();
                    fileExtension = Path.GetExtension(fileSection.FileName).ToLower();
                    if (!_fvcSettings.SupportedFormats.Contains(fileExtension))
                    {
                        return(BadRequest($"File {fileSection.FileName} is not supported for Feature Vector Calculation"));
                    }

                    Log.Information($"Saving file {fileSection.FileName}");
                    byte[] source;
                    byte[] buffer = new byte[16 * 1024];
                    using (MemoryStream ms = new MemoryStream())
                    {
                        int read;
                        while ((read = fileSection.FileStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            ms.Write(buffer, 0, read);
                        }
                        source = ms.ToArray();
                    }

                    var fileName = $"{correlationId}-file";

                    _keyValueRepository.SaveData(fileName, source);

                    TimeSpan expiry = TimeSpan.Parse(_sspSettings.RedisExpirationTime);

                    _keyValueRepository.SetExpiration(fileName, expiry);

                    isFileLoaded = true;
                }
            }

            if (fingerprints != null && isFileLoaded)
            {
                if (fileExtension == ".cif" && (fingerprints.Count() > 3 || fingerprints.Count() == 0))
                {
                    return(BadRequest("Invalid number of fingerprints for Feature Vector Calculation. It should bee between 1 and 3."));
                }

                await _bus.Publish <CalculateFeatureVectors>(new { CorrelationId = correlationId, Fingerprints = fingerprints, FileType = fileExtension.Replace(".", "") });

                return(StatusCode((int)HttpStatusCode.Created, new { Id = correlationId }));
            }
            else
            {
                return(BadRequest());
            }
        }