3

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.

2 Answers2

4

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/"
2

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.

heartpunk
  • 2,235
  • 1
  • 21
  • 26
  • 1
    If redirection is not an option (authentication, internal network) you'll have to first download the file locally and send it on from there - an inefficient way of tackling this problem. – cfeduke Dec 30 '12 at 04:52
  • If you can't redirect for whatever reason, then you shouldn't use a CDN. – heartpunk Dec 30 '12 at 04:53
  • Tehgeekmeister thanks for the info but I would be cautious about passing opinion statements as fact. – T. Weston Kendall Dec 30 '12 at 06:58
  • It's unclear to me what was opinion. If you'll point it out, I'm happy to edit my answer/comment to resolve that, as my points are not so much opinion as shorthand statements that can be expanded if need be. – heartpunk Dec 30 '12 at 08:44
  • Updated my answer to limit what could seem like opinion, and stick more to facts. – heartpunk Dec 30 '12 at 08:54
  • I dont want to download the file to my server but a redirect (if I understand it correctly) will just play the video in the browser. This is not what the user needs. The user needs to download the file. Am I missing something? Rackspace has just informed me that I need to change the Content-Disposition header to have the browser force the download. – T. Weston Kendall Dec 30 '12 at 15:07
  • Yes, you would need to take steps to ensure that the file is downloaded rather than displayed in the browser, if you cared about that. I don't know how that's done with rackspace, but it's generally possible easily enough with other CDNs I've used. You should either modify the question here to reflect this additional constraint, or preferably make a separate question to address that concern, because it is unrelated to this original question. – heartpunk Dec 30 '12 at 20:49