예제 #1
0
        public virtual async Task <ActionResult <IEnumerable <dynamic> > > SearchAsync([FromQuery] string fields, [FromQuery] string range, [FromQuery] string asc, [FromQuery] string desc)
        {
            IQueryCollection requestQuery = Request.Query;

            var query = _context.Set <TModel>().AsQueryable();

            // TRIS
            if (!string.IsNullOrWhiteSpace(asc) || !string.IsNullOrWhiteSpace(desc))
            {
                query = query.OrderByDynamic(asc, desc);
            }

            // FILTRES
            if (requestQuery != null && requestQuery.Count() > 0)
            {
                query = query.WhereDynamic(requestQuery, true);
            }

            // PAGINATION
            if (!string.IsNullOrWhiteSpace(range))
            {
                var tab = range.Split('-');
                query = query.TakePageResult(Int32.Parse(tab[0]), Int32.Parse(tab[1]));
            }

            // RENDU PARTIEL
            if (!string.IsNullOrWhiteSpace(fields))
            {
                var tab = fields.Split(',');
                query = query.SelectModel(tab);
                var results = await query.ToListAsync();

                return(Ok(results.Select((x) => IQueryableExtensions.SelectObject(x, tab)).ToList()));
            }
            else
            {
                return(Ok(ToJsonList(await query.ToListAsync())));
            }
        }
예제 #2
0
 public IActionResult GetAllFromCollectionByAndFilter(string name, string token = null)
 {
     try
     {
         UserAuthenticatedToChannel(token, name);
         List <BsonDocument> bson;
         IQueryCollection    filter = Request.Query;
         if (filter.Count() > 0)
         {
             bson = GetAllFromCollectionByFilterInternal(name, filter, true);
         }
         else
         {
             bson = GetAllFromCollectionInternal(name);
         }
         return(Json(bson.Select(x => BsonSerializer.Deserialize <Object>(x))));
     }
     catch (Exception e)
     {
         return(Json(new { message = e.Message }));
     }
 }
예제 #3
0
        public async Task <IActionResult> GetMediaFileAsync(IQueryCollection query)
        {
            var fileLink  = query[query.Keys.Where(x => x.Contains(FilePath)).FirstOrDefault()];
            var mediaType = query[query.Keys.Where(x => x.Contains(MediaType)).FirstOrDefault()];
            var tenantId  = query[query.Keys.Where(x => x.Contains(TenantId)).FirstOrDefault()];

            if (query.Count() == 0 ||
                (string.IsNullOrEmpty(fileLink) && string.IsNullOrEmpty(mediaType) && string.IsNullOrEmpty(tenantId)))
            {
                _logger.LogWarning("GetMediaFileAsync - Missing one or more query string parameters");

                Response ret = new Response
                {
                    Saying = new TwilioSay {
                        Text = string.Empty, Voice = "alice"
                    }
                };

                return(new OkObjectResult(ret));
            }

            try
            {
                fileLink = Encoding.UTF8.GetString(Base64UrlTextEncoder.Decode(query[query.Keys.Where(x => x.Contains(FilePath)).FirstOrDefault()]));
                var relativeAddress = $"{tenantId}/{mediaType}";
                var fileName        = $@"{fileLink.ToString().Substring(0, 2)}/{fileLink}";
                var cloudFile       = $"{relativeAddress}/{fileName}";

                _logger.LogInformation($"Getting media file: '{cloudFile}' for organizationId: '{tenantId}' from storage account");

                // Optimize for Storage RS-GRS
                var options = new BlobClientOptions
                {
                    Diagnostics = { IsLoggingEnabled = true },
                    GeoRedundantSecondaryUri = new Uri(_configuration[Constants.StorageAccount.SecondaryConnectionUrl]),
                    Retry =
                    {
                        Mode       = RetryMode.Exponential,
                        MaxRetries = int.Parse(_configuration[Constants.StorageAccount.GeoRedundantStorageMaxRetries] ?? "3"),
                        Delay      = TimeSpan.FromSeconds(double.Parse(_configuration[Constants.StorageAccount.GeoRedundantStorageDelayInSeconds] ?? "0.1")),
                        MaxDelay   = TimeSpan.FromSeconds(double.Parse(_configuration[Constants.StorageAccount.GeoRedundantStorageMaxDelayInSeconds] ?? "3"))
                    }
                };

                BlobClient blobClient = new BlobClient(_configuration[Constants.StorageAccount.ConnectionString],
                                                       Constants.StorageAccount.TwilioMediaFilesBlobContainer,
                                                       cloudFile,
                                                       options);

                try
                {
                    MemoryStream memoryStream = new MemoryStream();
                    await blobClient.DownloadToAsync(memoryStream);

                    memoryStream.Position = 0;
                    return(new FileStreamResult(memoryStream, contentType: "audio/wav"));
                }
                catch (Azure.RequestFailedException ex)
                {
                    _logger.LogError(ex, $"RequestFailedException - accessing media file: '{cloudFile}' \n{ex.Message}");
                }
                catch (Exception ex2)
                {
                    _logger.LogError(ex2, $"General Exception - accessing media file: '{cloudFile}' \n{ex2.Message}");
                }

                throw new Exception("Unable to open file not streamed");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"GetMediaFileAsync Exception. Details: {ex.Message}");

                Response ret = new Response
                {
                    Saying = new TwilioSay {
                        Text = string.Empty, Voice = "alice"
                    }
                };

                return(new OkObjectResult(ret));
            }
        }
