Skip to content

Instantly share code, notes, and snippets.

@joaodrp
Created January 15, 2012 15:51
Show Gist options
  • Save joaodrp/1616242 to your computer and use it in GitHub Desktop.
Save joaodrp/1616242 to your computer and use it in GitHub Desktop.
require 'uri'
module Store
class FileSystem
CHUNKSIZE = 65536
def initialize(uri)
@uri = uri
@path = uri.path
end
def get
open(@path, "rb") do |file|
yield file.read(CHUNKSIZE) until file.eof?
end
end
end
end
class GetImage < Goliath::API
use Goliath::Rack::Render, 'json'
def response(env)
begin
meta = db.get_image(params[:id])
rescue MyExceptions::NotFound => e
return [404, {}, {code: 404, message: e.message}]
end
uri = meta[:location]
headers = push_meta_into_headers(meta)
# duhh, this was obviously blocking the reactor with big files (now testing with 700mb iso)...
# EM.next_tick do
# Store::FileSystem.new(uri).get { |chunk| env.stream_send(chunk) }
# env.stream_close
# end
# this doesn't block, but probably there is a better way?
operation = proc do
Store::FileSystem.new(uri).get { |chunk| env.stream_send(chunk) }
end
callback = proc do |result|
env.stream_close
end
EM.defer operation, callback
headers.merge!('Content-Type' => 'application/octet-stream', 'X-Stream' => 'Goliath')
[200, headers, Goliath::Response::STREAMING]
end
end
class Server < Goliath::API
use Goliath::Rack::Heartbeat
use Goliath::Rack::Params
get '/images/:id', GetImage
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment