Sha256: 753e5888b04ff9fc62a21ff78d2d21287f28f38d342b2b0d758035e71510106b

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module ComfortableMediaSurfer::Seeds::File
  class Importer < ComfortableMediaSurfer::Seeds::Importer
    def initialize(from, to = from)
      super
      self.path = ::File.join(ComfortableMediaSurfer.config.seeds_path, from, 'files/')
    end

    def import!
      Dir["#{path}[^_]*"].each do |file_path|
        filename = ::File.basename(file_path)

        file = site.files.with_attached_attachment
          .where('active_storage_blobs.filename' => filename).references(:blob).first ||
               site.files.new

        # We need to track actual file and its attributes
        fresh_file = false

        if File.exist?(attrs_path = File.join(path, "_#{filename}.yml")) && fresh_seed?(file, attrs_path)
          fresh_file = true

          attrs = YAML.safe_load_file(attrs_path)
          category_ids = category_names_to_ids(file, attrs.delete('categories'))
          file.attributes = attrs.merge(
            category_ids: category_ids
          )
        end

        if fresh_seed?(file, file_path)
          fresh_file = true

          file_handler = File.open(file_path)
          file.file = {
            io: file_handler,
            filename: filename,
            content_type: MimeMagic.by_magic(file_handler)
          }
        end

        if fresh_file
          if file.save
            message = "[CMS SEEDS] Imported File \t #{file_path}"
            ComfortableMediaSurfer.logger.info(message)
          else
            message = "[CMS SEEDS] Failed to import File \n#{file.errors.inspect}"
            ComfortableMediaSurfer.logger.warn(message)
          end
        end

        seed_ids << file.id
      end

      # cleaning up
      site.files.where('id NOT IN (?)', seed_ids).destroy_all
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
comfortable_media_surfer-3.0.0 lib/comfortable_media_surfer/seeds/file/importer.rb