summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Bühlmann <tobias.buehlmann@gmx.de>2010-04-25 13:46:42 +0200
committerTobias Bühlmann <tobias.buehlmann@gmx.de>2010-04-25 13:46:42 +0200
commitd4ccdf736f536b420aceeb6cb8589495cd2e4a34 (patch)
treee6d5120836028ae2bbd4b96d3d64f1bb2f7c4e83
initial commit
-rw-r--r--Gemfile4
-rw-r--r--README.md5
-rw-r--r--config.ru3
-rw-r--r--file_upload.rb54
-rw-r--r--public/css/style.css11
-rw-r--r--views/index.haml14
-rw-r--r--views/layout.haml8
7 files changed, 99 insertions, 0 deletions
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|
+ = "<a href='files/#{file}'>#{file}</a>"
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