Module: Sinatra::RbbtRESTFileServer

Defined in:
lib/rbbt/rest/file_server.rb

Class Method Summary collapse

Class Method Details

.registered(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbbt/rest/file_server.rb', line 7

def self.registered(base)
  base.module_eval do

    get '/resource/:resource/get_directory' do
      directory, resource, create = params.values_at :directory, :resource, :create

      create = true unless create.nil? or create.empty? or %w(no false).include? create.downcase

      raise "The Rbbt resource may not be used since it has access to seccurity sensible files" if resource == "Rbbt"

      resource = Kernel.const_get(resource)

      directory = $1 if resource.subdir and directory =~ /^#{resource.subdir}\/?(.*)/

      path = resource.root[directory]

      raise "For security reasons the file path must not leave the resource root directory" unless Misc.path_relative_to(resource.root, path)

      Log.debug{"Serving resource: #{[resource, directory, path, path.find] * " | "}"}

      if create
        raise "Directory does not exist and cannot be created" unless path.exists?
      else
        raise "Directory does not exist" unless Open.exists? path.find
      end

      headers['Content-Encoding'] = 'gzip'
      stream do |out|
        tar = Misc.tarize(path.find)
        begin
          while chunk = tar.read(8192)
            break if out.closed?
            out << chunk
          end
        ensure
          tar.close
        end
        out.flush
      end
    end

    get '/resource/:resource/get_file' do
      file, resource, create = params.values_at :file, :resource, :create

      create = true unless create.nil? or create.empty? or %w(no false).include? create.downcase

      raise "The Rbbt resource may not be used since it has access to seccurity sensible files" if resource == "Rbbt"

      resource = Kernel.const_get(resource)

      file = $1 if Resource === resource and resource.subdir and file =~ /^#{resource.subdir}\/?(.*)/

      path = resource.root[file]

      raise "For security reasons the file path must not leave the resource root directory" unless Misc.path_relative_to(resource.root, path)

      Log.debug{"Resource: #{[resource, file, path, path.find] * " | "}"}

      raise "File does not exist and can not create it" unless path.exists?

      directory_url = File.join("/resource", resource.to_s , 'get_directory') << '?' << "create=#{create}" << '&' << "directory=#{file}"
      redirect to(directory_url) if path.directory?

      send_file path.find, :filename => path.find
    end

  end
end