Rails 3.0和Carrierwave如何防止非图像类型的版本创建

发布于 2024-11-02 20:19:06 字数 947 浏览 0 评论 0原文

我正在努力尝试实现最初对我来说看似简单的事情。在使用 rierwaverails 3.0 应用程序的上下文中。用户必须能够下载除 .exe 文件之外的任何类型的文档。在 Carriewave 中有一个白名单,

def extension_white_list
    %w(jpg jpeg gif png)
end

我希望也有一个黑名单,这对我来说会更容易。无论如何,这不是主要问题。

对于图像文件,我在上传器类中设置了 2 个版本。

 version :thumb do
      process :resize_to_fit => [50, 50]  
  end


  version :small do
     process :resize_to_fit => [125, 125]
  end

我必须承认我对这种语法有点困惑。什么样的 ruby​​ 代码“版本”定义。我的意思是,它不是一个方法,那么它在类中是什么?

我尝试创建方法图像?

def image?
%w(jpg jpeg gif png).include?(filename.extension.to_s)
end

然后从版本代码中调用它,以防止在文件不是图像时创建这些版本

version :thumb do
  if image?
    process :resize_to_fit => [50, 50]  
  end
end

,但此代码会抛出错误,

undefined method `image?' for #<Class:0x000001017274f8> 

任何帮助将不胜感激。

谢谢。

I'm struggling a bit trying to achieve something that appeared simple initially to me. In the context of a rails 3.0 app using carrierwave. The user must be able do download any type of document except .exe files. in Carriewave there is the whitelist

def extension_white_list
    %w(jpg jpeg gif png)
end

I whish there was a blacklist as well it would easier in my case. Anyway this is the not the main concern.

for image file I set 2 version in my uploader class.

 version :thumb do
      process :resize_to_fit => [50, 50]  
  end


  version :small do
     process :resize_to_fit => [125, 125]
  end

I have to admit that I'm little bit confused by this syntax. What kind of ruby code "version" define. I mean, it is not a method, so what's that in a class?

I tried to create a method image?

def image?
%w(jpg jpeg gif png).include?(filename.extension.to_s)
end

and then called it from the version piece of code to prevent creation of those version when the file is not an image

version :thumb do
  if image?
    process :resize_to_fit => [50, 50]  
  end
end

but this code throw an error

undefined method `image?' for #<Class:0x000001017274f8> 

any help would be appreciated.

thanks.

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

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

发布评论

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

评论(2

国产ˉ祖宗 2024-11-09 20:19:06

下面是您实际上可以如何做到这一点。当前版本的 Carrierwave 现在支持条件版本处理。请参阅 wiki 页面 https://github.com/jnicklas /rierwave/wiki/How-to%3A-Do-conditional-processing

要仅为特定类型创建版本,请执行以下操作:

version :thumb, :if => :image? do
  process :resize_to_fit => [50, 50]  
end

protected

def image?(new_file)
  new_file.content_type.include? 'image'
end

Here's how you actually can do this. The current version of carrierwave supports conditional version processing now. See the wiki page https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing

To only create versions for specific types, do this:

version :thumb, :if => :image? do
  process :resize_to_fit => [50, 50]  
end

protected

def image?(new_file)
  new_file.content_type.include? 'image'
end
那小子欠揍 2024-11-09 20:19:06

最后对这个问题有一个清晰干净的答案:https://gist.github.com/995663

Finally a clear and clean answer for this question: https://gist.github.com/995663

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