-
-
Save aaira-a/a6f1f371c71a3b415c0e412838a61782 to your computer and use it in GitHub Desktop.
A simple file server and uploader using sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 => 'inline') | |
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"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment