Class: IngestLocalFileJob

Inherits:
Object
  • Object
show all
Defined in:
app/jobs/ingest_local_file_job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generic_file_id, directory, filename, user_key) ⇒ IngestLocalFileJob

Returns a new instance of IngestLocalFileJob.



8
9
10
11
12
13
# File 'app/jobs/ingest_local_file_job.rb', line 8

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

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



2
3
4
# File 'app/jobs/ingest_local_file_job.rb', line 2

def directory
  @directory
end

#filenameObject

Returns the value of attribute filename.



2
3
4
# File 'app/jobs/ingest_local_file_job.rb', line 2

def filename
  @filename
end

#generic_file_idObject

Returns the value of attribute generic_file_id.



2
3
4
# File 'app/jobs/ingest_local_file_job.rb', line 2

def generic_file_id
  @generic_file_id
end

#user_keyObject

Returns the value of attribute user_key.



2
3
4
# File 'app/jobs/ingest_local_file_job.rb', line 2

def user_key
  @user_key
end

Instance Method Details

#job_userObject



38
39
40
# File 'app/jobs/ingest_local_file_job.rb', line 38

def job_user
  User.batchuser
end

#mime_type(file_name) ⇒ Object



42
43
44
45
# File 'app/jobs/ingest_local_file_job.rb', line 42

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

#queue_nameObject



4
5
6
# File 'app/jobs/ingest_local_file_job.rb', line 4

def queue_name
  :ingest
end

#runObject

TODO: this should use Actor#create_content



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/jobs/ingest_local_file_job.rb', line 16

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