Class: GV::Valley::FileServer::Api

Inherits:
Goliath::API
  • Object
show all
Defined in:
lib/gv/valley/file_server.rb

Instance Method Summary collapse

Instance Method Details

#on_body(env, data) ⇒ Object



42
43
44
# File 'lib/gv/valley/file_server.rb', line 42

def on_body(env, data)
  (env['async-body'] ||= '') << data
end

#on_headers(env, headers) ⇒ Object



38
39
40
# File 'lib/gv/valley/file_server.rb', line 38

def on_headers(env, headers)
  env['async-headers'] = headers
end

#response(env) ⇒ Object



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
78
79
80
81
# File 'lib/gv/valley/file_server.rb', line 46

def response(env)
    
  path = "#{ENV['GV_HOME']}/#{env['REQUEST_PATH']}"
    
  case env['REQUEST_METHOD']
      
  when 'GET'
    
    headers = {'X-filename' => path}

    raise Goliath::Validation::NotFoundError unless File.file?(path)

    operation = proc do
      FileSystem.new(path).get { |chunk| env.chunked_stream_send(chunk) }
    end

    callback = proc do |result|
      env.chunked_stream_close
    end

    EM.defer operation, callback

    headers.merge!( 'X-Stream' => 'Goliath')
    chunked_streaming_response(200, headers)
      
  when 'PUT'
    
    File.delete(path) rescue nil
    result = File.open(path, File::RDWR|File::CREAT){ |f| f.puts env['async-body'] }
    [ 200, {}, {body: "OK"} ]        
    
  when 'DELETE'  
    result = File.delete(path)
    [ 200, {}, {body: result } ]                
  end
end