ruby-on-railsrubyrspecshrine

Shrine - cannot read an attachment a second time


After migrating from Carrierwave to Shrine, some of the tests started to failed. I've noticed that when calling #read from the column where the Shrine uploader is mounted, it can only by called once. By doing a second #read will return an empty string. Maybe I'm missing some configuration but the same code works for Carrierwave (I know it is a different uploader but for what I'm experiencing migrating to Shrine needs very few/almost none changes to the codebase)

# shrine.rb -> for testing
require "shrine"
require "shrine/storage/s3"
require "shrine/storage/file_system"

Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
Shrine.plugin :determine_mime_type, analyzer: :mime_types

Shrine.storages = {
    cache: Shrine::Storage::FileSystem.new(
      "public",
      prefix: "tmp/cache",
    ),
    store: Shrine::Storage::FileSystem.new(
      "public",
      prefix: "tmp",
    ),
  }
# carrierwave.rb -> for testing

...

config.storage = :file
config.enable_processing = false

Solution

  • Before you can read the content agian, you have to reset the "read pointer". You can do this by calling .rewind.

    Shrine is able to upload any IO-like object that implement methods #read, #rewind, #eof? and #close whose behaviour matches the IO class.

    For Details see the getting_started and this examle.