コード例 #1
0
        private static Func <Stream> ProcessAndCacheImage(ImageMediaSource src, int?maxWidth, int?maxHeight, string borders, string format)
        {
            // if it's already in the cache, use that file
            string filename = String.Format("stream_{0}_{1}_{2}_{3}.{4}", src.GetUniqueIdentifier(), maxWidth, maxHeight, borders, format);

            if (cache.Contains(filename))
            {
                if (src.GetFileInfo().LastModifiedTime > cache.GetLastModifiedTime(cache.GetPath(filename)))
                {
                    cache.Invalidate(filename);
                }
                else
                {
                    return(() => new FileStream(cache.GetPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read));
                }
            }

            // otherwise, resize if needed and save to cache
            try
            {
                bool hasToResize = maxWidth.HasValue || maxHeight.HasValue;
                bool hasToRecode = format != src.Extension.Substring(1);

                if (!hasToResize && !hasToRecode)
                {
                    return(() => src.Retrieve());
                }

                // process and save to cache
                using (var stream = src.Retrieve())
                {
                    var image = hasToResize ? ResizeImage(stream, maxWidth, maxHeight, borders) : Image.FromStream(stream);
                    SaveImageToFile(image, cache.GetPath(filename), format);
                    image.Dispose();
                }

                return(() => new FileStream(cache.GetPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read));
            }
            catch (Exception ex)
            {
                Log.Warn(String.Format("Failed to process and cache image {0}", src.GetDebugName()), ex);
                return(null);
            }
        }
コード例 #2
0
ファイル: Images.cs プロジェクト: jpykie/MPExtended
        private static Stream StreamPostprocessedImage(ImageMediaSource src, int? maxWidth, int? maxHeight, string borders, string format)
        {
            if (!src.Exists)
            {
                Log.Info("Tried to stream image from non-existing source {0}", src.GetDebugName());
                WCFUtil.SetResponseCode(HttpStatusCode.NotFound);
                return Stream.Null;
            }

            if (borders != null && (!maxWidth.HasValue || !maxHeight.HasValue))
            {
                Log.Error("ResizeImage() called with a borders value but width or height is null");
                WCFUtil.SetResponseCode(HttpStatusCode.BadRequest);
                return Stream.Null;
            }

            if (format == null)
                format = src.Extension.Substring(1);

            // return from cache if possible
            string filename = String.Format("stream_{0}_{1}_{2}_{3}.{4}", src.GetUniqueIdentifier(), maxWidth, maxHeight, borders, format);
            if (cache.Contains(filename))
            {
                if (src.GetFileInfo().LastModifiedTime > cache.GetLastModifiedTime(cache.GetPath(filename)))
                {
                    cache.Invalidate(filename);
                }
                else
                {
                    WCFUtil.AddHeader(HttpResponseHeader.CacheControl, "public, max-age=5184000, s-maxage=5184000"); // not really sure why 2 months exactly
                    WCFUtil.SetContentType(GetMime(Path.GetExtension(filename)));
                    return new FileStream(cache.GetPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read);
                }
            }

            try
            {
                bool hasToResize = maxWidth.HasValue || maxHeight.HasValue;
                bool hasToRecode = format != src.Extension.Substring(1);

                if (!hasToResize && !hasToRecode)
                {
                    WCFUtil.AddHeader(HttpResponseHeader.CacheControl, "public, max-age=5184000, s-maxage=5184000");
                    WCFUtil.SetContentType(GetMime(src.Extension));
                    return src.Retrieve();
                }

                // save image to cache
                string path = cache.GetPath(String.Format("stream_{0}_{1}_{2}_{3}.{4}", src.GetUniqueIdentifier(), maxWidth, maxHeight, borders, format));
                using (var stream = src.Retrieve())
                {
                    var image = hasToResize ? ResizeImage(stream, maxWidth, maxHeight, borders) : Image.FromStream(stream);
                    SaveImageToFile(image, path, format);
                    image.Dispose();
                }

                // return image to client
                WCFUtil.AddHeader(HttpResponseHeader.CacheControl, "public, max-age=5184000, s-maxage=5184000");
                WCFUtil.SetContentType(GetCodecInfo(format).MimeType);
                return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch (Exception ex)
            {
                Log.Warn(String.Format("Failed to post-process and stream image {0}", src.GetDebugName()), ex);
                return Stream.Null;
            }
        }
コード例 #3
0
ファイル: Images.cs プロジェクト: StefanTischler/MPExtended
        private static Stream StreamPostprocessedImage(ImageMediaSource src, int?maxWidth, int?maxHeight, string borders, string format)
        {
            if (!src.Exists)
            {
                Log.Info("Tried to stream image from non-existing source {0}", src.GetDebugName());
                WCFUtil.SetResponseCode(HttpStatusCode.NotFound);
                return(Stream.Null);
            }

            if (borders != null && (!maxWidth.HasValue || !maxHeight.HasValue))
            {
                Log.Error("ResizeImage() called with a borders value but width or height is null");
                WCFUtil.SetResponseCode(HttpStatusCode.BadRequest);
                return(Stream.Null);
            }

            if (format == null)
            {
                format = src.Extension.Substring(1);
            }

            // return from cache if possible
            string filename = String.Format("stream_{0}_{1}_{2}_{3}.{4}", src.GetUniqueIdentifier(), maxWidth, maxHeight, borders, format);

            if (cache.Contains(filename))
            {
                if (src.GetFileInfo().LastModifiedTime > cache.GetLastModifiedTime(cache.GetPath(filename)))
                {
                    cache.Invalidate(filename);
                }
                else
                {
                    WCFUtil.AddHeader(HttpResponseHeader.CacheControl, "public, max-age=5184000, s-maxage=5184000"); // not really sure why 2 months exactly
                    WCFUtil.SetContentType(GetMime(Path.GetExtension(filename)));
                    return(new FileStream(cache.GetPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read));
                }
            }

            try
            {
                bool hasToResize = maxWidth.HasValue || maxHeight.HasValue;
                bool hasToRecode = format != src.Extension.Substring(1);

                if (!hasToResize && !hasToRecode)
                {
                    WCFUtil.AddHeader(HttpResponseHeader.CacheControl, "public, max-age=5184000, s-maxage=5184000");
                    WCFUtil.SetContentType(GetMime(src.Extension));
                    return(src.Retrieve());
                }

                // save image to cache
                string path = cache.GetPath(String.Format("stream_{0}_{1}_{2}_{3}.{4}", src.GetUniqueIdentifier(), maxWidth, maxHeight, borders, format));
                using (var stream = src.Retrieve())
                {
                    var image = hasToResize ? ResizeImage(stream, maxWidth, maxHeight, borders) : Image.FromStream(stream);
                    SaveImageToFile(image, path, format);
                    image.Dispose();
                }

                // return image to client
                WCFUtil.AddHeader(HttpResponseHeader.CacheControl, "public, max-age=5184000, s-maxage=5184000");
                WCFUtil.SetContentType(GetCodecInfo(format).MimeType);
                return(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read));
            }
            catch (Exception ex)
            {
                Log.Warn(String.Format("Failed to post-process and stream image {0}", src.GetDebugName()), ex);
                return(Stream.Null);
            }
        }
