Allow binary content (fixes #18)

This commit is contained in:
galfert 2012-11-02 13:17:49 +01:00
parent fa6235cd1f
commit 483523b091
3 changed files with 49 additions and 2 deletions

View File

@ -5,6 +5,7 @@ defaults: &defaults
buckets:
data: "user_data"
directories: "rs_directories"
binaries: "rs_binaries"
authorizations: "authorizations"
development:
@ -15,6 +16,7 @@ test:
buckets:
data: "user_data_test"
directories: "rs_directories_test"
binaries: "rs_binaries_test"
authorizations: "authorizations_test"
production:

View File

@ -23,6 +23,10 @@ module RemoteStorage
@auth_bucket ||= client.bucket(LiquorCabinet.config['buckets']['authorizations'])
end
def binary_bucket
@binary_bucket ||= client.bucket(LiquorCabinet.config['buckets']['binaries'])
end
def authorize_request(user, directory, token, listing=false)
request_method = env["REQUEST_METHOD"]
@ -47,6 +51,10 @@ module RemoteStorage
headers["Content-Type"] = object.content_type
headers["Last-Modified"] = last_modified_date_for(object)
if binary_link = object.links.select {|l| l.tag == "binary"}.first
object = client[binary_link.bucket].get(binary_link.key)
end
case object.content_type[/^[^;\s]+/]
when "application/json"
return object.data.to_json
@ -76,8 +84,10 @@ module RemoteStorage
object = data_bucket.new("#{user}:#{directory}:#{key}")
object.content_type = content_type || "text/plain; charset=utf-8"
unless set_object_data(object, data)
halt 422
if binary_data?(content_type)
save_binary_data(object, data) or halt 422
else
set_object_data(object, data) or halt 422
end
directory_index = directory == "" ? "/" : directory
@ -279,6 +289,21 @@ module RemoteStorage
return false
end
def save_binary_data(object, data)
binary_object = binary_bucket.new(object.key)
binary_object.content_type = object.content_type
binary_object.raw_data = data
binary_object.store
link = ::Riak::Link.new(binary_bucket.name, binary_object.key, "binary")
object.links << link
object.raw_data = ""
end
def binary_data?(content_type)
content_type[/[^;\s]+$/] == "charset=binary"
end
def parent_directories_for(directory)
directories = directory.split("/")
parent_directories = []

View File

@ -123,6 +123,26 @@ describe "Directories" do
directory.data.to_i.must_equal object.meta['timestamp'][0].to_i
end
end
context "with binary data" do
before do
header "Content-Type", "image/jpeg; charset=binary"
filename = File.join(File.expand_path(File.dirname(__FILE__)), "fixtures", "rockrule.jpeg")
@image = File.open(filename, "r").read
put "/jimmy/tasks/jaypeg.jpg", @image
end
it "lists the binary files" do
get "/jimmy/tasks/"
last_response.status.must_equal 200
content = JSON.parse(last_response.body)
content.must_include "jaypeg.jpg"
content["jaypeg.jpg"].must_be_kind_of Integer
content["jaypeg.jpg"].to_s.length.must_equal 13
end
end
end
context "for a sub-directory" do