Reputation: 349
I moved my file storage to Rackspace Cloudfiles and it broke my send_file action.
def full_res_download
@asset = Asset.find(params[:id])
@file = "#{Rails.root}/public#{@asset.full_res}"
send_file @file
end
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.
Upvotes: 3
Views: 2742
Reputation: 349
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
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/"
Upvotes: 4
Reputation: 2275
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:
This is as compared to the case with a redirect:
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.
Upvotes: 2