Sha256: 762843428ca5852fd9b63b1aa4ed691ca8401a9adbdd92d59eccf4c2bab1ab17

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

module WebResourceBundler::Filters::CdnFilter
  class Filter < WebResourceBundler::Filters::BaseFilter

		FILE_PREFIX				= 'cdn_'
		IMAGE_URL_PATTERN	= /url\s*\(['|"]?([^\)'"]+\.(jpg|gif|png|jpeg|bmp))['|"]?\)/i

    def initialize(settings, file_manager)
      super(settings, file_manager)
    end

    def apply!(block_data)
      block_data.styles.each do |file| 
        rewrite_content_urls!(file.path, file.content) unless file.content.empty? 
        file.path = new_filepath(file.path)
      end
      block_data
    end

		private

		def new_filepath(path)
      @file_manager.cache_path_with_prefix(FILE_PREFIX, path)
    end

    #insures that image linked to one particular host  
		#host type depends on request protocol type
    def host_for_image(image_url)
      hosts = get_hosts
      index = image_url.hash % hosts.size
      hosts[index]
    end

		def get_hosts
			key = @settings[:protocol] == 'https' ? :https_hosts : :http_hosts
			@settings[key]
		end

		#rewrites image urls excluding mhtml and base64 urls
    def rewrite_content_urls!(file_path, content)
      content.gsub!(IMAGE_URL_PATTERN) do |s|
				url = WebResourceBundler::CssUrlRewriter.rewrite_relative_path(file_path, $1)
				host = host_for_image(url)
				s = url_css_tag(host, url) 
      end
    end

		def url_css_tag(host, url)
			"url('#{File.join(host, url)}')"
		end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
web_resource_bundler-0.0.23 lib/web_resource_bundler/filters/cdn_filter.rb
web_resource_bundler-0.0.22 lib/web_resource_bundler/filters/cdn_filter.rb