protected virtual bool NoSvgSupport(SvgMagicOptions options, HttpBrowserCapabilitiesBase browser) { Debug.WriteLine(browser.Browser); return ((browser.Browser == "IE" && browser.MajorVersion < 9) // IE8 || (browser.Browser == "Android" && browser.MajorVersion <= 4 && browser.MinorVersion < 4)); // Android browser < 4.4 (requires App_Browsers files for detection }
public static SvgMagicOptions Parse(NameValueCollection queryStringParams, SvgMagicHandlerConfigurationSection config) { var options = new SvgMagicOptions(); var format = config.DefaultImageFormat; Enum.TryParse(queryStringParams.Get("format"), true, out format); options.Format = format; bool refresh; bool.TryParse(queryStringParams.Get("refresh"), out refresh); options.Refresh = refresh; bool force; bool.TryParse(queryStringParams.Get("force"), out force); options.Force = force; float height; float.TryParse(queryStringParams.Get("height"), out height); options.Height = height; float width; float.TryParse(queryStringParams.Get("width"), out width); options.Width = width; options.SetImageFormat(config); if (config.TestMode) { options.Force = true; options.Refresh = true; } return(options); }
protected virtual Stream ConvertSvg(Stream svgInput, SvgMagicOptions options, bool failOnError = false) { if (svgInput == null || !svgInput.CanRead) { if (failOnError) { throw new SvgMagicException("Svg input stream was null or could not be read", HttpStatusCode.NotFound); } return(null); } var svg = SvgDocument.Open <SvgDocument>(svgInput); if (svg == null) { if (failOnError) { throw new SvgMagicException("Unable to open stream as Svg", HttpStatusCode.UnsupportedMediaType); } return(null); } if ((svg.Height.Type == SvgUnitType.Percentage) || (svg.Width.Type == SvgUnitType.Percentage)) { if (failOnError) { throw new SvgMagicException("Svg's with percentage based dimensions are not supported for fallback conversion", HttpStatusCode.UnsupportedMediaType); } return(null); } if (options.HasDimensions()) { svg.Height = options.Height; svg.Width = options.Width; } else if (options.Height > 0) { var aspectRatio = svg.Height.Value / svg.Width.Value; svg.Height = options.Height; svg.Width = options.Height / aspectRatio; } else if (options.Width > 0) { var aspectRatio = svg.Width / svg.Height; svg.Width = options.Width; svg.Height = options.Width / aspectRatio; } else { options.Height = float.Parse(svg.Height.Value.ToString()); options.Width = float.Parse(svg.Width.Value.ToString()); } var outputStream = new MemoryStream(); using (var bmp = svg.Draw()) { Thread.Sleep(50); switch (options.Format) { case SvgMagicImageFormat.Bmp: bmp.Save(outputStream, ImageFormat.Bmp); break; case SvgMagicImageFormat.Png: bmp.Save(outputStream, ImageFormat.Png); break; case SvgMagicImageFormat.Jpeg: bmp.Save(outputStream, ImageFormat.Jpeg); break; case SvgMagicImageFormat.Gif: bmp.Save(outputStream, ImageFormat.Gif); break; default: throw new SvgMagicException(string.Format("Unknown image format specified - {0}", options.Format), HttpStatusCode.UnsupportedMediaType); } } return(outputStream); }
protected virtual void ProcessRequestCore(HttpContextBase context) { if (context.Request.CurrentExecutionFilePathExtension != string.Format(".{0}", _config.SvgExtension)) { context.Response.StatusCode = 415; context.Response.StatusDescription = string.Format("Invalid resource type for handler, handler supports files with '{0}' extension only", _config.SvgExtension); return; } var urlPath = context.Request.CurrentExecutionFilePath; var resourcePath = context.Request.MapPath(urlPath); if (!ResourceExists(resourcePath)) { context.Response.StatusCode = 404; return; } DateTime modifiedSince; DateTime resourceModifiedDate = GetResourceUpdateDateTime(resourcePath); resourceModifiedDate = resourceModifiedDate.AddTicks(-(resourceModifiedDate.Ticks % TimeSpan.TicksPerSecond)); string ifModifiedSince = context.Request.Headers["If-Modified-Since"]; if (!string.IsNullOrEmpty(ifModifiedSince) && ifModifiedSince.Length > 0 && DateTime.TryParse(ifModifiedSince, out modifiedSince)) { if (resourceModifiedDate <= modifiedSince) { context.Response.StatusCode = 304; return; } } context.Response.AddFileDependency(resourcePath); context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetETagFromFileDependencies(); context.Response.Cache.SetLastModifiedFromFileDependencies(); var options = SvgMagicOptions.Parse(context.Request.QueryString, Configuration); if (options.Force || NoSvgSupport(options, context.Request.Browser)) { var imageCache = GetImageCache(context); using (var cachedFileStream = imageCache.Get(urlPath, options)) { if (cachedFileStream == null || ShouldInvalidateCachedItem(resourceModifiedDate, imageCache.GetCacheItemModifiedDateTime(urlPath, options))) { using (var svg = GetResourceStream(resourcePath)) { using (var outputStream = ConvertSvg(svg, options, true)) { if (outputStream != null) { imageCache.Put(outputStream, urlPath, options); outputStream.CopyTo(context.Response.OutputStream); outputStream.Close(); } else { context.Response.StatusCode = 415; context.Response.StatusDescription = "Unable to convert Svg to " + options.MimeType; } } svg.Close(); } } else { cachedFileStream.CopyTo(context.Response.OutputStream); cachedFileStream.Close(); } } context.Response.ContentType = options.MimeType; } else { context.Response.ContentType = Configuration.SvgMimeType; context.Response.TransmitFile(resourcePath); } }