Dan*_*pin 5 ruby-on-rails carrierwave rails-activestorage
由于多种原因,我将上传内容从 ActiveStorage (AS) 迁移到 CarrierWave (CW)。
我正在制作 rake 任务并整理逻辑 - 我对如何将 AS blob 馈送到 CW 文件感到困惑。
我正在尝试类似的事情:
@files.each.with_index(1) do | a, index |
if a.attachment.attached?
a.attachment.download do |file|
a.file = file
end
a.save!
end
end
Run Code Online (Sandbox Code Playgroud)
这是基于这两个链接:
https://edgeguides.rubyonrails.org/active_storage_overview.html#downloading-files
message.video.open do |file|
system '/path/to/virus/scanner', file.path
# ...
end
Run Code Online (Sandbox Code Playgroud)
和
https://github.com/rierwaveuploader/rierwave#activerecord
# like this
File.open('somewhere') do |f|
u.avatar = f
end
Run Code Online (Sandbox Code Playgroud)
我在本地测试了这个,文件不是通过上传器安装的。我的问题是:
奖金业力问题:
file从块中获得的对象是attachment.download一个字符串。.download更准确地说,来自文件的响应“以块的形式进行流式传输和生成”(请参阅文档)。我通过打电话来确认这一点,file.class以确保课程符合我的预期。
.read因此,要解决您的问题,您需要提供一个可以调用的对象。通常这是使用 Ruby StringIO类完成的。
但是,考虑到Carrierwave 也需要一个 filename,您可以使用继承的辅助模型来解决它StringIO(来自上面链接的博客文章):
class FileIO < StringIO
def initialize(stream, filename)
super(stream)
@original_filename = filename
end
attr_reader :original_filename
end
Run Code Online (Sandbox Code Playgroud)
然后你可以替换a.file = file为a.file = FileIO.new(file, 'new_filename')
| 归档时间: |
|
| 查看次数: |
1213 次 |
| 最近记录: |