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 GetImageThumbnail(string imageId, string sizeId, string scaleId) { using (Profiler.Measure("ImageController.GetImageThumbnail")) { EntityRef imageIdRef = WebApiHelpers.GetIdWithDashedAlias(imageId); EntityRef sizeIdRef = WebApiHelpers.GetIdWithDashedAlias(sizeId); EntityRef scaleIdRef = WebApiHelpers.GetIdWithDashedAlias(scaleId); var imageInterface = new ImageInterface(); imageInterface.PreloadImageThumbnail(imageIdRef); ImageFileType thumbnailImage = imageInterface.GetImageThumbnail(imageIdRef, sizeIdRef, scaleIdRef); if (thumbnailImage == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } string dataHash = thumbnailImage.FileDataHash; // Get the entity tag, which is the hash of the image data + dimensions + scaling string etag = string.Format("\"{0}\"", imageInterface.GetImageThumbnailETag(dataHash, sizeIdRef, scaleIdRef)); // If the request contains the same e-tag then return a not modified if (Request.Headers.IfNoneMatch != null && Request.Headers.IfNoneMatch.Any(et => et.Tag == etag)) { var notModifiedResponse = new HttpResponseMessage(HttpStatusCode.NotModified); notModifiedResponse.Headers.ETag = new EntityTagHeaderValue(etag, false); notModifiedResponse.Headers.CacheControl = new CacheControlHeaderValue { MustRevalidate = true, MaxAge = GetCacheMaxAge( ) }; return(notModifiedResponse); } Stream imageDataStream; try { imageDataStream = imageInterface.GetImageDataStream(dataHash); } catch (FileNotFoundException) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } if (imageDataStream == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } // Return the image var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(imageDataStream) }; response.Headers.ETag = new EntityTagHeaderValue(etag, false); response.Headers.CacheControl = new CacheControlHeaderValue { MustRevalidate = true, MaxAge = GetCacheMaxAge( ) }; response.Content.Headers.ContentType = new MediaTypeHeaderValue(GetImageMediaType(thumbnailImage.FileExtension)); return(response); } }