Created
December 14, 2015 18:42
-
-
Save sathish316/f5098067c393a7eafaa2 to your computer and use it in GitHub Desktop.
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' | |
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