Sha256: a8000692d2b75b8b91d9090efc1655a665d0a3af5bd11e3f135eef4701258685

Contents?: true

Size: 1.49 KB

Versions: 52

Compression:

Stored size: 1.49 KB

Contents

require 'sprockets/asset'
require 'fileutils'
require 'zlib'

module Sprockets
  # `StaticAsset`s are used for files that are served verbatim without
  # any processing or concatenation. These are typical images and
  # other binary files.
  class StaticAsset < Asset
    # Returns file contents as its `source`.
    def source
      # File is read everytime to avoid memory bloat of large binary files
      pathname.open('rb') { |f| f.read }
    end

    # Implemented for Rack SendFile support.
    def to_path
      pathname.to_s
    end

    # Save asset to disk.
    def write_to(filename, options = {})
      # Gzip contents if filename has '.gz'
      options[:compress] ||= File.extname(filename) == '.gz'

      if options[:compress]
        # Open file and run it through `Zlib`
        pathname.open('rb') do |rd|
          File.open("#{filename}+", 'wb') do |wr|
            gz = Zlib::GzipWriter.new(wr, Zlib::BEST_COMPRESSION)
            buf = ""
            while rd.read(16384, buf)
              gz.write(buf)
            end
            gz.close
          end
        end
      else
        # If no compression needs to be done, we can just copy it into place.
        FileUtils.cp(pathname, "#{filename}+")
      end

      # Atomic write
      FileUtils.mv("#{filename}+", filename)

      # Set mtime correctly
      File.utime(mtime, mtime, filename)

      nil
    ensure
      # Ensure tmp file gets cleaned up
      FileUtils.rm("#{filename}+") if File.exist?("#{filename}+")
    end
  end
end

Version data entries

52 entries across 41 versions & 6 rubygems

Version Path
sprockets-2.1.4 lib/sprockets/static_asset.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-rolls-0.2.0 vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-0.8.3 vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-0.8.1 vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-rolls-0.1.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-rolls-0.1.0 vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-0.8.0.pre vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-0.7.1 vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-0.7.0 vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-0.7.0.pre2 vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-0.7.0.pre vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
challah-0.6.2 vendor/bundle/gems/sprockets-2.1.2/lib/sprockets/static_asset.rb
challah-0.6.2 vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/rails-uploader-0.0.1/vendor/bundle/ruby/1.9.1/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
initforthe-cookies-0.0.1 vendor/bundle/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/rails-uploader-0.0.1/vendor/bundle/ruby/1.9.1/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/sprockets-2.1.3/lib/sprockets/static_asset.rb