Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/public/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
source "https://rubygems.org/" | ||
|
||
gem "rake" | ||
gem "puma" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
rake (10.3.1) | ||
puma (2.8.2) | ||
rack (>= 1.1, < 2.0) | ||
rack (1.5.2) | ||
rake (10.3.2) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
puma | ||
rake |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
web: bundle exec ruby -run -e httpd -- --port=$PORT --do-not-reverse-lookup . | ||
web: bundle exec puma --port $PORT config.ru |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# -*- ruby -*- | ||
|
||
require "fileutils" | ||
|
||
class Uploader | ||
class ValidationError < StandardError | ||
end | ||
|
||
def initialize(request, response) | ||
@request = request | ||
@response = response | ||
@response["Content-Type"] = "text/plain" | ||
end | ||
|
||
def run | ||
begin | ||
validate | ||
rescue ValidationError | ||
else | ||
upload | ||
end | ||
|
||
@response.finish | ||
end | ||
|
||
private | ||
def validate | ||
validate_method | ||
validate_path | ||
validate_content | ||
end | ||
|
||
def validation_error(message) | ||
@response.status = 400 | ||
@response.write(message) | ||
raise ValidationError | ||
end | ||
|
||
def validate_method | ||
return if @request.put? | ||
validation_error("must be PUT\n") | ||
end | ||
|
||
def validate_path | ||
return unless @request.path.end_with?("/") | ||
validation_error("must be file name\n") | ||
end | ||
|
||
def validate_content | ||
return if @request.body | ||
validation_error("body is required\n") | ||
end | ||
|
||
def upload | ||
path = "public#{@request.path}" | ||
FileUtils.mkdir_p(File.dirname(path)) | ||
File.open(path, "wb") do |output| | ||
buffer_size = 8912 | ||
buffer = "" | ||
while @request.body.read(buffer_size, buffer) | ||
output.write(buffer) | ||
end | ||
end | ||
|
||
@response.write("Uploaded\n") | ||
end | ||
end | ||
|
||
file_server = Rack::File.new("public") | ||
|
||
application = lambda do |env| | ||
request = Rack::Request.new(env) | ||
response = Rack::Response.new | ||
if request.get? | ||
file_server.call(env) | ||
else | ||
uploader = Uploader.new(request, response) | ||
uploader.run | ||
end | ||
end | ||
run application |