From d4ccdf736f536b420aceeb6cb8589495cd2e4a34 Mon Sep 17 00:00:00 2001 From: Tobias Bühlmann Date: Sun, 25 Apr 2010 13:46:42 +0200 Subject: initial commit --- Gemfile | 4 ++++ README.md | 5 +++++ config.ru | 3 +++ file_upload.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ public/css/style.css | 11 +++++++++++ views/index.haml | 14 ++++++++++++++ views/layout.haml | 8 ++++++++ 7 files changed, 99 insertions(+) create mode 100644 Gemfile create mode 100644 README.md create mode 100644 config.ru create mode 100644 file_upload.rb create mode 100644 public/css/style.css create mode 100644 views/index.haml create mode 100644 views/layout.haml diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..45cc5f4 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'http://rubygems.org/' + +gem 'haml' +gem 'sinatra', '>= 1.0' diff --git a/README.md b/README.md new file mode 100644 index 0000000..8976cd6 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +A simple Sinatra file upload application + +Starting: + +`$ rackup config.ru` diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..25c06c2 --- /dev/null +++ b/config.ru @@ -0,0 +1,3 @@ +require 'file_upload' + +run FileUpload 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 diff --git a/public/css/style.css b/public/css/style.css new file mode 100644 index 0000000..7a1f67e --- /dev/null +++ b/public/css/style.css @@ -0,0 +1,11 @@ +body { + background-color: #5CACEE; +} + +a { + color: white; +} + +a:hover { + color: black; +} diff --git a/views/index.haml b/views/index.haml new file mode 100644 index 0000000..51e4781 --- /dev/null +++ b/views/index.haml @@ -0,0 +1,14 @@ +%h1 Sinatra File Upload + +- if @flash + = @flash + +%form{:action => '/upload', :method => 'post', :enctype => 'multipart/form-data'} + %p< + %input{:type => 'file', :name => 'file'} + %p< + %input{:type => 'submit', :value => 'Upload'} + +%h3 Files +- @files.each do |file| + = "#{file}" diff --git a/views/layout.haml b/views/layout.haml new file mode 100644 index 0000000..1e37026 --- /dev/null +++ b/views/layout.haml @@ -0,0 +1,8 @@ +!!! +%html{:xmlns => 'http://www.w3.org/1999/xhtml'} + %head + %title Sinatra File Upload + %link{:rel => 'stylesheet', :type => 'text/css', :href => 'css/style.css'} + %meta{:content => 'text/html; charset=utf-8', :'http-equiv' => 'Content-Type'} + %body + = yield -- cgit v1.2.3