Skip to content

Instantly share code, notes, and snippets.

@skord
Created May 23, 2011 18:41
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save skord/987260 to your computer and use it in GitHub Desktop.
Save skord/987260 to your computer and use it in GitHub Desktop.
A simple file server and uploader using sinatra
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'haml'
$pwd = ENV['PWD']
if File.exists?(ARGV.last)
if ARGV.last != 'bfile.rb'
$download_file = ARGV.last
end
end
def file_listing(directory)
Dir.glob(directory + '/*')
end
get '/upload' do
haml :upload
end
post '/upload' do
file = params[:file]
filename = file[:filename]
tempfile = file[:tempfile]
File.open(filename, 'w') {|f| f.write tempfile.read}
redirect '/browse'
end
get '/download/:filename' do |filename|
file = File.join($pwd, filename)
send_file(file, :disposition => 'attachment')
end
get '/browse' do
@files = file_listing($pwd)
haml :index
end
get '/bfile.rb' do
file = File.join($pwd, 'bfile.rb')
send_file(file, :disposition => 'attachment')
end
get '/' do
if $download_file
file = File.join($pwd, $download_file)
send_file(file, :disposition => 'attachment')
else
redirect '/browse'
end
end
__END__
@@ layout
%html
%a{:href => '/browse'}Browse Files
%a{:href => '/upload'}Upload a File
= yield
@@ index
%h1 File Server
%table
%tr
%th File
%th Size
- for file in @files
- if File.file?(file)
%tr
%td
%a{:title => file, :href => '/download/' + File.basename(file)}=File.basename(file)
%td= File.size(file).to_s + "b"
@@upload
%h1 Upload
%form{:action=>"/upload",:method=>'post',:enctype=>"multipart/form-data"}
%input{:type => "file",:name => "file"}
%input{:type => "submit",:value => "Upload"}
@sushildive
Copy link

Amazing!
Just the thing i wanted for sharing fies with my team members!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment