Class: Sufia::GenericFile::Actor

Inherits:
Object
  • Object
show all
Defined in:
app/actors/sufia/generic_file/actor.rb

Overview

Actions are decoupled from controller logic so that they may be called from a controller or a background job.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generic_file, user) ⇒ Actor

Returns a new instance of Actor.



7
8
9
10
# File 'app/actors/sufia/generic_file/actor.rb', line 7

def initialize(generic_file, user)
  @generic_file = generic_file
  @user = user
end

Instance Attribute Details

#generic_fileObject (readonly)

Returns the value of attribute generic_file.



5
6
7
# File 'app/actors/sufia/generic_file/actor.rb', line 5

def generic_file
  @generic_file
end

#userObject (readonly)

Returns the value of attribute user.



5
6
7
# File 'app/actors/sufia/generic_file/actor.rb', line 5

def user
  @user
end

Class Method Details

.virus_check(file) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'app/actors/sufia/generic_file/actor.rb', line 112

def virus_check(file)
  path = file.is_a?(String) ? file : file.path
  unless defined?(ClamAV)
    ActiveFedora::Base.logger.warn "Virus checking disabled, #{path} not checked"
    return
  end
  scan_result = ClamAV.instance.scanfile(path)
  raise Sufia::VirusFoundError.new("A virus was found in #{path}: #{scan_result}") unless scan_result == 0
end

Instance Method Details

#create_content(file, file_name, path, mime_type) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'app/actors/sufia/generic_file/actor.rb', line 31

def create_content(file, file_name, path, mime_type)
  generic_file.add_file(file, path: path, original_name: file_name, mime_type: mime_type)
  generic_file.label ||= file_name
  generic_file.title = [generic_file.label] if generic_file.title.blank?
  save_characterize_and_record_committer do
    if Sufia.config.respond_to?(:after_create_content)
      Sufia.config.after_create_content.call(generic_file, user)
    end
  end
end

#create_metadata(batch_id) {|generic_file| ... } ⇒ Object

in order to avoid two saves in a row, create_metadata does not save the file by default. it is typically used in conjunction with create_content, which does do a save. If you want to save when using create_metadata, you can do this:

(batch_id) { |gf| gf.save }

Yields:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/actors/sufia/generic_file/actor.rb', line 16

def (batch_id)
  generic_file.(user)
  time_in_utc = DateTime.now.new_offset(0)
  generic_file.date_uploaded = time_in_utc
  generic_file.date_modified = time_in_utc
  generic_file.creator = [user.name]

  if batch_id
    generic_file.batch_id = batch_id
  else
    ActiveFedora::Base.logger.warn "unable to find batch to attach to"
  end
  yield(generic_file) if block_given?
end

#destroyObject



73
74
75
76
77
78
79
# File 'app/actors/sufia/generic_file/actor.rb', line 73

def destroy
  generic_file.destroy
  FeaturedWork.where(generic_file_id: generic_file.id).destroy_all
  if Sufia.config.respond_to?(:after_destroy)
    Sufia.config.after_destroy.call(generic_file.id, user)
  end
end

#push_characterize_jobObject



107
108
109
# File 'app/actors/sufia/generic_file/actor.rb', line 107

def push_characterize_job
  Sufia.queue.push(CharacterizeJob.new(@generic_file.id))
end

#revert_content(revision_id) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'app/actors/sufia/generic_file/actor.rb', line 42

def revert_content(revision_id)
  generic_file.content.restore_version(revision_id)
  generic_file.content.create_version
  save_characterize_and_record_committer do
    if Sufia.config.respond_to?(:after_revert_content)
      Sufia.config.after_revert_content.call(generic_file, user, revision_id)
    end
  end
end

#save_and_record_committerObject

Takes an optional block and executes the block if the save was successful. returns false if the save was unsuccessful



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/actors/sufia/generic_file/actor.rb', line 90

def save_and_record_committer
  save_tries = 0
  begin
    return false unless generic_file.save
  rescue RSolr::Error::Http => error
    ActiveFedora::Base.logger.warn "Sufia::GenericFile::Actor::save_and_record_committer Caught RSOLR error #{error.inspect}"
    save_tries+=1
    # fail for good if the tries is greater than 3
    raise error if save_tries >=3
    sleep 0.01
    retry
  end
  yield if block_given?
  generic_file.record_version_committer(user)
  true
end

#save_characterize_and_record_committerObject

Takes an optional block and executes the block if the save was successful.



82
83
84
85
86
# File 'app/actors/sufia/generic_file/actor.rb', line 82

def save_characterize_and_record_committer
  save_and_record_committer { push_characterize_job }.tap do |val|
    yield if block_given? && val
  end
end

#update_content(file, path) ⇒ Object



52
53
54
55
56
57
58
59
# File 'app/actors/sufia/generic_file/actor.rb', line 52

def update_content(file, path)
  generic_file.add_file(file, path: path, original_name: file.original_filename, mime_type: file.content_type)
  save_characterize_and_record_committer do
    if Sufia.config.respond_to?(:after_update_content)
      Sufia.config.after_update_content.call(generic_file, user)
    end
  end
end

#update_metadata(attributes, visibility) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'app/actors/sufia/generic_file/actor.rb', line 61

def (attributes, visibility)
  generic_file.attributes = attributes
  update_visibility(visibility)
  generic_file.date_modified = DateTime.now
  remove_from_feature_works if generic_file.visibility_changed? && !generic_file.public?
  save_and_record_committer do
    if Sufia.config.respond_to?(:after_update_metadata)
      Sufia.config..call(generic_file, user)
    end
  end
end