在 Rails 中使用回形针设置原始图像的路径? [英] Set path for original images using paperclip in Rails?

查看:37
本文介绍了在 Rails 中使用回形针设置原始图像的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况

我有一个简单的模型,带有使用回形针的附加图像,它有几个处理过的样式(缩略图、完整、功能).此时它正常工作,并为 /public/assets/foo/ 中的每个对象创建一个目录结构,包含原始、缩略图、完整和功能的子目录.

问题

我不想让用户获取原始(高分辨率)图像.所以我希望有一种方法可以指定不同的路径来将原始文件存储在 /public/ 之外的某个地方.理想情况下,回形针应该仍然能够使用原始图像作为源重新处理样式,就像现在一样.

我也愿意接受其他建议,让外部用户无法访问原件.无论是这里最实用的解决方案.谢谢.

解决方案

我建议使用自定义插值,将原始文件放置在公共目录之外.像这样:

<预><代码>Paperclip.interpolates :maybe_public do |附件,样式|风格==:原创?私人":公共"结尾has_attached_file :image, :path => ":rails_root/:maybe_public/:attachment..."

这会将您的 :original 文件保存在一个不可公开访问的目录中以进行保护,但仍允许 Paperclip 访问.它会将您的缩略图保存在公共目录中以供标准访问.

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). 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.

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/. 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. And it will keep your thumbnails in the public directory for standard access.

这篇关于在 Rails 中使用回形针设置原始图像的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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