public HttpResponseMessage Photo(int id, [FromUri] ImageParam param) { HttpResponseMessage message = new HttpResponseMessage(); ImageFormatter formatter = new ImageFormatter(GuideProvider.GetGuideImage(id), Pictures.GuideNoPhoto); param.ApplyFormat(formatter); Stream content = formatter.CreateStream(); if (content != null) { message.Content = new StreamContent(content); message.Content.Headers.ContentType = new MediaTypeHeaderValue(formatter.MediaType); return message; } message.StatusCode = HttpStatusCode.NotFound; return message; }
public HttpResponseMessage CatalogImage(int id, [FromUri] CatalogImageParam param) { if (param == null) { throw new ArgumentNullException("param"); } HttpResponseMessage message = new HttpResponseMessage(); object[] args = new object[6]; args[0] = id; args[1] = param.h; args[2] = param.w; args[3] = param.i; args[4] = param.Mode; bool? showDefault = param.ShowDefault; args[5] = showDefault.HasValue ? ((object) showDefault.GetValueOrDefault()) : ((object) 1); string key = string.Format("catalogImage[id:{0}][w:{1}][h:{2}][i:{3}][m:{4}][d:{5}]", args); ImageCacheItem item = HttpContext.Current.Cache[key] as ImageCacheItem; if ((item == null) || Settings.IsCacheDisabled) { Image catalogImage = ExcursionProvider.GetCatalogImage(id, param.Index); if ((catalogImage == null) && (!(showDefault = param.ShowDefault).GetValueOrDefault() && showDefault.HasValue)) { message.StatusCode = HttpStatusCode.NotFound; } else { ImageFormatter formatter = new ImageFormatter(catalogImage, Pictures.nophoto) { Format = (catalogImage != null) ? ImageFormat.Jpeg : ImageFormat.Png }; param.ApplyFormat(formatter); Stream stream = formatter.CreateStream(); if (stream != null) { item = ImageCacheItem.Create(stream, formatter.MediaType); HttpContext.Current.Cache.Add(key, item, null, DateTime.Now.AddMinutes(10.0), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null); message.Content = new StreamContent(stream); message.Content.Headers.ContentType = new MediaTypeHeaderValue(formatter.MediaType); } else { message.StatusCode = HttpStatusCode.NotFound; } } } else { message.Content = new StreamContent(item.CraeteStream()); message.Content.Headers.ContentType = new MediaTypeHeaderValue(item.MediaType); } message.Headers.CacheControl = new CacheControlHeaderValue(); message.Headers.CacheControl.Public = true; message.Headers.CacheControl.MaxAge = new TimeSpan?(TimeSpan.FromHours(1.0)); return message; }