Sha256: 6e71f6f46f1ea782815071dd443bf321fb18c4f3d19d724088ff64593f9a0aeb

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Roadie
  # Asset provider that looks for files on your local filesystem.
  #
  # It will be locked to a specific path and it will not access files above
  # that directory.
  class FilesystemProvider
    # Raised when FilesystemProvider is asked to access a file that lies above
    # the base path.
    class InsecurePathError < Error; end

    attr_reader :path

    def initialize(path = Dir.pwd)
      @path = path
    end

    # @return [Stylesheet, nil]
    def find_stylesheet(name)
      file_path = build_file_path(name)
      if File.exist? file_path
        Stylesheet.new file_path, File.read(file_path)
      end
    end

    # @raise InsecurePathError
    # @return [Stylesheet]
    def find_stylesheet!(name)
      file_path = build_file_path(name)
      if File.exist? file_path
        Stylesheet.new file_path, File.read(file_path)
      else
        basename = File.basename file_path
        raise CssNotFound.new(
          css_name: basename,
          message: %{#{file_path} does not exist. (Original name was "#{name}")},
          provider: self
        )
      end
    end

    def to_s
      inspect
    end

    def inspect
      "#<#{self.class} #{@path}>"
    end

    private

    def build_file_path(name)
      raise InsecurePathError, name if name.include?("..")
      File.join(@path, name[/^([^?]+)/])
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
roadie-5.2.1 lib/roadie/filesystem_provider.rb
roadie-5.2.0 lib/roadie/filesystem_provider.rb
roadie-5.1.0 lib/roadie/filesystem_provider.rb
roadie-5.0.1 lib/roadie/filesystem_provider.rb
roadie-5.0.0 lib/roadie/filesystem_provider.rb