Rails 3,显示来自RAILS_ROOT上方的本地文件系统的jpg图像 [英] Rails 3, displaying jpg images from the local file system above RAILS_ROOT

查看:50
本文介绍了Rails 3,显示来自RAILS_ROOT上方的本地文件系统的jpg图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来显示来自RAILS_ROOT之下(在RedHat或Ubuntu环境中)的已挂载文件系统的图像.我不想使用符号链接,因为此应用程序实际上是通过Tomcat部署的,当我关闭Tomcat时,Tomcat会尝试遵循该符号链接并删除安装中的所有映像.由于这些文件的数量和大小,因此也不可以将它们置于public/images下.

我查看了send_file,但这只会显示一个图像.我需要在格式正确的页面中显示6个请求的图像.由于膨胀,我不希望使用Base64编码,但是我不知道如何将图像数据与渲染页面一起向下传递.

同样,问题是无法通过生成的HTML中的url访问图像,图像数据必须与页面的其余部分一起传递.我知道创建某种类型的服务来存储图像将是处理此问题的最佳方法,但是现在我只需要使它工作即可.

任何帮助都将不胜感激!

解决方案

出于明显的原因,Rails不允许用户访问RAILS_ROOT/public之外的文件系统部分.提到sendfile(或优先选择x-sendfile)时,您实际上就是其中的一部分.解决方案是让Rails正常提供html页面,该页面包含指向Rails操作的链接,该操作提供单独的文件.

例如,假设我要显示文件系统深处的一些图像.首先,我必须创建一个动作,一次提供一个图像.因此,您可以在config/routes.rb中添加以下内容:

match '/serve_image/:filename' => 'images#serve'

然后,我必须创建一个控制器以匹配该路由.例如:

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

现在,您的图片可以在以下网址下找到:/serve_image/my_image.jpg.因此,最后要做的是创建一个漂亮的html页面,其中显示了所有必要的图像.就像这样简单:

<img src="/serve_image/image1.jpg">
<img src="/serve_image/image2.jpg">
<img src="/serve_image/image3.jpg">
<img src="/serve_image/image4.jpg">

I'm trying to figure out a way to display images from a mounted file system that is not under RAILS_ROOT (in RedHat or Ubuntu environments). I don't want to use a symbolic link because this app is actually deployed through Tomcat, and Tomcat tries to follow the symbolic link and delete all the images in the mount when I shut down Tomcat. Putting the images under public/images is also not an option due to the number and size of these files.

I looked into send_file, but that will just display the one image. I need to display 6 requested images in a nicely formatted page. I would prefer not to use Base64 encoding because of the bloat, but I don't know how else to pass the image data down with the rendered page.

Again, the issue is that the images cannot be accessed via url in the generated HTML, the image data must be passed down with the rest of the page. I know that creating some type of service to server up the images would be the best way to handle this, but right now I just need to get this working.

Any help greatly appreciated!

解决方案

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">

这篇关于Rails 3,显示来自RAILS_ROOT上方的本地文件系统的jpg图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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