Sha256: 41b26024bd19c227362777db6966bce9693687c00df70ddc54bbbfe4085f3c64

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

# encoding: UTF-8

module Spontaneous::Storage
  class Local < Backend
    attr_reader :root

    def initialize(root_directory, url_path, accepts = nil)
      @root, @url_path, @accepts = ::File.expand_path(root_directory), url_path, accepts
    end

    def copy(existing_file, media_path, headers = {})
      dest_path = create_absolute_path(media_path)
      copy_file(existing_file, dest_path)
      set_permissions(dest_path)
      dest_path
    end

    def copy_file(existing_file, dest_path)
      if existing_file.respond_to?(:read)
        # Re-open the file because it's been modified on disk by the optimisation process
        # and if we don't re-open it the copy will take the unmodified version
        File.open(existing_file.path, "rb") do |src|
          src.binmode
          File.open(dest_path, "wb") do |f|
            f.binmode
            while chunk = src.read(8192)
              f.write(chunk)
            end
          end
        end
      else
        FileUtils.copy_file(existing_file, dest_path)
      end
    end

    def open(relative_path, headers, mode, &block)
      dest_path = create_absolute_path(relative_path)
      File.open(dest_path, mode) do |f|
        f.binmode
        block.call(f)
      end
      set_permissions(dest_path)
    end

    def create_absolute_path(relative_path)
      absolute_path = File.join(root, join_path(relative_path))
      absolute_dir = File.dirname(absolute_path)
      FileUtils.mkdir_p(absolute_dir) unless File.exist?(absolute_dir)
      absolute_path
    end

    def join_path(path)
      File.join(*path)
    end

    def public_url(path)
      File.join(@url_path, join_path(path))
    end

    def local?
      true
    end

    def set_permissions(filepath)
      File.chmod(0644, filepath)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta4 lib/spontaneous/storage/local.rb
spontaneous-0.2.0.beta3 lib/spontaneous/storage/local.rb
spontaneous-0.2.0.beta2 lib/spontaneous/storage/local.rb