Created
October 24, 2012 03:31
-
-
Save itsmikeq/3943515 to your computer and use it in GitHub Desktop.
Sinatra quick download script
This file contains hidden or 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
require 'sinatra' | |
require 'rubygems' | |
require "dm-migrations" | |
require 'sinatra-authentication' | |
require 'sinatra/authorization' | |
download_base = './Downloads' | |
set :authorization_realm, "download" | |
configure do | |
set :template_engine, :erb # for example | |
end | |
class DownloadFiles < Sinatra::Application | |
def self.initialize(base) | |
self.download_base = base || download_base | |
end | |
# @@download_base = "./Downloads" | |
configure :production, :development do | |
enable :logging | |
end | |
def self.download_base=(base) | |
puts "Setting base to #{base}" | |
@@download_base = base | |
end | |
def download_file(file) | |
send_file "#{@@download_base}/#{file}", :type=>"application/zip", :x_sendfile=>true | |
end | |
def self.downloadable_files | |
Dir.glob("#{@@download_base}/**") | |
end | |
end | |
class User | |
def peasant? | |
self.permission_level == 0 | |
end | |
end | |
helpers do | |
def authorize(login, password) | |
login == "admin" && password == "secret" | |
end | |
end | |
get '/' do | |
login_required | |
DownloadFiles.download_base=download_base | |
@files = DownloadFiles.downloadable_files | |
erb :index | |
end | |
get "/Downloads/:file" do | |
# Do stuff with the file here | |
"Would get a file: #{params[:captures]}" | |
end | |
post '/' do | |
puts "Hello #{params[:name]}!" | |
puts "You are posting with #{params.inspect}!" | |
end | |
__END__ | |
@@index | |
<%= Time.now %> | |
<br /> | |
<% @files.each do |file|%> | |
<a href=<%= file%>><%= file%> | |
<br/> | |
<%end%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment