Sha256: b67c806abc2fabafa2f51be5a7230e2c8600d81e619714c13607bf33e9c77c30

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require 'base64'
module WebResourceBundler
  module Filters
    module ImageEncodeFilter
      #ImageData contains info about image found in css files
    	class ImageData
    		MAX_RAND_FOR_ID = 10000
    		attr_reader :extension, :id, :path, :exist, :url

    		def initialize(url, folder)
          @url = url
          #computing absolute file path using folder of css file
    			@path = File.join(folder, url) 
          if File.file?(@path)
    			  @exist = true 
          else 
            @exist = false
          end
          if WebResourceBundler::Bundler.instance.logger and !@path.include?('://') and !@exist
            WebResourceBundler::Bundler.instance.logger.info("Image not found #{@path}")
          end
    			if @exist
    				@size = File.size(@path)
    				name, @extension = File.basename(@path).split('.')
    				#id is a filename plus random number - to provide uniqueness
    				@id = name + rand(MAX_RAND_FOR_ID).to_s
    			end
    		end

        def size
          @size / 1024
        end

    		#constructs part of css header with data for current image
    		def construct_mhtml_image_data(separator)
    			if @exist
    				result = separator + "\n"
    				result << 'Content-Location:' << @id << "\n"
    				result <<	'Content-Transfer-Encoding:base64' << "\n\n"
    				result << encoded << "\n\n"
    			end
    		end

    		def encoded
    			return nil unless @exist
    			Base64.encode64(File.read(@path)).gsub("\n", '')
    		end
    	end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
web_resource_bundler-0.0.19 lib/web_resource_bundler/filters/image_encode_filter/image_data.rb
web_resource_bundler-0.0.18 lib/web_resource_bundler/filters/image_encode_filter/image_data.rb
web_resource_bundler-0.0.16 lib/web_resource_bundler/filters/image_encode_filter/image_data.rb
web_resource_bundler-0.0.15 lib/web_resource_bundler/filters/image_encode_filter/image_data.rb