Pregunta

I moved my file storage to Rackspace Cloudfiles and it broke my send_file action.

old

def full_res_download
  @asset = Asset.find(params[:id])
  @file = "#{Rails.root}/public#{@asset.full_res}"
  send_file @file
end

new

def full_res_download
  @asset = Asset.find(params[:id])
  @file = "http://86e.r54.cf1.rackcdn.com/uploads/fake/filepath.mov"
  send_file @file
end

When the files were in the public file. the code worked great. When you click in the link the file would download and the webpage would not change. Now it gives this error.

Cannot read file http://86e.r54.cf1.rackcdn.com/uploads/fake/filepath.mov

What am i missing?

Thank you so much for your time.

¿Fue útil?

Solución

what worked

def full_res_download
  @asset = Asset.find(params[:id])
  @file = open("http://86e.r54.cf1.rackcdn.com/uploads/fake/filepath.mov")
  send_file( @file, :filename => File.basename(@asset.file.path.to_s))
end

real code

controler.rb

def web_video_download
  @asset = Asset.find(params[:id])
  @file = open(CDNURL + @asset.video_file.path.to_s)
  send_file( @file, :filename => File.basename(@asset.video_file.path.to_s))
end

development.rb

CDNURL = "http://86e.r54.cf1.rackcdn.com/"

Otros consejos

send_file opens a local file and sends it using a rack middleware. You should just redirect to the url since you're not hosting the file anymore.

As one of the comments points out, in some situations you may not be able to use a redirect, for various reasons. If this is the case, you would have to download the file and relay it back to the user after retrieving it. The effect of this is that the transfer to the user would do the following:

  1. Request gets to your server, processing in your action begins.
  2. Your action requests the file from the CDN, and waits until the file has been fully retrieved.
  3. Your server can now relay the file on to the end user.

This is as compared to the case with a redirect:

  1. Request gets to your server, processing in your action begins.
  2. Your action redirects the user to the CDN.

In both cases the user has to wait for two full connections, but your server has saved some work. As a result, it is more efficient to use a redirect when circumstances allow.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top