读取 rails 种子文件后图像文件被删除
Image file getting removed after reading in rails seeds file
在 rails 种子文件中使用它来选择随机图像。
Room.all.each do |room|
count = rand(1..5)
count.times do
room.photos.create!(
image: File.new(Dir['app/assets/images/sampleimages/*.jpg'].sample))
end
end
但是在阅读它之后从我的资产文件夹中删除了该图像。这里可能是什么问题?上面的代码是应该这样做还是与 shrine
(image uploader)?
有关
使用版本 Rails 5.2.0
和 shrine 2.10.1
。
我满imageUploader.rb
class ImageUploader < Shrine
include ImageProcessing::MiniMagick
plugin :processing
plugin :determine_mime_type
plugin :remove_attachment
plugin :store_dimensions
plugin :validation_helpers
plugin :versions
plugin :pretty_location
plugin :delete_raw
Attacher.validate do
validate_max_size 5.megabytes, message: 'is too large (max is 5 MB)'
validate_mime_type_inclusion ['image/jpeg', 'image/png', 'image/gif']
end
def process(io, context)
case context[:phase]
when :store
original = io.download
pipeline = ImageProcessing::MiniMagick.source(original)
size_300 = pipeline.resize_to_fit!(300, 300)
size_150 = pipeline.resize_to_fill!(150, 150)
original.close!
{original: io, medium: size_300, thumb: size_150}
end
end
end
如果加载 delete_raw
插件,原始文件对象将在上传后自动删除。建议在使用 versions
时加载此插件,因为您希望处理后的图像缩略图在上传后在本地删除。但是,它有一个不幸的副作用,即默认情况下它还会删除输入文件。
分配的文件将上传到临时存储,因此您可以通过告诉 delete_raw
插件仅删除上传到永久存储的原始文件来解决此问题:
plugin :delete_raw, storages: [:store]
在 rails 种子文件中使用它来选择随机图像。
Room.all.each do |room|
count = rand(1..5)
count.times do
room.photos.create!(
image: File.new(Dir['app/assets/images/sampleimages/*.jpg'].sample))
end
end
但是在阅读它之后从我的资产文件夹中删除了该图像。这里可能是什么问题?上面的代码是应该这样做还是与 shrine
(image uploader)?
使用版本 Rails 5.2.0
和 shrine 2.10.1
。
我满imageUploader.rb
class ImageUploader < Shrine
include ImageProcessing::MiniMagick
plugin :processing
plugin :determine_mime_type
plugin :remove_attachment
plugin :store_dimensions
plugin :validation_helpers
plugin :versions
plugin :pretty_location
plugin :delete_raw
Attacher.validate do
validate_max_size 5.megabytes, message: 'is too large (max is 5 MB)'
validate_mime_type_inclusion ['image/jpeg', 'image/png', 'image/gif']
end
def process(io, context)
case context[:phase]
when :store
original = io.download
pipeline = ImageProcessing::MiniMagick.source(original)
size_300 = pipeline.resize_to_fit!(300, 300)
size_150 = pipeline.resize_to_fill!(150, 150)
original.close!
{original: io, medium: size_300, thumb: size_150}
end
end
end
如果加载 delete_raw
插件,原始文件对象将在上传后自动删除。建议在使用 versions
时加载此插件,因为您希望处理后的图像缩略图在上传后在本地删除。但是,它有一个不幸的副作用,即默认情况下它还会删除输入文件。
分配的文件将上传到临时存储,因此您可以通过告诉 delete_raw
插件仅删除上传到永久存储的原始文件来解决此问题:
plugin :delete_raw, storages: [:store]