gpt4 book ai didi

ruby-on-rails - Rails4 : how to restrict streaming video files access?

转载 作者:行者123 更新时间:2023-12-03 15:01:58 24 4
gpt4 key购买 nike

我想在我的页面中放置一些 html5 视频,但是只能授予登录用户访问此视频的权限(因此,未登录的用户必须看不到视频,如果他们有 url):

<video width="320" height="240" controls>
<source src="/video.mp4" type="video/mp4">
<source src="/video.m4v" type="video/m4v">
Your browser does not support the video tag.
</video>

我在我的操作中尝试了这样的事情(使用前置过滤器来限制访问):
  def video
respond_to do |format|
format.m4v{
send_data File.join([Rails.root, "public", "videos", "sample.m4v"]), type: 'video/m4v', disposition: :inline
}
format.mp4{
send_data File.join([Rails.root, "public", "videos", "sample.mp4"]), type: 'video/mp4', disposition: :inline
}
end
end

但这是将文件作为附件发送,而不仅仅是提供文件。

是否可以?如果是,怎么做?

谢谢你

最佳答案

难道你不想用video_tag将视频渲染为 View 的一部分?

如果是这样,只需使用设计助手 user_signed_in?在您希望视频显示的 ERB 文件中。这确保只有登录的用户才能在 video_tag 中看到视频。 .

<% if user_signed_in? %>
<%= video_tag ['/video.mp4', '/video.m4v'], controls: true, height: '240', width: '320' %>
<% end %>

更多信息 video_tag , 看:
http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-video_tag
http://guides.rubyonrails.org/layouts_and_rendering.html#linking-to-videos-with-the-video-tag

关于ruby-on-rails - Rails4 : how to restrict streaming video files access?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21687066/

24 4 0