Rails回形针仅适用于图像吗? [英] Is Rails Paperclip only for Images?

查看:115
本文介绍了Rails回形针仅适用于图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有用于管理与ActiveRecord连接的文件附件的rails库?

Are there any rails libraries for managing file attachments connected with ActiveRecord?

我知道回形针,但它似乎主要适合图像.他们确实在 github项目页面上提到了音频和pdf文件,但是对此用法没有进一步的解释.不同的文件类型.如果您上载音频文件,则:style之类的属性将更改其含义.因此,不同的文件大小不会以二维分辨率表示,而是会以比特率表示.

I know paperclip, but it seems suitable for images primarily. They indeed mention audio and pdf files on the github project page, but there's no further explanation about the usage of different file types. Attributes like :style would change their meaning if you uploaded an audio file. So different file sizes wouldn't be expressed in a two-dimensional resolution but in terms of bitrates.

回形针是否有其他选择?还是可能不只是将 imagemagick 与回形针链接,还可能是例如 ffmpeg ?

Are there any alternatives to paperclip? Or is it possible to not just link imagemagick with paperclip but for example ffmpeg?

推荐答案

以上问题包含多个方面.因此,我将尝试全部一一回答.

The question above contains more than one facette. So I'll try to answer them all one-by-one.

除了图像之外,还可以将回形针用于其他文件.您可以通过子类化 Paperclip :: Processor .以下代码显示了定制处理器实现的最小结构.可以使用自定义options来适应任何文件类型.

It is possible to use paperclip for other files than images. You can define custom import actions by subclassing Paperclip::Processor. The following code shows a minimal structure of a custom processor implementation. This can be adapted to any file type with custom options.

module Paperclip
  class FileContents < Processor

    def initialize file, options = {}, attachment = nil
      @file           = file
      @options        = options
      @instance       = attachment.instance
      @current_format = File.extname(attachment.instance.asset_file_name)
      @basename       = File.basename(@file.path, @current_format)
      @whiny          = options[:whiny].nil? ? true : options[:whiny]
    end

    def make
      begin

        # your import code (e.g. ocr or video resizing)...

        @file
      rescue StandardError => e
        raise PaperclipError, "There was an error processing the file contents for #{@basename} - #{e}" if @whiny
      end
    end
  end
end

回形针 ffmpeg

有人已经为视频文件编写了回形针处理器.看看 paperclip-ffmpeg的来源 gem,了解如何编写复杂的处理器.

paperclip and ffmpeg

Someone wrote a paperclip processor for video files already. Have a look at the source of paperclip-ffmpeg gem to see how complex processors are written.

以下是我找到的一些替代方法:

Here are some alternatives I found:

  • CarrierWave
  • Dragonfly
  • attachment_fu

关于Pro和Cons的问题已经在在stackoverflow上进行了讨论.

Pro and Cons are already discussed here on stackoverflow.

这篇关于Rails回形针仅适用于图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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