Sha256: 87068c7d73a48afe86483c1e9c1690f9102a57112d20cbe73624a5955d40f54d

Contents?: true

Size: 1.79 KB

Versions: 12

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true
module Bulkrax
  module ExportBehavior
    extend ActiveSupport::Concern

    delegate :export_type, :exporter_export_path, to: :importerexporter

    def build_for_exporter
      build_export_metadata
      write_files if export_type == 'full' && !importerexporter.parser_klass.include?('Bagit')
    rescue RSolr::Error::Http, CollectionsCreatedError => e
      raise e
    rescue StandardError => e
      status_info(e)
    else
      status_info
    end

    def build_export_metadata
      raise StandardError, 'not implemented'
    end

    def hyrax_record
      @hyrax_record ||= ActiveFedora::Base.find(self.identifier)
    end

    def write_files
      return if hyrax_record.is_a?(Collection)

      file_sets = hyrax_record.file_set? ? Array.wrap(hyrax_record) : hyrax_record.file_sets
      file_sets.each do |fs|
        path = File.join(exporter_export_path, 'files')
        FileUtils.mkdir_p(path)
        file = filename(fs)
        require 'open-uri'
        io = open(fs.original_file.uri)
        next if file.blank?
        File.open(File.join(path, file), 'wb') do |f|
          f.write(io.read)
          f.close
        end
      end
    end

    # Prepend the file_set id to ensure a unique filename
    def filename(file_set)
      return if file_set.original_file.blank?
      fn = file_set.original_file.file_name.first
      mime = Mime::Type.lookup(file_set.original_file.mime_type)
      ext_mime = MIME::Types.of(file_set.original_file.file_name).first
      if fn.include?(file_set.id) || importerexporter.metadata_only?
        return fn if mime.to_s == ext_mime.to_s
        return "#{fn}.#{mime.to_sym}"
      else
        return "#{file_set.id}_#{fn}" if mime.to_s == ext_mime.to_s
        return "#{file_set.id}_#{fn}.#{mime.to_sym}"
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
bulkrax-3.1.1 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.1.0 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.0.1 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.0.0 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.0.0.beta8 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.0.0.beta7 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.0.0.beta6 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.0.0.beta5 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.0.0.beta4 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.0.0.beta3 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.0.0.beta2 app/models/concerns/bulkrax/export_behavior.rb
bulkrax-3.0.0.beta1 app/models/concerns/bulkrax/export_behavior.rb