예제 #4
0
        public virtual async Task <ActionResult <IEnumerable <dynamic> > > GetAllAsync([FromQuery] string fields, [FromQuery] string range, [FromQuery] string asc, [FromQuery] string desc)
        {
            IQueryCollection requestQuery = Request.Query;

            var query = _context.Set <TModel>().AsQueryable();

            // TRIS
            if (!string.IsNullOrWhiteSpace(asc) || !string.IsNullOrWhiteSpace(desc))
            {
                query = query.OrderByDynamic(asc, desc);
            }

            // FILTRES
            if (requestQuery != null && requestQuery.Count() > 0)
            {
                query = query.WhereDynamic(requestQuery);
            }

            // PAGINATION
            if (!string.IsNullOrWhiteSpace(range))
            {
                var tab = range.Split('-');

                int total = query.Count();

                query = query.TakePageResult(Int32.Parse(tab[0]), Int32.Parse(tab[1]));

                var linkBuilder = new PageLinkBuilder(Url, "", null, Int32.Parse(tab[0]), Int32.Parse(tab[1]), total);

                List <string> links = new List <string>();
                if (linkBuilder.FirstPage != null)
                {
                    links.Add(string.Format(LinkHeaderTemplate, linkBuilder.FirstPage, "first"));
                }
                if (linkBuilder.PreviousPage != null)
                {
                    links.Add(string.Format(LinkHeaderTemplate, linkBuilder.PreviousPage, "prev"));
                }
                if (linkBuilder.NextPage != null)
                {
                    links.Add(string.Format(LinkHeaderTemplate, linkBuilder.NextPage, "next"));
                }
                if (linkBuilder.LastPage != null)
                {
                    links.Add(string.Format(LinkHeaderTemplate, linkBuilder.LastPage, "last"));
                }
                Response.Headers.Add("Link", string.Join(", ", links));

                Response.Headers.Add("Content-Range", linkBuilder.GetContentRange());
                Response.Headers.Add("Accept-Ranges", typeof(TModel).Name + " " + total);
            }

            // RENDU PARTIEL
            if (!string.IsNullOrWhiteSpace(fields))
            {
                var tab = fields.Split(',');
                query = query.SelectModel(tab);
                var results = await query.ToListAsync();

                return(Ok(results.Select((x) => IQueryableExtensions.SelectObject(x, tab)).ToList()));
            }
            else
            {
                return(Ok(ToJsonList(await query.ToListAsync())));
            }
        }