Sha256: baeabc0b5b8186ae9d44f1dd7c5a4be9b62fc6343a1d5546249cc90446749265

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 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.logger and !@path.include?('://') and !@exist
            WebResourceBundler::Bundler.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 support 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

1 entries across 1 versions & 1 rubygems

Version Path
web_resource_bundler-0.0.13 lib/web_resource_bundler/filters/image_encode_filter/image_data.rb