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
75
76
77
# 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] * " | "}")

      raise "Directory does not exist" unless path.exists? or create
      raise "Directory does not exist and can not create it" unless path.exists? or path.produce.exists?

      #stream do |out|
      #  io = nil
      #  Misc.in_dir path.find do
      #    io = CMD.cmd("tar cfvz - '.'", :pipe => true)
      #  end
      #  while not io.closed? and block = io.read(4096) 
      #    out << block
      #  end
      #  io.close
      #  out.flush
      #end
      
      TmpFile.with_file :extension => 'tar.gz' do |file|
        Misc.in_dir path.find do
          CMD.cmd("tar cfvz '#{file}' .") 
        end
        headers['Content-Encoding'] = 'gzip'
        send_file file, :filename => directory.gsub('/','_') + '.tar.gz', :type => "application/x-gzip"
      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.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
    end

  end
end