Created
January 18, 2018 11:20
-
-
Save mvasin/63ec16c9be08355703583848ddc18a9b to your computer and use it in GitHub Desktop.
A simple rack app for educational purposes: upload a file and have it presented in the browser
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
class App | |
def self.call env | |
new(env).call | |
end | |
def initialize env | |
@request = Rack::Request.new env | |
@response = Rack::Response.new | |
end | |
def set_response | |
case @request.request_method | |
when 'GET' | |
@response.set_header 'Content-Type', 'text/html' | |
@response.write '<form method="post" enctype="multipart/form-data">' | |
@response.write '<input type="file" name="myfile"></input><input type="submit"></input>' | |
when 'POST' | |
@response.set_header 'Content-Type', Rack::Mime.mime_type(File.extname @request.params['myfile'][:tempfile]) | |
@response.write @request.params['myfile'][:tempfile].read | |
else | |
raise 'Method unknown!' | |
end | |
end | |
def call | |
set_response | |
@response.finish | |
end | |
end | |
run App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment