Sha256: 14f4ec3d2bc992f91089963db408bdc503c7890e7daa770390e2f5d195a5aa51

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

class IngestLocalFileJob
  attr_accessor :directory, :filename, :user_key, :generic_file_id

  def queue_name
    :ingest
  end

  def initialize(generic_file_id, directory, filename, user_key)
    self.generic_file_id = generic_file_id
    self.directory = directory
    self.filename = filename
    self.user_key = user_key
  end

  # TODO: this should use Actor#create_content
  def run
    user = User.find_by_user_key(user_key)
    raise "Unable to find user for #{user_key}" unless user
    generic_file = GenericFile.find(generic_file_id)
    path = File.join(directory, filename)

    actor = Sufia::GenericFile::Actor.new(generic_file, user)

    if actor.create_content(File.open(path), filename, 'content', mime_type(filename))
      FileUtils.rm(path)
      Sufia.queue.push(ContentDepositEventJob.new(generic_file.id, user_key))

      message = "The file (#{File.basename(filename)}) was successfully deposited."
      subject = 'Local file ingest'
    else
      message = "There was a problem depositing #{File.basename(filename)}. Please contact a system admin."
      subject = 'Local file ingest error'
    end

    job_user.send_message(user, message, subject)
  end

  def job_user
    User.batchuser
  end

  def mime_type(file_name)
    mime_types = MIME::Types.of(file_name)
    mime_types.empty? ? "application/octet-stream" : mime_types.first.content_type
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sufia-models-6.7.0 app/jobs/ingest_local_file_job.rb
sufia-models-6.6.1 app/jobs/ingest_local_file_job.rb
sufia-models-6.6.0 app/jobs/ingest_local_file_job.rb
sufia-models-6.5.0 app/jobs/ingest_local_file_job.rb
sufia-models-6.4.0 app/jobs/ingest_local_file_job.rb
sufia-models-6.3.0 app/jobs/ingest_local_file_job.rb