【问题标题】:Shrine - cannot read an attachment a second timeShrine - 无法再次读取附件
【发布时间】:2020-04-09 03:12:43
【问题描述】:

从 Carrierwave 迁移到 Shrine 后,一些测试开始失败。我注意到,当从安装了 Shrine 上传器的列中调用 #read 时,它只能调用一次。通过第二次#read 将返回一个空字符串。也许我遗漏了一些配置,但相同的代码适用于 Carrierwave(我知道它是一个不同的上传器,但对于我正在经历的迁移到 Shrine 的情况需要对代码库进行很少/几乎不需要更改)

# 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?您能否也添加规范文件或至少它的相关部分?

标签: ruby-on-rails ruby rspec shrine


【解决方案1】:

在您可以重新读取内容之前,您必须重置“读取指针”。 您可以通过调用.rewind 来完成此操作。

Shrine 能够上传任何实现#read、#rewind、#eof 方法的类 IO 对象?和 #close ,其行为与 IO 类匹配。

详情请参阅getting_startedthis examle

【讨论】:

  • @jpac 你觉得这个答案有帮助吗?如果是,请将其标记为已接受。如果您遗漏了什么或不清楚的地方,请告诉我。
  • 您的解决方案有效。我还更改了代码以在读取之前使用#open,并确保文件最后关闭。
猜你喜欢
  • 1970-01-01
  • 2018-03-04
  • 2015-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 2011-05-25
  • 1970-01-01
相关资源
最近更新 更多