コード例 #4
0
ファイル: Images.cs プロジェクト: puenktchen/MPExtended
        private static Func<Stream> ProcessAndCacheImage(ImageMediaSource src, int? maxWidth, int? maxHeight, string borders, string format)
        {
            // if it's already in the cache, use that file
            string filename = String.Format("stream_{0}_{1}_{2}_{3}.{4}", src.GetUniqueIdentifier(), maxWidth, maxHeight, borders, format);
            if (cache.Contains(filename))
            {
                if (src.GetFileInfo().LastModifiedTime > cache.GetLastModifiedTime(cache.GetPath(filename)))
                {
                    cache.Invalidate(filename);
                }
                else
                {
                    return () => new FileStream(cache.GetPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read);
                }
            }

            // otherwise, resize if needed and save to cache
            try
            {
                bool hasToResize = maxWidth.HasValue || maxHeight.HasValue;
                bool hasToRecode = format != src.Extension.Substring(1);

                if (!hasToResize && !hasToRecode)
                    return () => src.Retrieve();

                // process and save to cache
                using (var stream = src.Retrieve())
                {
                    var image = hasToResize ? ResizeImage(stream, maxWidth, maxHeight, borders) : Image.FromStream(stream);
                    SaveImageToFile(image, cache.GetPath(filename), format);
                    image.Dispose();
                }

                return () => new FileStream(cache.GetPath(filename), FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch (Exception ex)
            {
                Log.Warn(String.Format("Failed to process and cache image {0}", src.GetDebugName()), ex);
                return null;
            }
        }