Skip to content
Browse files

One object for the code, the other for the file.

  • Loading branch information...
1 parent e34d5ae commit 63eef55ebf8d55d86a9255e8ce27695e4fe7d76c @athoune committed Nov 2, 2011
Showing with 38 additions and 19 deletions.
  1. +37 −14 lib/terasound/riak.rb
  2. +1 −5 spec/file_spec.rb
View
51 lib/terasound/riak.rb
@@ -16,24 +16,47 @@ def bucket(bucket, &block)
)
end
- def upload_file(filepath, bucket, contentType, metadata={})
- head = {"Content-Type" => contentType}
- metadata.map do |k, v|
- #head["X-Riak-Index-#{k}"] = v
- head["X-Riak-Meta-#{k}"] = v
- end
- req = @conn.post(
+ def upload_code(code, bucket)
+ head = {"Content-Type" => "text/plain"}
+ @conn.post(
path: "/riak/#{bucket}",
keepalive: true,
- file: filepath,
+ body: code,
head: head
)
- req.errback do
- puts "argh, can't upload", req
- puts req.response_header
- puts req.response
- end
- req
+ end
+
+ def upload_file(filepath, bucket, contentType, metadata={}, &block)
+ code = upload_code(metadata['code'], bucket)
+
+ code.callback do
+ if code.response_header.status == 201
+ key = code.response_header['LOCATION'].split('/')[3..-1].join('/')
+ head = {
+ "Content-Type" => contentType,
+ "link" => "</#{key}>; riaktag=\"code\""
+ }
+ metadata.map do |k, v|
+ #head["X-Riak-Index-#{k}"] = v
+ head["X-Riak-Meta-#{k}"] = v.to_s if v != nil and v != ""
+ end
+ req = @conn.post(
+ path: "/riak/#{bucket}",
+ keepalive: true,
+ file: filepath,
+ head: head
+ )
+ req.errback do
+ puts "argh, can't upload", req
+ puts req.response_header
+ puts req.response_header.status
+ puts req.response
+ end
+ req.callback do
+ yield block
+ end
+ end
+ end
end
end
View
6 spec/file_spec.rb
@@ -5,11 +5,7 @@
it "puts mp3 to the server" do
f = Terasound::Riak.new
EM.run do
- p = f.upload_file("spec/test.mp3", "terasound3", "audio/mpeg", {beuha: "aussi"})
- p.callback do
- puts p.response_header
- puts p.response
- puts "uploaded"
+ p = f.upload_file("spec/test.mp3", "terasound3", "audio/mpeg", {beuha: "aussi"}) do
EM.stop
end
end

0 comments on commit 63eef55

Please sign in to comment.
Something went wrong with that request. Please try again.