CarrierWave:为多种类型的文件创建 1 个上传器

发布于 2024-12-05 18:11:11 字数 267 浏览 1 评论 0原文

我想为多种类型的文件(图像、pdf、视频)创建 1 个上传程序

对于每种 content_type 都会执行不同的操作

我如何定义文件的 content_type?

例如:

if image?
  version :thumb do
    process :proper_resize    
  end
elsif video?
  version :thumb do
    something
  end
end

I want to create 1 uploader for multiple types of files (images, pdf, video)

For each content_type will different actions

How I can define what content_type of file?

For example:

if image?
  version :thumb do
    process :proper_resize    
  end
elsif video?
  version :thumb do
    something
  end
end

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

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

发布评论

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

评论(1

征﹌骨岁月お 2024-12-12 18:11:11

我遇到了这个,它看起来像是如何解决此问题的示例:https://gist.github.com/995663

当您调用 mount_uploader 时,上传器首先被加载,此时 if image?elsif video? 将不起作用,因为尚未定义要上传的文件。您需要在实例化类时调用这些方法。

我上面给出的链接的作用是重写 process 方法,以便它采用文件扩展名列表,并且仅当您的文件与这些扩展名之一匹配时才进行处理,

# create a new "process_extensions" method.  It is like "process", except
# it takes an array of extensions as the first parameter, and registers
# a trampoline method which checks the extension before invocation
def self.process_extensions(*args)
  extensions = args.shift
  args.each do |arg|
    if arg.is_a?(Hash)
      arg.each do |method, args|
        processors.push([:process_trampoline, [extensions, method, args]])
      end
    else
      processors.push([:process_trampoline, [extensions, arg, []]])
    end
  end
end

# our trampoline method which only performs processing if the extension matches
def process_trampoline(extensions, method, args)
  extension = File.extname(original_filename).downcase
  extension = extension[1..-1] if extension[0,1] == '.'
  self.send(method, *args) if extensions.include?(extension)
end

然后您可以使用它来调用什么过去用于处理

IMAGE_EXTENSIONS = %w(jpg jpeg gif png)
DOCUMENT_EXTENSIONS = %(exe pdf doc docm xls)
def extension_white_list
  IMAGE_EXTENSIONS + DOCUMENT_EXTENSIONS
end

process_extensions IMAGE_EXTENSIONS, :resize_to_fit => [1024, 768]

版本,如果您使用的是 >0.5.4,则 Carrierwave wiki 上有一个页面允许您有条件地处理版本。 https://github.com/jnicklas/rierwave/wiki/How-to% 3A-执行条件处理。您必须将版本代码更改为如下所示:

version :big, :if => :image? do
  process :resize_to_limit => [160, 100]
end

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

I came across this, and it looks like an example of how to solve this problem: https://gist.github.com/995663.

The uploader first gets loaded when you call mount_uploader, at which point things like if image? or elsif video? won't work, because there is no file to upload defined yet. You'll need the methods to be called when the class is instantiated instead.

What the link I gave above does, is rewrite the process method, so that it takes a list of file extensions, and processes only if your file matches one of those extensions

# create a new "process_extensions" method.  It is like "process", except
# it takes an array of extensions as the first parameter, and registers
# a trampoline method which checks the extension before invocation
def self.process_extensions(*args)
  extensions = args.shift
  args.each do |arg|
    if arg.is_a?(Hash)
      arg.each do |method, args|
        processors.push([:process_trampoline, [extensions, method, args]])
      end
    else
      processors.push([:process_trampoline, [extensions, arg, []]])
    end
  end
end

# our trampoline method which only performs processing if the extension matches
def process_trampoline(extensions, method, args)
  extension = File.extname(original_filename).downcase
  extension = extension[1..-1] if extension[0,1] == '.'
  self.send(method, *args) if extensions.include?(extension)
end

You can then use this to call what used to be process

IMAGE_EXTENSIONS = %w(jpg jpeg gif png)
DOCUMENT_EXTENSIONS = %(exe pdf doc docm xls)
def extension_white_list
  IMAGE_EXTENSIONS + DOCUMENT_EXTENSIONS
end

process_extensions IMAGE_EXTENSIONS, :resize_to_fit => [1024, 768]

For versions, there's a page on the carrierwave wiki that allows you to conditionally process versions, if you're on >0.5.4. https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing. You'll have to change the version code to look like this:

version :big, :if => :image? do
  process :resize_to_limit => [160, 100]
end

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