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



35
36
37
# File 'app/jobs/ingest_local_file_job.rb', line 35

def job_user
  User.batchuser
end

#queue_nameObject



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

def queue_name
  :ingest
end

#runObject



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

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)

  generic_file.label = File.basename(filename)
  generic_file.add_file(File.open(path), 'content', generic_file.label)
  generic_file.record_version_committer(user)
  generic_file.save!

  FileUtils.rm(path)
  Sufia.queue.push(ContentDepositEventJob.new(generic_file.pid, user_key))
  # add message to user for downloaded file
  message = "The file (#{File.basename(filename)}) was successfully deposited."
  job_user.send_message(user, message, 'Local file ingest')
rescue => error
  job_user.send_message(user, error.message, 'Local file ingest error')
end