Upload e download de arquivos no Sinatra

Um exemplo simples.

Lista

get '/' do
list
= Dir.glob("./files/*.*").map{|f| f.split('/').last}
# render list here
end

Envio

post '/' do
tempfile
= params['file'][:tempfile]
filename
= params['file'][:filename]
File.copy(tempfile.path, "./files/#{filename}")
redirect
'/'
end

Baixar

get '/download/:filename' do |filename|
send_file
"./files/#{filename}", :filename => filename, :type => 'Application/octet-stream'
end

Excluir

get '/remove/:filename' do |filename|
File.delete("./files/#{filename}")
redirect
'/'
end