Paperclip处理器返回文件不变

时间:2014-08-04 05:31:41

标签: ruby ruby-on-rails-4 paperclip

我正在创建一个Paperclip处理器,如果图像大于阈值,它会将图像缩小到某个阈值以下。如果图像已经足够小,它应该保持图像不变。

当文件大于阈值时,处理器运行良好但是当文件保持不变时,Paperclip会抛出IOError: closed stream。我应该从#make方法返回什么来避免错误?从原始文件创建并返回重复的临时文件只是错误。

module Paperclip
  class Shrinker < Processor

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

    def make
      if @file.size > 4000000
        do_the_processing_and_return_a_tempfile
      else
        @file
      end
    end

    ...

  end
end

堆栈跟踪结束:

 # /home/mika/.rvm/gems/ruby-2.1.1/gems/paperclip-4.1.1/lib/paperclip/io_adapters/abstract_adapter.rb:8:in `rewind'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/paperclip-4.1.1/lib/paperclip/storage/filesystem.rb:52:in `block in flush_writes'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/paperclip-4.1.1/lib/paperclip/storage/filesystem.rb:37:in `each'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/paperclip-4.1.1/lib/paperclip/storage/filesystem.rb:37:in `flush_writes'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/paperclip-4.1.1/lib/paperclip/attachment.rb:239:in `save'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/paperclip-4.1.1/lib/paperclip/has_attached_file.rb:88:in `block in add_active_record_callbacks'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:438:in `instance_exec'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:438:in `block in make_lambda'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:221:in `call'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:221:in `block in halting_and_conditional'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `call'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.0/lib/active_support/callbacks.rb:86:in `run_callbacks'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/callbacks.rb:302:in `create_or_update'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/persistence.rb:125:in `save!'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/validations.rb:57:in `save!'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/transactions.rb:273:in `block in save!'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `block in transaction'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `within_new_transaction'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/transactions.rb:208:in `transaction'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
 # /home/mika/.rvm/gems/ruby-2.1.1/gems/activerecord-4.1.0/lib/active_record/transactions.rb:273:in `save!'
 # ./app/models/email.rb:254:in `block in save_images'
 # ./app/models/email.rb:243:in `each'
 # ./app/models/email.rb:243:in `save_images'

我的处理器没有显示在堆栈跟踪中,但它已运行并且是问题的根源。

1 个答案:

答案 0 :(得分:2)

似乎@file实例是一个已关闭的文件对象,并且回形针在使用封闭的文件流时遇到问题。

您可以尝试返回

File.new(@file.path)

而不是@file从文件创建新的打开文件流,以便回形针能够无问题地操作打开的文件流。