public FilePathResult Serve() { string filename = (string)RouteData.Values["file_name"]; string extension = (string)RouteData.Values["extension"]; string screen_size = Request.Cookies["screen-size"].Value; var img = Models.Image.GetImage(filename); if (img == null) { return(File(Site.MapPath("~/images/" + filename + "." + extension), "image/" + extension)); } else if (!String.IsNullOrEmpty(screen_size)) { int screenWidth = Convert.ToInt32(screen_size.Split('-').First()); int screenHeight = Convert.ToInt32(screen_size.Split('-').Last()); Models.Image.Device defaultImageDevice = Models.Image.Device.GetDevice(img.TargetDevice); if (img.ResizeForDevices || (img.ResizeDevice > 0)) { List <Models.Image> resizes = img.GetResizes(); if (img.ResizeOf > 0) { resizes = Models.Image.GetImage(img.ResizeOf).GetResizes(); } List <Models.Image.Device> devices = resizes.Select((imgR) => Models.Image.Device.GetDevice(imgR.ResizeDevice)).ToList(); if (screenWidth / defaultImageDevice.Width >= 0.8) { return(File(img.URL, "image/" + img.Extension)); } else { devices.Sort((dv1, dv2) => { return(dv2.Width.CompareTo(dv1.Width)); }); for (int i = 0; i < devices.Count; i++) { if (screenWidth / devices[i].Width >= 0.8 || i == devices.Count - 1) { return(File(resizes.Where((imgR) => imgR.ResizeDevice == devices[i].ID).FirstOrDefault().URL, "image/" + resizes[i].Extension)); } } } } return(File(Site.MapPath(img.URL), "image/" + img.Extension)); } else if (Site.IsMobile()) { List <Models.Image> resizes = img.GetResizes(); var iphoneImage = (from pp in resizes where pp.ResizeDevice.Equals(3) select pp).FirstOrDefault(); return(File(iphoneImage.URL, "image/" + iphoneImage.Extension)); } else { return(File(Site.MapPath("~/images/" + filename + "." + extension), "image/" + extension)); } }
public FilePathResult Serve() { System.Web.HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); System.Web.HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "*"); string filename = (string)RouteData.Values["file_name"]; string extension = (string)RouteData.Values["extension"]; string screen_size = null; if (!String.IsNullOrEmpty((string)Site.Context().Session["screen-size"])) { screen_size = (string)Site.Context().Session["screen-size"]; } if (String.IsNullOrEmpty(screen_size) && Request.Cookies["screen-size"] != null) { screen_size = Convert.ToString(Request.Cookies["screen-size"].Value); } if (String.IsNullOrEmpty(screen_size) && Request.Headers["screen-size"] != null) { screen_size = Convert.ToString(Request.Headers["screen-size"]); } var img = Models.Image.GetImage(filename); if (img == null) { return(File(Site.MapPath("~/images/" + filename + "." + extension), "image/" + extension)); } else if (!String.IsNullOrEmpty(screen_size)) { int screenWidth = Convert.ToInt32(screen_size.Split('-').First()); int screenHeight = Convert.ToInt32(screen_size.Split('-').Last()); Models.Image.Device defaultImageDevice = Models.Image.Device.GetDevice(img.TargetDevice); if (defaultImageDevice == null) { defaultImageDevice = Image.Device.GetDefault(); } if (img.ResizeForDevices || (img.ResizeDevice > 0)) { List <Models.Image> resizes = img.GetResizes(); if (img.ResizeOf > 0) { resizes = Models.Image.GetImage(img.ResizeOf).GetResizes(); } List <Models.Image.Device> devices = resizes.Select((imgR) => Models.Image.Device.GetDevice(imgR.ResizeDevice)).Where((imgR) => imgR != null).ToList(); if (screenWidth / defaultImageDevice.Width >= 0.8) { return(File(img.GetFileURL(), "image/" + img.Extension)); } else { devices.Sort((dv1, dv2) => { return(dv2.Width.CompareTo(dv1.Width)); }); for (int i = 0; i < devices.Count; i++) { if (screenWidth / devices[i].Width >= 0.8 || i == devices.Count - 1) { return(File(resizes.Where((imgR) => imgR.ResizeDevice == devices[i].ID).FirstOrDefault().GetFileURL(), "image/" + resizes[i].Extension)); } } } } return(File(Site.MapPath(img.GetFileURL()), "image/" + img.Extension)); } else if (Site.IsMobile()) { List <Models.Image> resizes = img.GetResizes(); var iphoneImage = (from pp in resizes where pp.ResizeDevice.Equals(1) select pp).FirstOrDefault(); if (iphoneImage == null) { iphoneImage = img; } return(File(iphoneImage.GetFileURL(), "image/" + iphoneImage.Extension)); } else { return(File(Site.MapPath(img.GetFileURL()), "image/" + extension)); } }