Exemplo n.º 1
0
        public async Task <IActionResult> Get(string url, string token)
        {
            try
            {
                Uri uri;

                string[] allowedHosts = _configuration["AllowedMediaHosts"].Split(',');

                if (!VerifyUrlIfEncryptionMode(url, token) ||
                    !Uri.IsWellFormedUriString(url, UriKind.Absolute) ||
                    !Uri.TryCreate(url, UriKind.Absolute, out uri) ||
                    !SecurityUtility.IsAllowedHost(uri.Host, allowedHosts)
                    )
                {
                    _logger.LogWarning(string.Format("Invalid request attempted with the following url \"{0}\".", url));
                    return(new BadRequestResult());
                }

                var data = await _cacheService.GetContent(uri);

                return(new FileContentResult(data.Data, data.ContentType ?? "application/octet-stream"));
            }
            catch (AssetNotFoundException ex)
            {
                _logger.LogError(ex.Message);
                return(new StatusCodeResult(502)); //Bad Gateway
            }
            catch (ServiceUnavailableException ex)
            {
                _logger.LogError(ex.Message);

                return(new StatusCodeResult(503)); //Service Unavailable
            }
            catch (UnsupportedContentTypeException ex)
            {
                _logger.LogError(ex.Message);
                return(new StatusCodeResult(415)); //Unsupported Media Type
            }
            catch (InvalidUriException ex)
            {
                _logger.LogError(ex.Message);
                return(new StatusCodeResult(400)); //Bad request
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Configuration error - please check application configuration", ex);
                throw; // 500 Internal Server Error
            }
        }