Why does Ruby open-uri's open return a StringIO in my unit test, but a FileIO in my controller?

前端 未结 3 704
梦谈多话
梦谈多话 2020-12-24 06:43

I inherited a Rails 2.2.2 app that stores user-uploaded images on Amazon S3. The attachment_fu-based Photo model offers a rotate method that uses <

3条回答
  •  隐瞒了意图╮
    2020-12-24 06:55

    The open-uri library uses a constant to set the 10KB size limit for StringIO objects.

    > OpenURI::Buffer::StringMax
    => 10240 
    

    You can change this setting to 0 to prevent open-uri from ever creating a StringIO object. Instead, this will force it to always generate a temp file.

    Just throw this in an initializer:

    # Don't allow downloaded files to be created as StringIO. Force a tempfile to be created.
    require 'open-uri'
    OpenURI::Buffer.send :remove_const, 'StringMax' if OpenURI::Buffer.const_defined?('StringMax')
    OpenURI::Buffer.const_set 'StringMax', 0
    

    You can't just set the constant directly. You need to actually remove the constant and then set it again (as above), otherwise you'll get a warning:

    warning: already initialized constant StringMax
    

    UPDATED 12/18/2012: Rails 3 doesn't require OpenURI by default, so you need to add require 'open-uri' at the top of the initializer. I updated the code above to reflect that change.

提交回复
热议问题