Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'haml'
require 'fileutils'
set :port, 25000
$pwd = ENV['PWD']
def file_listing(directory)
Dir.glob(directory + '/*')
end
get '/upload' do
haml :upload
end
post '/' do
filepath = params[:filePath]
dir = Dir.pwd + filepath[0, filepath.rindex("/")]
FileUtils.mkdir_p dir
file = params[:file]
filename = file[:filename]
tempfile = file[:tempfile]
File.open(Dir.pwd + filepath, 'w') {|f| f.write tempfile.read}
redirect '/'
end
get '/*' do |rel_path|
path = File.join(Dir.pwd, rel_path)
if File.file?(path)
send_file(path, :disposition => 'attachment')
elsif File.directory?(path)
@files = file_listing(path)
haml :index
else
haml :upload
end
end
__END__
@@ layout
%html
= yield
@@ index
%h1 File Server
%table
%tr
%th File
%th Size
- for file in @files
%tr
%td
%a{:title => file, :href => file.sub(Dir.pwd, "").gsub("//", "/")}=File.basename(file) + (File.directory?(file) ? "/" : "")
%td= File.size(file).to_s + "b"
@@upload
%h1 Upload
%form{:action=>"/",: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