public HttpResponseMessage GetDownloadImage(string imageId) { using (Profiler.Measure("ImageController.GetDownloadImage")) { EntityRef imageIdRef = WebApiHelpers.GetIdWithDashedAlias(imageId); var imageInterface = new ImageInterface(); imageInterface.PreloadImage(imageIdRef); var imageFileType = ReadiNow.Model.Entity.Get <ImageFileType>(imageIdRef); if (imageFileType == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } string fileName = imageFileType.Name; string fileExtension = imageFileType.FileExtension; string extension = Path.GetExtension(fileName); if (string.IsNullOrEmpty(extension)) { fileName = string.Format("{0}{1}", fileName, fileExtension); } Stream stream = imageInterface.GetImageDataStream(imageIdRef); if (stream == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue(GetImageMediaType(fileExtension)); // Note: We are not setting the content length because the CompressionHandler will compress // the stream . Specifying the length here will cause the browser to hang as the actual data it // receives (as it is compressed) will be less than the specified content length. response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName, }; return(response); } }
public HttpResponseMessage GetImage(string imageId) { using (Profiler.Measure("ImageController.GetImage")) { EntityRef imageIdRef = WebApiHelpers.GetIdWithDashedAlias(imageId); var imageInterface = new ImageInterface( ); imageInterface.PreloadImage(imageIdRef); using (CacheManager.ExpectCacheHits( )) { HttpResponseMessage response = FileController.GetFileForRequest(Request, imageIdRef, GetImageEtag); switch (response.StatusCode) { case HttpStatusCode.OK: { var imageFileType = ReadiNow.Model.Entity.Get <ImageFileType>(imageIdRef); response.Headers.CacheControl = new CacheControlHeaderValue { MustRevalidate = true, MaxAge = GetCacheMaxAge( ) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue(GetImageMediaType(imageFileType.FileExtension)); } break; case HttpStatusCode.NotModified: response.Headers.CacheControl = new CacheControlHeaderValue { MustRevalidate = true, MaxAge = GetCacheMaxAge( ) }; break; } return(response); } } }