Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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);
                }
            }
        }
Ejemplo n.º 3
0
 public ImageController(ImageInterface context)
 {
     _db = context;
 }
Ejemplo n.º 4
0
        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);
            }
        }