Exemplo n.º 1
0
        public void Put(Stream stream, string urlPath, SvgMagicOptions options)
        {
            if (stream.CanSeek)
            {
                stream.Seek(0, SeekOrigin.Begin);
            }

            if (stream.CanRead)
            {
                var physicalPath = Path.Combine(_basePath, urlPath.TrimStart('/'));
                if (!Directory.Exists(physicalPath))
                {
                    Directory.CreateDirectory(physicalPath);
                }

                var fileName   = urlPath.Split('/').LastOrDefault();
                var extension  = fileName.Split('.').LastOrDefault();
                var cachedFile = Path.Combine(physicalPath, fileName.Replace(extension, options.Extension));

                using (var fs = new FileStream(cachedFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    stream.CopyTo(fs);
                    fs.Flush();
                    fs.Close();
                }
                if (stream.CanSeek)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                }
            }
        }
            protected override void Context()
            {
                SetupUserAgentStringForRequest("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)");
                _imageCache.Setup(s => s.Put(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <SvgMagicOptions>())).Verifiable();

                _options = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());
            }
            protected override void Context()
            {
                _imageCache.Setup(s => s.Put(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <SvgMagicOptions>())).Verifiable();

                _options = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());

                _request.SetupGet(s => s.CurrentExecutionFilePathExtension).Returns(".aspx");
            }
            protected override void Context()
            {
                _imageCache.Setup(s => s.Put(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <SvgMagicOptions>())).Verifiable();

                _options = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());

                _handler.ResourceExistsFunc = s => false;
            }
 protected override bool NoSvgSupport(SvgMagicOptions options, HttpBrowserCapabilitiesBase browser)
 {
     if (NoSvgSupportFunc != null)
     {
         return(NoSvgSupportFunc(options, browser));
     }
     return(base.NoSvgSupport(options, browser));
 }
            protected override void Context()
            {
                _imageCache.Setup(s => s.Put(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <SvgMagicOptions>())).Verifiable();

                _queryString.Add("force", "true");
                _queryString.Add("format", "bmp");

                _options = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());
            }
        protected override void SharedContext()
        {
            _fileSystem = CreateDependency <IFileSystem>();

            _imageCache = new FileSystemImageCache(_fileSystem.Object);

            _queryString = new NameValueCollection();
            _options     = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());
        }
        public Stream Get(string urlPath, SvgMagicOptions options)
        {
            var cachedFile = GetCacheFilePathAndName(urlPath, options);

            if (_fileSystem.Exists(cachedFile))
            {
                return(_fileSystem.Open(cachedFile));
            }

            return(null);
        }
            protected override void Context()
            {
                _imageCache.Setup(s => s.Put(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <SvgMagicOptions>())).Verifiable();

                _options = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());

                _browserCacheDate = DateTime.Now;
                _resourceDate     = _browserCacheDate.AddSeconds(-1);

                _headers.Add("If-Modified-Since", _browserCacheDate.ToString("R"));
                _handler.GetResourceUpdateDateTimeFunc = s => _resourceDate;
            }
            protected override void Context()
            {
                _queryString = new NameValueCollection()
                {
                    { "height", "100" },
                    { "width", "100" }
                };

                _options = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());

                _urlPath      = "images/myimage.svg";
                _cacheUrlPath = "images/myimage.svg/myimage.png";
            }
            protected override void Context()
            {
                _browserCapMock = CreateDependency <HttpBrowserCapabilitiesBase>();

                _browserCapMock.SetupGet(s => s.Browser).Returns("Android");
                _browserCapMock.SetupGet(s => s.MajorVersion).Returns(4);
                _browserCapMock.SetupGet(s => s.MinorVersion).Returns(4);
                _request.SetupGet(s => s.Browser).Returns(_browserCapMock.Object);

                _imageCache.Setup(s => s.Put(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <SvgMagicOptions>())).Verifiable();

                _options = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());
            }
            protected override void Context()
            {
                _imageCache.Setup(s => s.Put(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <SvgMagicOptions>())).Callback(
                    (Stream s, string path, SvgMagicOptions options) =>
                {
                    var bitmap = new Bitmap(s);
                    _size      = bitmap.Size;
                }).Verifiable();

                _queryString.Add("force", "true");
                _queryString.Add("format", "png");
                _queryString.Add("height", "1500");

                _options = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());
            }
        private static string BuildFileName(string fileName, SvgMagicOptions options)
        {
            var cacheFileName = fileName;

            if (options.Width > 0)
            {
                cacheFileName = string.Format("w{0}_{1}", options.Width, cacheFileName);
            }

            if (options.Height > 0)
            {
                cacheFileName = string.Format("h{0}_{1}", options.Height, cacheFileName);
            }

            return(cacheFileName);
        }
            protected override void Context()
            {
                _request.SetupGet(s => s.CurrentExecutionFilePath).Returns("scotland.svg");
                _request.Setup(s => s.MapPath(It.IsAny <string>())).Returns("scotland.svg");

                _imageCache.Setup(s => s.Put(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <SvgMagicOptions>())).Verifiable();

                _options = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());

                _imageCache.Setup(s => s.Get(It.IsAny <string>(), It.IsAny <SvgMagicOptions>()))
                .Returns(GetSampleImageStream());

                _imageCache.Setup(s => s.GetCacheItemModifiedDateTime(It.IsAny <string>(), It.IsAny <SvgMagicOptions>()))
                .Returns(DateTime.Now.AddDays(-1));

                _handler.NoSvgSupportFunc = (options, @base) => true;
            }
            protected override void Context()
            {
                _queryString = new NameValueCollection()
                {
                    { "height", "100" },
                    { "width", "100" }
                };

                _options = SvgMagicOptions.Parse(_queryString, new SvgMagicHandlerConfigurationSection());

                _existingUrlPath      = "images/myimage.svg";
                _existingCacheUrlPath = "images/myimage.svg/h100_w100_myimage.png";

                _fileSystem.Setup(s => s.Exists(_existingUrlPath)).Returns(true);
                _fileSystem.Setup(s => s.Exists(_existingCacheUrlPath)).Returns(true);
                _fileSystem.Setup(s => s.Open(It.Is <string>(u => u == _existingCacheUrlPath))).Returns(new MemoryStream()).Verifiable();
            }
        public void Put(Stream stream, string urlPath, SvgMagicOptions options)
        {
            if (stream.CanSeek)
            {
                stream.Seek(0, SeekOrigin.Begin);
            }

            if (stream.CanRead)
            {
                var cachedFile = GetCacheFilePathAndName(urlPath, options);

                _fileSystem.Save(stream, cachedFile);

                if (stream.CanSeek)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                }
            }
        }
        private string GetCacheFilePathAndName(string urlPath, SvgMagicOptions options)
        {
            var physicalPath = Path.Combine(_basePath, urlPath.TrimStart('/')).Replace('\\', '/');

            if (!_fileSystem.Exists(physicalPath))
            {
                _fileSystem.Create(physicalPath);
            }

            var fileName = urlPath.Split('/').LastOrDefault();

            fileName = BuildFileName(fileName, options);
            var extension = fileName.Split('.').LastOrDefault();

            var cachedFile = Path.Combine(physicalPath, fileName.Replace(extension, options.Extension));

            cachedFile = cachedFile.Replace('\\', '/');

            return(cachedFile);
        }
Exemplo n.º 18
0
        public Stream Get(string urlPath, SvgMagicOptions options)
        {
            var physicalPath = Path.Combine(_basePath, urlPath.TrimStart('/'));

            if (!Directory.Exists(physicalPath))
            {
                return(null);
            }

            var fileName  = urlPath.Split('/').LastOrDefault();
            var extension = fileName.Split('.').LastOrDefault();

            var cachedFile = Path.Combine(physicalPath, fileName.Replace(extension, options.Extension));

            if (File.Exists(cachedFile))
            {
                return(File.Open(cachedFile, FileMode.Open, FileAccess.Read, FileShare.Read));
            }

            return(null);
        }
        public DateTime GetCacheItemModifiedDateTime(string urlPath, SvgMagicOptions options)
        {
            var cachedFile = GetCacheFilePathAndName(urlPath, options);

            return(_fileSystem.GetModificationDateTime(cachedFile));
        }