summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortbuehlmann <tobias.buehlmann@gmx.de>2013-02-03 09:30:16 +0100
committertbuehlmann <tobias.buehlmann@gmx.de>2013-02-03 09:30:16 +0100
commit98bfd0502409f5b41975aae224d05296c0ea173b (patch)
tree1856d0948df659875a6d07626ea074065df74bfe
parent03c619322017a60c27b516a925a75c2c5dc8c2dd (diff)
Several things.
- Using Slim instead of Haml. - Having a before filter for having flash functionality. - Using Bundler.
-rw-r--r--Gemfile4
-rw-r--r--config.ru6
-rw-r--r--file_upload.rb30
-rw-r--r--views/index.haml16
-rw-r--r--views/index.slim15
-rw-r--r--views/layout.haml9
-rw-r--r--views/layout.slim8
7 files changed, 46 insertions, 42 deletions
diff --git a/Gemfile b/Gemfile
new file mode 100644
index 0000000..975cab0
--- /dev/null
+++ b/Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+gem 'sinatra', '~> 1.3'
+gem 'slim', '~> 1.3'
diff --git a/config.ru b/config.ru
index ba412c2..7e916a6 100644
--- a/config.ru
+++ b/config.ru
@@ -1,5 +1,5 @@
-$LOAD_PATH.unshift(File.dirname(__FILE__))
-require 'file_upload'
+# $LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'bundler/setup'
+require './file_upload'
run FileUpload
-
diff --git a/file_upload.rb b/file_upload.rb
index 671abcb..ce2c65f 100644
--- a/file_upload.rb
+++ b/file_upload.rb
@@ -1,17 +1,17 @@
# encoding: utf-8
-require 'rubygems'
-require 'haml'
require 'sinatra/base'
+require 'slim'
class FileUpload < Sinatra::Base
configure do
enable :static
enable :sessions
- set :views, File.join(File.dirname(__FILE__), 'views')
+ set :views, File.join(File.dirname(__FILE__), 'views')
set :public_folder, File.join(File.dirname(__FILE__), 'public')
- set :files, File.join(settings.public_folder, 'files')
+ set :files, File.join(settings.public_folder, 'files')
+ set :unallowed_paths, ['.', '..']
end
helpers do
@@ -20,21 +20,22 @@ class FileUpload < Sinatra::Base
end
end
+ before do
+ @flash = session.delete(:flash)
+ end
+
not_found do
- haml '404'
+ slim 'h1 404'
end
error do
- haml "Error (#{request.env['sinatra.error']})"
+ slim "Error (#{request.env['sinatra.error']})"
end
get '/' do
- @files = Dir.entries(settings.files) - ['.', '..']
+ @files = Dir.entries(settings.files) - settings.unallowed_paths
- @flash = session[:flash]
- session[:flash] = nil
-
- haml :index
+ slim :index
end
post '/upload' do
@@ -42,9 +43,11 @@ class FileUpload < Sinatra::Base
filename = params[:file][:filename]
file = params[:file][:tempfile]
- File.open(File.join(settings.files, filename), 'wb') {|f| f.write file.read }
+ File.open(File.join(settings.files, filename), 'wb') do |f|
+ f.write file.read
+ end
- flash 'Uploaded successfully'
+ flash 'Upload successful'
else
flash 'You have to choose a file'
end
@@ -52,4 +55,3 @@ class FileUpload < Sinatra::Base
redirect '/'
end
end
-
diff --git a/views/index.haml b/views/index.haml
deleted file mode 100644
index b9d78e7..0000000
--- a/views/index.haml
+++ /dev/null
@@ -1,16 +0,0 @@
-%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
-%ul
- - @files.each do |file|
- %li= "<a href='files/#{file}'>#{file}</a>"
-
diff --git a/views/index.slim b/views/index.slim
new file mode 100644
index 0000000..270f91f
--- /dev/null
+++ b/views/index.slim
@@ -0,0 +1,15 @@
+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
+ul
+ - @files.each do |file|
+ li
+ a href='files/#{file}' #{file}
diff --git a/views/layout.haml b/views/layout.haml
deleted file mode 100644
index d8eb52c..0000000
--- a/views/layout.haml
+++ /dev/null
@@ -1,9 +0,0 @@
-!!!
-%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
-
diff --git a/views/layout.slim b/views/layout.slim
new file mode 100644
index 0000000..0d4d1f1
--- /dev/null
+++ b/views/layout.slim
@@ -0,0 +1,8 @@
+doctype
+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