diff options
-rw-r--r-- | config.ru | 2 | ||||
-rw-r--r-- | file_upload.rb | 31 | ||||
-rw-r--r-- | views/index.haml | 6 | ||||
-rw-r--r-- | views/layout.haml | 1 |
4 files changed, 23 insertions, 17 deletions
@@ -1,3 +1,5 @@ +$LOAD_PATH.unshift(File.dirname(__FILE__)) require 'file_upload' run FileUpload + diff --git a/file_upload.rb b/file_upload.rb index aab6b4e..a23d6a1 100644 --- a/file_upload.rb +++ b/file_upload.rb @@ -2,38 +2,38 @@ 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 + + set :views, File.join(File.dirname(__FILE__), 'views') + set :public, File.join(File.dirname(__FILE__), 'public') + set :files, File.join(public, 'files') 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.new('.'), Pathname.new('..')] - + @files = Dir.entries(settings.files) - ['.', '..'] + @flash = session[:flash] session[:flash] = nil - + haml :index end @@ -41,14 +41,15 @@ class FileUpload < Sinatra::Base if params[:file] filename = params[:file][:filename] file = params[:file][:tempfile] - - File.open(settings.public.join('files', filename), 'wb') {|f| f.write file.read } - + + File.open(File.join(settings.files, filename), 'wb') {|f| f.write file.read } + flash 'Uploaded successfully' else flash 'You have to choose a file' end - + redirect '/' end end + diff --git a/views/index.haml b/views/index.haml index 51e4781..b9d78e7 100644 --- a/views/index.haml +++ b/views/index.haml @@ -10,5 +10,7 @@ %input{:type => 'submit', :value => 'Upload'} %h3 Files -- @files.each do |file| - = "<a href='files/#{file}'>#{file}</a>" +%ul + - @files.each do |file| + %li= "<a href='files/#{file}'>#{file}</a>" + diff --git a/views/layout.haml b/views/layout.haml index 1e37026..d8eb52c 100644 --- a/views/layout.haml +++ b/views/layout.haml @@ -6,3 +6,4 @@ %meta{:content => 'text/html; charset=utf-8', :'http-equiv' => 'Content-Type'} %body = yield + |