Skip to content

Instantly share code, notes, and snippets.

@skord
Created May 23, 2011 18:41
Show Gist options
  • Select an option

  • Save skord/987260 to your computer and use it in GitHub Desktop.

Select an option

Save skord/987260 to your computer and use it in GitHub Desktop.

Revisions

  1. skord created this gist May 23, 2011.
    82 changes: 82 additions & 0 deletions bfile.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    #!/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"}