I have rails project. In my project I load images on server (rails 3 + paperclip + devise + cancan). I want to limit access to files (for example, the original image can be viewed only by the administrator). How should I do it?
相关问题
- Views base64 encoded blob in HTML with PHP
- How to get the background from multiple images by
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- What is the best way to do a search in a large fil
相关文章
- Right way to deploy Rails + Puma + Postgres app to
- Use savefig in Python with string and iterative in
- AWS S3 in rails - how to set the s3_signature_vers
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- What is the correct way to declare and use a FILE
- “No explicit conversion of Symbol into String” for
- Where does this quality loss on Images come from?
Files are handled with
ActionDispatch::Static
. IMO the best solution is provide similar middleware (basing on Rails source code) but with some authentication and insert it beforeActionDispatch::Static
. This would work with Warden-based solutions (like Devise) since they do authentication in middleware; just ensure your middleware is put after Warden and old plainActionDispatch::Static
is run just after them.EDIT: One more thing worth noting. It's quite common in production that Nginx (I'm not sure about Apache and others) is configured to serve all the static files by itself, without passing those requests to rack stack. You may need to disable this Nginx' feature.
If you are limiting by an attribute in your database then one way would be to serve the image via a controller. It's not the most performant but it is secure.
I haven't tried this out, but if you were serving the image from a URL like
/images/picture_of_mickey_mouse.png
then you could create a route in your app that responds to
/images
with a filename attribute and serve all those images through a controller.e.g.
You would want to make sure you sanitize the
params[:filename]
however, otherwise the user would be able to download any file on your server!The docs on
send_file
are here: http://apidock.com/rails/ActionController/DataStreaming/send_file