有 Rails “image_tag”浏览 /public 文件夹外的图像?

发布于 2024-10-10 18:44:59 字数 805 浏览 1 评论 0原文

我正在尝试将 Paperclip 与 Heroku 一起使用。是的,根据 Heroku 的应用程序限制。我只能将上传的文件存储到 /tmp 或 /.log 文件夹中,该文件夹完全位于 /public 文件夹之外。

如果我们不讨论 Amazon S3,我如何使用 image_tag 标签访问 /tmp 目录中的图像。

这是我的照片模型,使用回形针

class ObbProductphoto < ActiveRecord::Base
belongs_to :product

has_attached_file :photo, :styles => {:high => '1024x768', :medium => '640x480', :thumb => '100x100'},
:path => "tmp/:id.:extension",
:url => "tmp/:id.:extension"
end

这是我在浏览器中得到的:

<img src="/images/tmp/24.JPG?1294433924" alt="24" />

它仍然使用 /images 文件夹,

我试图破解任何 /.... /,无法得到解决方案。

谢谢你们, 苏帕特

I am trying to use Paperclip with Heroku. And yes, according to Heroku's Application Constraints. I'll be able to store the uploaded files into /tmp or /.log folder only, which is totally located outside the /public folder.

If we are not going to talk about Amazon S3, how can I access the image in the /tmp directory with image_tag tag.

This is my model of the photo, that using Paperclip

class ObbProductphoto < ActiveRecord::Base
belongs_to :product

has_attached_file :photo, :styles => {:high => '1024x768', :medium => '640x480', :thumb => '100x100'},
:path => "tmp/:id.:extension",
:url => "tmp/:id.:extension"
end

And this is what I got in the browser:

<img src="/images/tmp/24.JPG?1294433924" alt="24" />

It still using the /images folder,

I tried to hack any /.. or ../, couldn't get the solution.

Thank you guys,
Suebphatt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卸妝后依然美 2024-10-17 18:44:59

假设您可以将文件放在比 /tmp 更永久的位置,则可以使用 :url 和 :path 参数以及一些配置工作。

从我最近编写的代码中提取的一个示例(它经过了足够的修改,如果逐字复制它可能无法工作):

app/models/picture.rb
  # Define the attachment
  has_attached_file :image,
                    :styles => {:large  => ["700",      :jpg],
                                :thumb  => ["100x100>", :gif]},
                    :url => "/asset/picture/:id/:style/:basename.:extension",
                    :path => ":base/picture/:id/:style/:basename.:extension"

config/initializers/storage.rb
  # Configure common attachment storage
  # This approach allows more flexibility in defining, and potentially moving,
  # a common storage root for multiple models.  If unneeded, just replace
  # :base in the :path parameter with the actual path
  Paperclip.interpolates :base do |attachment, style|
    /path/to/persistent/storage
    # A relative path from the Rails.root directory should work as well
  end

app/controllers/pictures_controller.rb
  # Make the attachment accessible
  def asset
    instance = Picture.find(params[:id])
    params[:style].gsub!(/\.\./, '')
    #check permissions before delivering asset?
    send_file instance.image.path(params[:style].intern),
              :type => instance.image_content_type,
              :disposition => 'inline'
  end

config/routes.rb
  # Add the necessary route
  match '/asset/picture/:id/:style/:basename.:extension', :to => 'pictures#asset'

app/views/pictures/show.html.erb
  <% # Display the picture %>
  <%= image_tag picture.image.url(:large) %>

请注意,这都是 Rails 3 语法。要在 Rails 2.x 中使用它,需要进行一些更改,但希望您能了解,嗯,图片。

Assuming you can put the files somewhere more permanent than /tmp, storing and accessing uploaded files outside the public directory is possible using the :url and :path parameters, along with a bit of configuration work.

An example extracted from code I wrote recently (it's modified enough that it might not work if you copy it verbatim):

app/models/picture.rb
  # Define the attachment
  has_attached_file :image,
                    :styles => {:large  => ["700",      :jpg],
                                :thumb  => ["100x100>", :gif]},
                    :url => "/asset/picture/:id/:style/:basename.:extension",
                    :path => ":base/picture/:id/:style/:basename.:extension"

config/initializers/storage.rb
  # Configure common attachment storage
  # This approach allows more flexibility in defining, and potentially moving,
  # a common storage root for multiple models.  If unneeded, just replace
  # :base in the :path parameter with the actual path
  Paperclip.interpolates :base do |attachment, style|
    /path/to/persistent/storage
    # A relative path from the Rails.root directory should work as well
  end

app/controllers/pictures_controller.rb
  # Make the attachment accessible
  def asset
    instance = Picture.find(params[:id])
    params[:style].gsub!(/\.\./, '')
    #check permissions before delivering asset?
    send_file instance.image.path(params[:style].intern),
              :type => instance.image_content_type,
              :disposition => 'inline'
  end

config/routes.rb
  # Add the necessary route
  match '/asset/picture/:id/:style/:basename.:extension', :to => 'pictures#asset'

app/views/pictures/show.html.erb
  <% # Display the picture %>
  <%= image_tag picture.image.url(:large) %>

Note this is all Rails 3 syntax. A handful of changes would be needed to use it in Rails 2.x, but hopefully you get the, um, picture.

貪欢 2024-10-17 18:44:59

据我所知,你不能 - /tmp 没有配置为可以提供项目的东西(它用于临时存储)。

您可以在浏览器中访问 /tmp URL 上的图像吗?它们有效吗?

As far as I'm aware, you can't - /tmp isn't configured as something that you can serve items out of (it's intended for temporary storage).

Can you access images at the /tmp URLs in your browser? Do they work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文