1
# frozen_string_literal: true
2

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

13 5
    attr_reader :path
14

15 5
    def initialize(path = Dir.pwd)
16 5
      @path = path
17
    end
18

19
    # @return [Stylesheet, nil]
20 5
    def find_stylesheet(name)
21 5
      file_path = build_file_path(name)
22 5
      if File.exist? file_path
23 5
        Stylesheet.new file_path, File.read(file_path)
24
      end
25
    end
26

27
    # @raise InsecurePathError
28
    # @return [Stylesheet]
29 5
    def find_stylesheet!(name)
30 5
      file_path = build_file_path(name)
31 5
      if File.exist? file_path
32 5
        Stylesheet.new file_path, File.read(file_path)
33
      else
34 5
        basename = File.basename file_path
35 5
        raise CssNotFound.new(basename, %{#{file_path} does not exist. (Original name was "#{name}")}, self)
36
      end
37
    end
38

39 5
    def to_s() inspect end
40 5
    def inspect() "#<#{self.class} #@path>" end
41

42 5
    private
43 5
    def build_file_path(name)
44 5
      raise InsecurePathError, name if name.include?("..")
45 5
      File.join(@path, name[/^([^?]+)/])
46
    end
47
  end
48
end

Read our documentation on viewing source code .

Loading