Abhilash
Abhilash

Reputation: 2953

Image file getting removed after reading in rails seeds file

Using this in rails seeds file for picking random images.

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

But after reading its removing that image from my assets folder. What could be the issue here? Is the above code suppose to do that or is it something to do with shrine(image uploader)?

Using version Rails 5.2.0 and shrine 2.10.1.

My full 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

Upvotes: 0

Views: 307

Answers (1)

Janko
Janko

Reputation: 9305

Raw file objects will get automatically deleted after upload if delete_raw plugin is loaded. It's recommended to load this plugin when using versions, as you want processed image thumbnails to get deleted locally after they're uploaded. However, it has an unfortunate side effect that by default it will also delete input files.

Assigned files will get uploaded to temporary storage, so you can work around this by telling the delete_raw plugin to only delete raw files that are uploaded to permanent storage:

plugin :delete_raw, storages: [:store]

Upvotes: 1

Related Questions