public void ProcessRequest(HttpContext context)
{
var requested_file = context.Request.RawUrl;
var source_file = context.Server.MapPath(requested_file);
int resolution = 0;
//check source file exists
if (!File.Exists(source_file)) {
SendErrorImage(context, "Image not found");
return;
}
//check if we should ignore this image
if (use_filter) {
if(!System.Text.RegularExpressions.Regex.IsMatch(source_file, regex_filter)) {
SendImage(context, source_file, browser_cache);
return;
}
}
//look for cookie identifying resolution
if (context.Request.Cookies[cookie_name] != null) {
int client_width = 0;
if (int.TryParse(context.Request.Cookies[cookie_name].Value, out client_width)) {
resolution = resolutions.OrderBy(i => i).FirstOrDefault(break_point => client_width <= break_point);
//if resolution exceeds largest break-point, return raw image.
if(default_raw && client_width > resolution) {
SendImage(context, source_file, browser_cache);
return;
}
} else {
//delete the mangled cookie
context.Response.Cookies[cookie_name].Value = string.Empty;
context.Response.Cookies[cookie_name].Expires = DateTime.Now;
}
}
//if no resolution set, use default
if (resolution == 0) {
resolution = mobile_first && !BrowserDetect(context) ? resolutions.Min() : resolutions.Max();
}
//map path to cached file
string cache_file = context.Server.MapPath(string.Format("/{0}/{1}/{2}", cache_path, resolution, requested_file));
//send image
try {
if (File.Exists(cache_file)) { // it exists cached at that size
if (watch_cache) { // if cache watching is enabled, compare cache and source modified dates to ensure the cache isn't stale
cache_file = RefreshCache(source_file, cache_file, resolution);
}
//send cached image
SendImage(context, cache_file, browser_cache);
return;
} else {
string file = GenerateImage(source_file, cache_file, resolution);
SendImage(context, file, browser_cache);
return;
}
} catch (Exception ex) { // send exception message as image
SendErrorImage(context, ex.Message);
}
}