For obvious reasons Rails won't allow a user to access parts of the filesystem outside of RAILS_ROOT/public
. You were actually part of the way there when you mentioned sendfile (or x-sendfile for preference.) The solution is to have Rails serve up an html page as normal which contains links to a Rails action which serves up the individual files.
For example, let's say I want to show some images from somewhere deep in the filesystem. First I have to create an action that serves up one image at a time. So in the config/routes.rb
you could add the following:
match '/serve_image/:filename' => 'images#serve'
Then I have to create a controller to match that route. For example:
class ImagesController < ApplicationController
def serve
path = "/my/servers/image/path/#{params[:filename]}"
send_file( path,
:disposition => 'inline',
:type => 'image/jpeg',
:x_sendfile => true )
end
end
Now your images are available under the url: /serve_image/my_image.jpg. So the last thing to do is create a nice html page that shows all the necessary images. It's as simple as:
<img src="/serve_image/image1.jpg">
<img src="/serve_image/image2.jpg">
<img src="/serve_image/image3.jpg">
<img src="/serve_image/image4.jpg">
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…