class Sprockets::StaticAsset
other binary files.
any processing or concatenation. These are typical images and
`StaticAsset`s are used for files that are served verbatim without
def self.serialized_attributes
def self.serialized_attributes super + %w( content_type mtime length digest ) end
def body
def body # File is read everytime to avoid memory bloat of large binary files pathname.open('rb') { |f| f.read } end
def fresh?
Checks if Asset is fresh by comparing the actual mtime and
def fresh? # Check current mtime and digest dependency_fresh?('path' => pathname, 'mtime' => mtime, 'hexdigest' => digest) end
def initialize(environment, logical_path, pathname, digest = nil)
def initialize(environment, logical_path, pathname, digest = nil) super(environment, logical_path, pathname) @digest = digest load! end
def load!
def load! content_type mtime length digest end
def to_path
def to_path pathname.to_s end
def to_s
def to_s body end
def write_to(filename, options = {})
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