Is Rails Paperclip only for Images?

后端 未结 2 2040
傲寒
傲寒 2021-01-26 19:32

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

I know paperclip, but it seems suitable for images primarily. They inde

2条回答
  •  旧巷少年郎
    2021-01-26 20:26

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

    Paperclip::Processor

    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
    

    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.

    Alternatives

    Here are some alternatives I found:

    • CarrierWave
    • Dragonfly
    • attachment_fu

    Pro and Cons are already discussed here on stackoverflow.

提交回复
热议问题