Sha256: e839d95a392c3c740177f848bea7f94ffe8f9b4b9f11ec359f867c7e515cfc93

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'open3'

module Ddr::Jobs
  class FitsFileCharacterization

    @queue = :file_characterization

    EVENT_SUMMARY = 'FITS characterization of content file'.freeze

    def self.perform(pid)
      obj = ActiveFedora::Base.find(pid)
      tmp_filename = Ddr::Utils::sanitize_filename(obj.original_filename) || obj.content.default_file_name
      Dir.mktmpdir(nil, Ddr::Models.tempdir) do |dir|
        infile = create_temp_infile(dir, tmp_filename, obj.content.content)
        fits_output, err, status = Open3.capture3(fits_command, '-i', infile)
        if status.success? && fits_output.present?
          obj.reload
          obj.fits.content = fits_output
          obj.save!
        end
        notify_event(pid, err, status)
      end
    end

    def self.fits_command
      File.join(Ddr::Models.fits_home, 'fits.sh')
    end

    def self.fits_version
      `#{fits_command} -v`.strip
    end

    private

    def self.create_temp_infile(dir, tmp_filename, content)
      temp_infile = File.join(dir, tmp_filename)
      File.open(temp_infile, 'wb') do |f|
        f.write content
      end
      temp_infile
    end

    def self.notify_event(pid, err, status)
      details = status.success? ? nil : err
      event_args = { pid: pid, summary: EVENT_SUMMARY, software:  "fits #{fits_version}", detail: details }
      event_args[:outcome] = status.success? ? Ddr::Events::Event::SUCCESS : Ddr::Events::Event::FAILURE
      Ddr::Notifications.notify_event(:update, event_args)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ddr-models-2.3.2 lib/ddr/jobs/fits_file_characterization.rb