保护 Carrierwave 上传的文件
想要确保只有登录的用户才能查看/下载从 Carrierwave 上传的文件。
这是怎么做到的?
将存储目录从 public 移至 RAILS_ROOT,创建了显示和下载的路径。
问题是,如果它是图像, <%= image_tag(photo.image_url) %>
我得到完整路径 /Users/myname/projects/appname/files/image/id/image.png ,这样就不会渲染。
另外,如果视图不会呈现我的绝对路径,而只是站点的相对路径,那就太好了。
want to make sure that ONLY users that are logged in can view/download files uploaded from carrierwave.
How is this done?
Moved store directory from public to RAILS_ROOT, created route to show and download.
Problem is if its an image, <%= image_tag(photo.image_url) %>
I get the full path /Users/myname/projects/appname/files/image/id/image.png, so that doesn't render.
Also, would be nice if view would not render my absolute path, just relative path from site.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上是一件非常容易做到的事情。这是一个讨论 sendfile 和 x-sendfile 的博客。这应该会引导您走向正确的方向。
http://www.therailsway.com/2009/2/22 /file-downloads-done-right
这是它的 Rails 文档。
http://api.rubyonrails.org/classes/ActionController/Streaming .html#method-i-send_file
由于这一切都是在控制器级别处理的,因此只需确保您有一个 before_filter 来检查用户是否获得授权,然后在控制器操作中使用 send_file 方法。
This is actually a really easy thing to do. Here's a blog that talks about sendfile and x-sendfile. This should send you in the right direction.
http://www.therailsway.com/2009/2/22/file-downloads-done-right
Here is the rails docs for it.
http://api.rubyonrails.org/classes/ActionController/Streaming.html#method-i-send_file
Since this is all handled at the controller level, just make sure you have a before_filter that checks if the user is authorized, then use the send_file method in your controller action.