Created
October 9, 2008 09:30
-
-
Save anonymous/15733 to your computer and use it in GitHub Desktop.
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 "rack" | |
app = lambda do |env| | |
request = Rack::Request.new(env) | |
if request.get? | |
[ | |
200, | |
{"Content-Type" => "text/html"}, | |
<<-HTML | |
<html> | |
<head></head> | |
<body> | |
<form method="POST" enctype="multipart/form-data"> | |
<p> | |
<label>select file</label> | |
<input type="file" name="file"> | |
</p> | |
<p style="margin-top: 20px"> | |
<input type="submit" value="Upload"> | |
</p> | |
</form> | |
</body> | |
</html> | |
HTML | |
] | |
elsif request.post? | |
Dir.mkdir "./public" unless File.exists? "./public" | |
Dir.mkdir "./public/fs" unless File.exists? "./public/fs" | |
file_path = "/fs/#{request["file"][:filename]}" | |
FileUtils.mv( | |
request["file"][:tempfile].path, | |
File.expand_path("public#{file_path}") | |
) | |
[ | |
201, | |
{"Location" => file_path, "Content-Type" => "text/html"}, | |
<<-HTML | |
<html> | |
<head></head> | |
<body> | |
<h1>File saved: <a href="#{file_path}">#{file_path}</a></h1> | |
</body> | |
</html> | |
HTML | |
] | |
end | |
end | |
use Rack::ShowExceptions | |
use Rack::Static, :urls => ["/fs"], :root => "./public" | |
run app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment