简体   繁体   English

在Rails中使用回形针设置原始图像的路径?

[英]Set path for original images using paperclip in Rails?

The situation 情况

I have a simple model with an attached image using paperclip, which has a couple of processed styles for it (thumbnail, full, feature). 我有一个简单的模型与附加图像使用paperclip,它有几个处理样式(缩略图,完整,功能)。 At this point it works as it should, and makes a directory structure for each object in /public/assets/foo/ , containing subdirectories for original, thumbnail, full, and feature. 此时它可以正常工作,并为/public/assets/foo/每个对象创建一个目录结构,其中包含原始,缩略图,完整和功能的子目录。

The problem 问题

I don't want the original (high resolution) images to be exposed for users to obtain. 我不希望公开原始(高分辨率)图像供用户使用。 So I'm hoping there is a way to specify a different path to store the originals somewhere outside of /public/ . 所以我希望有一种方法可以指定一个不同的路径来存储/public/之外的某个原件。 Ideally paperclip should still be able to reprocess the styles using that original image as the source, as it does currently. 理想情况下,回形针应该仍然能够使用原始图像作为源重新处理样式,就像当前一样。

I'm also open to alternative suggestions for making the originals inaccessible to outside users. 我也愿意接受让外部用户无法访问原件的替代建议。 Whatever is the most practical solution here. 这里最实用的解决方案是什么。 Thanks. 谢谢。

I would recommend using a custom interpolation that will place your original files outside the public directory. 我建议使用自定义插值将原始文件放在公共目录之外。 Something like this: 像这样的东西:


Paperclip.interpolates :maybe_public do |attachment, style|
  style == :original ? "private" : "public"
end

has_attached_file :image, :path => ":rails_root/:maybe_public/:attachment..."

This will save your :original files in a non-publicly accessible directory for protection, but still allow Paperclip access. 这会将您的原始文件保存在不可公开访问的目录中以进行保护,但仍允许Paperclip访问。 And it will keep your thumbnails in the public directory for standard access. 它会将您的缩略图保留在公共目录中以进行标准访问。

If it is acceptable, you could skip saving the originals, by setting the default style. 如果可以接受,您可以通过设置默认样式来跳过保存原件。

  has_attached_file :image,
                    :styles => { :normal => "800x600>" },
                    :default_style => :normal

If not, and you want to keep the originals, if you are using apache, you could use a .htaccess file to restrict the access to the originals directory 如果没有,并且您想要保留原始文件,如果您使用的是apache,则可以使用.htaccess文件来限制对原始目录的访问

<FilesMatch "^\.(jpe?g|gif|png)$">
   Order allow,deny
   Deny from all
</Files>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM