diff options
author | Tobias Bühlmann <tobias.buehlmann@gmx.de> | 2010-04-25 13:46:42 +0200 |
---|---|---|
committer | Tobias Bühlmann <tobias.buehlmann@gmx.de> | 2010-04-25 13:46:42 +0200 |
commit | d4ccdf736f536b420aceeb6cb8589495cd2e4a34 (patch) | |
tree | e6d5120836028ae2bbd4b96d3d64f1bb2f7c4e83 /file_upload.rb |
initial commit
Diffstat (limited to 'file_upload.rb')
-rw-r--r-- | file_upload.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/file_upload.rb b/file_upload.rb new file mode 100644 index 0000000..b9a6d6d --- /dev/null +++ b/file_upload.rb @@ -0,0 +1,54 @@ +# encoding: utf-8 + +require 'rubygems' +require 'haml' +require 'pathname' +require 'sinatra/base' + +class FileUpload < Sinatra::Base + configure do + enable :static + enable :sessions + + set :views, Pathname.new(__FILE__).dirname.join('views').expand_path + set :public, Pathname.new(__FILE__).dirname.join('public').expand_path + end + + helpers do + def flash(message = '') + session[:flash] = message + end + end + + not_found do + haml '404' + end + + error do + haml "Error (#{request.env['sinatra.error']})" + end + + get '/' do + @files = settings.public.join('files').entries - [Pathname('.'), Pathname('..')] + + @flash = session[:flash] + session[:flash] = nil + + haml :index + end + + post '/upload' do + if params[:file] + filename = params[:file][:filename] + file = params[:file][:tempfile] + + File.open(settings.public.join('files', filename), 'wb') {|f| f.write file.read } + + flash 'Uploaded successfully' + else + flash 'You have to choose a file' + end + + redirect '/' + end +end |