Rails 服务大文件 [英] Rails serving large files

查看:34
本文介绍了Rails 服务大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个仅向登录用户提供大型视频的应用程序.

I'm developing an application serving large videos only to logged users.

为了保持这些视频的私密性,我将它们放在 Rails 项目中的私有文件夹中,让 Rails 为它们提供服务,而不是使用公共文件夹并排除来自 apache 的请求(以避免直接链接到它们).

To keep these videos private i put them in a private folder inside Rails project and let Rails serve them, instead of using the public folder and excluding requests from apache (to avoid direct linking to them).

我在控制器中的操作如下所示:

My action in the controller looks like this:

def video
      respond_to do |format|
        format.mp4{
         send_file File.join([Rails.root, "private/videos", @lesson.link_video1 + ".mp4"]), 
      :disposition => :inline, :stream => true
        }
      end
  end

一切正常,但只是小文件,一旦我尝试使用真实文件,我就会收到错误:

Everything works perfectly, but just with small files, as soon as i try with real files i receive the error:

NoMemoryError (failed to allocate memory)

我在某处读到将 send_file 用于大文件不是一个好习惯,但是使用另一种方法让 apache 为文件提供服务时,我在向移动 Apple 设备提供文件时遇到了问题,因为它们没有发送HTTP_REFERER.

I read somewhere that is not a good practice to use send_file for large files, but using the other approach, to let apache serve the files, i had an issue serving files to mobile apple devices, as they're not sending the HTTP_REFERER.

你知道这个内存限制有多小吗?我的视频从 400MB 到 2GB(试图减少它们).

Do you have any idea on how small is this memory limit? My videos are from 400MB to 2GB (trying to reduce them).

我在这里发现的唯一问题是没有答案从 rails 中的 assets 文件夹提供大型媒体文件

The only question i found here is without an answer serving large media files from the assets folder in rails

推荐答案

我设法在 Apache 上激活了 X-Sendfile,而不是让 Rails 来处理大文件.与 Capistrano 合作,我找到了一个很好的解决方案.这里解释了 Capistrano &X-发送文件

I managed to activate X-Sendfile on Apache instead of letting Rails to serve large files. Working with Capistrano i found a good solution. Here is explained how Capistrano & X-Sendfile

这篇关于Rails 服务大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