public OutputContent GetContent(ICacheProvider cacheProvider)
        {
            var upgraded = false;

            _lock.EnterUpgradeableReadLock();

            try
            {
                // check the cache first
                var cachedValue = cacheProvider.Get <OutputContent>(GetCacheKeyFor(RequestFor));

                if (cachedValue == null)
                {
                    _lock.EnterWriteLock();
                    upgraded = true;

                    var content = this.Select(f => f.GetContent(cacheProvider));

                    var minificationProvider = MinificationRegistry.Get(ResourceType);

                    Log.WriteLine("Combining content for '{0}'", RequestFor);

                    // Combine all of the files into one string
                    var minifiedContent = minificationProvider.Combine(content.Select(x => x.Content));
                    cachedValue = new OutputContent
                    {
                        Content            = minifiedContent,
                        ContentHash        = minifiedContent.HashArray(),
                        AllowClientCaching = ContainsPlaceHolder || CacheFor.HasValue,
                        CacheFor           = CacheFor,
                        ContentType        = minificationProvider.GetContentType(RequestFor),
                        LastModifiedDate   = content.Max(x => x.LastModifiedDate)
                    };

                    cacheProvider.Add(GetCacheKeyFor(RequestFor), cachedValue, GetDependencies());
                }

                return(cachedValue);
            }
            finally
            {
                if (upgraded)
                {
                    _lock.ExitWriteLock();
                }

                _lock.ExitUpgradeableReadLock();
            }
        }
示例#2
0
        public OutputContent GetContent(ICacheProvider cacheProvider, Mode mode)
        {
            var upgraded = false;

            _lock.EnterUpgradeableReadLock();

            try
            {
                // check the cache first
                var returnValue = cacheProvider.Get <OutputContent>(GetCacheKeyFor(VirtualPath, mode));

                if (returnValue == null)
                {
                    Log.WriteLine("Generating Content For '{0}'", VirtualPath);

                    _lock.EnterWriteLock();
                    upgraded = true;

                    byte[] rejuicedValue = null;

                    var file = new FileInfo(PhysicalPath);

                    // clear existing dependencies
                    _dependencies.Clear();

                    var minificationProvider = MinificationRegistry.Get(ResourceType);

                    var notFound = false;
                    try
                    {
                        var fileBytes = File.ReadAllBytes(PhysicalPath);

                        if (fileBytes.Length > 2 && fileBytes[0] == 0xEF && fileBytes[1] == 0xBB && fileBytes[2] == 0xBF)
                        {
                            fileBytes = fileBytes.Skip(3).ToArray();
                        }

                        rejuicedValue = FileTransformationPipeline.TransformInputFile(this, fileBytes);
                    }
                    catch (IOException)
                    {
                    }

                    // Combined value
                    var combinedValue = new OutputContent
                    {
                        Content            = rejuicedValue,
                        ContentHash        = rejuicedValue.HashArray(),
                        AllowClientCaching = false,
                        ContentType        =
                            ResourceType == ResourceType.Css ? "text/css" : "text/javascript",
                        LastModifiedDate = file.LastWriteTimeUtc
                    };

                    var dependencies = GetDependencies();
                    cacheProvider.Add(GetCacheKeyFor(VirtualPath, Mode.Combine), combinedValue, dependencies);
                    returnValue = combinedValue;

                    if (Mode == Mode.Minify && !notFound)
                    {
                        Log.WriteLine("Minifying Content For '{0}'", VirtualPath);

                        // Minified value
                        byte[] minifiedContent = null;
                        try
                        {
                            minifiedContent = minificationProvider.Minify(rejuicedValue);
                        }
                        catch (Exception e)
                        {
                            // Yes, catching Exception is bad. However, anyone can plug in their own minification provider
                            // and throw any exception they want. We want to make sure that exceptions thrown by rejuicer
                            // have the filename inside them. So we just wrap the exception here & throw the wrapped exception.
                            throw new InvalidOperationException(string.Format("Encountered exception trying minify invalid JavaScript for file '{0}'.", VirtualPath), e);
                        }

                        var minifiedValue = new OutputContent
                        {
                            Content            = minifiedContent,
                            ContentHash        = minifiedContent.HashArray(),
                            AllowClientCaching = false,
                            ContentType        =
                                ResourceType == ResourceType.Css
                                                            ? "text/css"
                                                            : "text/javascript",
                            LastModifiedDate = file.LastWriteTimeUtc
                        };

                        cacheProvider.Add(GetCacheKeyFor(VirtualPath, mode), minifiedValue, dependencies);

                        returnValue = minifiedValue;
                    }
                }

                return(returnValue);
            }
            finally
            {
                if (upgraded)
                {
                    _lock.ExitWriteLock();
                }

                _lock.ExitUpgradeableReadLock();
            }
        }