Class: Ddr::Datastreams::DeleteExternalFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/ddr/datastreams/delete_external_files.rb

Class Method Summary collapse

Class Method Details

.call(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/ddr/datastreams/delete_external_files.rb', line 6

def self.call(*args)
  event = ActiveSupport::Notifications::Event.new(*args)
  case event.name
  when Ddr::Datastreams::DELETE
    delete_files(event.payload[:version_history])
  when Ddr::Models::Base::DELETE, Ddr::Models::Base::DEACCESSION
    delete_files(event.payload[:datastream_history].values.flatten)
  end
end

.delete_files(profiles) ⇒ Object



23
24
25
26
# File 'lib/ddr/datastreams/delete_external_files.rb', line 23

def self.delete_files(profiles)
  paths = get_files_to_delete(profiles)
  FileUtils.rm_f(paths) unless paths.empty?
end

.get_files_to_delete(profiles) ⇒ Object



16
17
18
19
20
21
# File 'lib/ddr/datastreams/delete_external_files.rb', line 16

def self.get_files_to_delete(profiles)
  return [] if profiles.empty?
  profiles
    .select { |prof| (prof["dsControlGroup"] == "E") && prof["dsLocation"].start_with?("file:") }
    .map    { |prof| Ddr::Utils.path_from_uri(prof["dsLocation"]) }
end