Example #1
0
 private bool IsExistsOnClient(WebResource resource)
 {
     var etag = Context.Request.Headers["If-None-Match"];
     return !String.IsNullOrEmpty(etag) && String.Equals(resource.ETag, etag);
 }
Example #2
0
        public WebResource GetResource(WebResourceDescriptor descriptor)
        {
            if (descriptor == null)
            {
                throw new ArgumentNullException("descriptor");
            }

            try
            {
                var key = descriptor.GenerateCacheKey(compressor.EncodingType);

                syncRoot.EnterUpgradeableReadLock();

                var resource = context.Cache[key] as WebResource;

                if (resource == null)
                {
                    var group = WebResources.Scripts[descriptor.Name];

                    if (group == null)
                    {
                        throw new WebResourceLocatorException();
                    }

                    if (!group.Combined)
                    {
                        throw new WebResourceLocatorException();
                    }

                    try
                    {
                        syncRoot.EnterWriteLock();

                        var path = Path.GetTempFileName();

                        using (var writer = new StreamWriter(compressor.CreateOutputStream(File.Create(path))))
                        {
                            foreach (var item in group.Items)
                            {
                                var provider = GetResourceProvider(item.Source, group.DefaultPath ?? WebResources.ScriptFilesPath);

                                trace.TraceInformation("Source: {0}", item.Source);

                                writer.WriteLine();
                                writer.WriteLine("/* Web Resource Locator: {0} */", DateTime.Now);
                                writer.WriteLine("/* {0} */", item.Source);

                                using (var reader = new StreamReader(provider.GetResourceStream(item.Source)))
                                {
                                    int count;
                                    var buffer = new char[4096];

                                    while ((count = reader.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        writer.Write(buffer, 0, count);
                                    }
                                }

                                writer.WriteLine();
                            }

                            resource = new WebResource(path)
                                           {
                                               ContentType = "application/x-javascript",
                                               Version = group.Version
                                           };
                        }

                        context.Cache.Add(key,
                                          resource,
                                          new CacheDependency(path),
                                          group.CacheDuration > TimeSpan.Zero
                                              ? DateTime.Now + group.CacheDuration
                                              : Cache.NoAbsoluteExpiration,
                                          Cache.NoSlidingExpiration,
                                          CacheItemPriority.Normal,
                                          null);
                    }
                    finally
                    {
                        syncRoot.ExitWriteLock();
                    }
                }

                return resource;
            }
            finally
            {
                syncRoot.ExitUpgradeableReadLock();
            }
        }