Shrine - 无法第二次阅读附件

Shrine - cannot read an attachment a second time

从 Carrierwave 迁移到 Shrine 后,一些测试开始失败。我注意到当从挂载 Shrine 上传器的列调用 #read 时,它只能被调用一次。通过执行第二个 #read 将 return 一个空字符串。也许我缺少一些配置,但相同的代码适用于 Carrierwave(我知道它是一个不同的上传器,但对于我正在经历的迁移到 Shrine 需要对代码库进行非常 few/almost none 更改)

# 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

在您重新阅读内容之前,您必须重置 "read pointer"。 您可以通过调用 .rewind.

来完成此操作

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

有关详细信息,请参阅 getting_started and this examle