I am building a web application using sinatra. It basically takes a url of a page as request and fetches the link to media file from the page. Once that is retrieved it opens a stream to the media file. Now I want to send this stream as http response to client. I tried to do the following.
get '/' do
url=PM.link(params[:url])
puts url
buf=nil
open(url, 'rb') do |rf|
while(buf=rf.read(1024)) do
stream do |out|
out << buf
end
end
end
end
and also this
get '/' do
url=PM.link(params[:url])
puts url
buf=nil
open(url, 'rb') do |rf|
stream do |out|
out.write rf.read
end
end
end
But neither work. I tried storing the media file in a local file it stores it properly. But when I try to write it to out stream it responds with no data. Am I missing something? Or sinatra doesn't allow writing binary from one stream to other.