public virtual IFileToStream GetFileToStream(string path) { IFileToStream file = null; try { file = GetFileToStreamInternal(path); } catch (FileNotFoundException) { } if (file == null) { file = new FileToStreamImpl(path); } return(file); }
protected override void OnLoad(EventArgs e) { // See if client has a cached version of the image string ifModifiedSince = this.Request.Headers.Get("If-Modified-Since"); string reqPath = this.Request.QueryString["path"]; string path = reqPath ?? string.Empty; IFileToStream fileToStream = this.GetFileToStream(path); if (fileToStream.Exists) { //Get the last modified time for the current file //Handle the situation where we get a LastModified that is in the future DateTime now = DateTime.Now; DateTime lastModifiedTime = fileToStream.LastModified > now ? now : fileToStream.LastModified; //Check to see if it is a conditional HTTP GET. if (ifModifiedSince != null) { //This is a conditional HTTP GET request. Compare the strings. try { DateTime incrementalIndexTime = DateTime.Parse(ifModifiedSince, DateTimeFormatInfo.InvariantInfo).ToUniversalTime(); // Has to do a string compare because of the resolution if (incrementalIndexTime.ToString(DateTimeFormatInfo.InvariantInfo) == lastModifiedTime.ToString(DateTimeFormatInfo.InvariantInfo)) { // If the file has not been modifed, send a not changed status this.Response.StatusCode = 304; this.Response.End(); } } catch (FormatException) { } } string reqWidth = Page.Request.QueryString["width"]; string reqHeight = Page.Request.QueryString["height"]; string reqAllowEnlarging = Page.Request.QueryString["allowEnlarging"]; string reqAllowStretching = Page.Request.QueryString["allowStretching"]; int width = 0; int height = 0; bool allowEnlarging = reqAllowEnlarging != null && string.Compare(reqAllowEnlarging, "true", true, CultureInfo.InvariantCulture) == 0; bool allowStretching = reqAllowStretching != null && string.Compare(reqAllowStretching, "true", true, CultureInfo.InvariantCulture) == 0; IConfig config = ConfigRepository.Instance.GetSystemConfig(); SmoothingMode smoothingMode = SmoothingMode.Default; if (config.ContainsKey("imageScaleConfig", "smoothingMode")) { smoothingMode = (SmoothingMode)Enum.Parse(typeof(SmoothingMode), config.GetValue("imageScaleConfig", "smoothingMode"), true); } InterpolationMode interpolationMode = InterpolationMode.Default; if (config.ContainsKey("imageScaleConfig", "interpolationMode")) { interpolationMode = (InterpolationMode)Enum.Parse(typeof(InterpolationMode), config.GetValue("imageScaleConfig", "interpolationMode"), true); } PixelOffsetMode pixelOffsetMode = PixelOffsetMode.Default; if (config.ContainsKey("imageScaleConfig", "pixelOffsetMode")) { pixelOffsetMode = (PixelOffsetMode)Enum.Parse(typeof(PixelOffsetMode), config.GetValue("imageScaleConfig", "pixelOffsetMode"), true); } CompositingQuality compositingQuality = CompositingQuality.Default; if (config.ContainsKey("imageScaleConfig", "compositingQuality")) { compositingQuality = (CompositingQuality)Enum.Parse(typeof(CompositingQuality), config.GetValue("imageScaleConfig", "compositingQuality"), true); } bool useEmbeddedColorManagement = false; if (config.ContainsKey("imageScaleConfig", "useEmbeddedColorManagement")) { useEmbeddedColorManagement = config.GetValue <bool>("imageScaleConfig", "useEmbeddedColorManagement"); } try { width = reqWidth == null ? width : int.Parse(reqWidth, NumberFormatInfo.InvariantInfo); } catch (FormatException) { } try { height = reqHeight == null ? height : int.Parse(reqHeight, NumberFormatInfo.InvariantInfo); } catch (FormatException) { } using (Stream fileStream = fileToStream.OpenStream()) { ImageScale.ResizeFlags resizeFlags = ImageScale.ResizeFlags.None; if (allowEnlarging) { resizeFlags = resizeFlags | ImageScale.ResizeFlags.AllowEnlarging; } if (allowStretching) { resizeFlags = resizeFlags | ImageScale.ResizeFlags.AllowStretching; } this.Response.Clear(); try { ImageScale.Resize(fileStream, this.Response.OutputStream, width, height, resizeFlags, smoothingMode, interpolationMode, pixelOffsetMode, compositingQuality, useEmbeddedColorManagement); this.Response.AddHeader("Content-Disposition", "inline;filename=\"" + fileToStream.Name + "\""); this.Response.ContentType = fileToStream.MimeType; this.Response.Cache.SetLastModified(lastModifiedTime); //The following lines enable downlevel caching in server or browser cache. But not in proxies. this.Response.Cache.SetCacheability(HttpCacheability.Public); //Set the expiration time for the downlevel cache this.Response.Cache.SetExpires(DateTime.Now.AddMinutes(5)); this.Response.Cache.SetValidUntilExpires(true); this.Response.Cache.VaryByParams["*"] = true; } catch (OutOfMemoryException ex) { // If not image throw new FileNotFoundException(ex.Message, ex); } catch (FileNotFoundException) { // If file not found // We know that the file exists so we throw a forbidden request exception throw new HttpException(403, "Forbidden"); } } } else { this.Response.Clear(); throw new HttpException(404, "Not Found"); } }