Module: Dor::Contentable

Extended by:
ActiveSupport::Concern, Deprecation
Included in:
Item
Defined in:
lib/dor/models/concerns/contentable.rb

Instance Method Summary collapse

Instance Method Details

#add_constituent(druid) ⇒ Object

TODO: Move to Dor-Utils. Adds a RELS-EXT constituent relationship to the given druid

Parameters:

  • druid (String)

    the parent druid of the constituent relationship e.g.: <fedora:isConstituentOf rdf:resource=“info:fedora/druid:hj097bm8879” />



192
193
194
# File 'lib/dor/models/concerns/contentable.rb', line 192

def add_constituent(druid)
  add_relationship :is_constituent_of, ActiveFedora::Base.find(druid)
end

#add_file(file, resource, file_name, mime_type = nil, publish = 'no', shelve = 'no', preserve = 'no') ⇒ Object

add a file to a resource, not to be confused with add a resource to an object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dor/models/concerns/contentable.rb', line 10

def add_file(file, resource, file_name, mime_type = nil, publish = 'no', shelve = 'no', preserve = 'no')
  xml = datastreams['contentMetadata'].ng_xml
  # make sure the resource exists
  raise 'resource doesnt exist.' if xml.search('//resource[@id=\'' + resource + '\']').length == 0

  sftp = Net::SFTP.start(Config.content.content_server, Config.content.content_user, auth_methods: ['publickey'])
  druid_tools = DruidTools::Druid.new(pid, Config.content.content_base_dir)
  location = druid_tools.path(file_name)
  oldlocation = location.gsub('/' + pid.gsub('druid:', ''), '')
  md5  = Digest::MD5.file(file.path).hexdigest
  sha1 = Digest::SHA1.file(file.path).hexdigest
  size = File.size?(file.path)
  # update contentmd
  file_hash = { name: file_name, md5: md5, publish: publish, shelve: shelve, preserve: preserve, size: size.to_s, sha1: sha1, mime_type: mime_type }
  begin
    sftp.stat!(location.gsub(file_name, ''))
    begin
      sftp.stat!(location)
      raise "The file #{file_name} already exists!"
    rescue Net::SFTP::StatusException
      sftp.upload!(file.path, location)
      .add_file file_hash, resource
    end
  rescue Net::SFTP::StatusException
    # directory layout doesn't match the new style, so use the old style.
    begin
      sftp.stat!(oldlocation)
      raise "The file #{file_name} already exists!"
    rescue Net::SFTP::StatusException
      # file doesn't already exist, which is good. Add it
      sftp.upload!(file.path, oldlocation)
      .add_file file_hash, resource
    end
  end
  # can only arrive at this point if a non status exception occurred.
end

#decommission(tag) ⇒ Object

Clears RELS-EXT relationships, sets the isGovernedBy relationship to the SDR Graveyard APO

Parameters:

  • tag (String)

    optional String of text that is concatenated to the identityMetadata/tag “Decommissioned : ”



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/dor/models/concerns/contentable.rb', line 172

def decommission(tag)
  # remove isMemberOf and isMemberOfCollection relationships
  clear_relationship :is_member_of
  clear_relationship :is_member_of_collection
  # remove isGovernedBy relationship
  clear_relationship :is_governed_by
  # add isGovernedBy to graveyard APO druid:sw909tc7852
  # SEARCH BY dc title for 'SDR Graveyard'
  add_relationship :is_governed_by, ActiveFedora::Base.find(Dor::SearchService.sdr_graveyard_apo_druid)
  # eliminate contentMetadata. set it to <contentMetadata/> ?
  .content = '<contentMetadata/>'
  # eliminate rightsMetadata. set it to <rightsMetadata/> ?
  .content = '<rightsMetadata/>'
  TagService.add self, "Decommissioned : #{tag}"
end

#get_file(file) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dor/models/concerns/contentable.rb', line 76

def get_file(file)
  druid_tools = DruidTools::Druid.new(pid, Config.content.content_base_dir)
  location = druid_tools.path(file)
  oldlocation = location.gsub('/' + file, '').gsub('/' + pid.gsub('druid:', ''), '') + '/' + file
  sftp = Net::SFTP.start(Config.content.content_server, Config.content.content_user, auth_methods: ['publickey'])
  begin
    data = sftp.download!(location)
  rescue StandardError
    data = sftp.download!(oldlocation)
  end
  data
end

#get_preserved_file(file, version) ⇒ Object



71
72
73
# File 'lib/dor/models/concerns/contentable.rb', line 71

def get_preserved_file(file, version)
  Sdr::Client.get_preserved_file_content(pid, file, version)
end

#is_file_in_workspace?(filename) ⇒ Boolean

Returns whether the file in question is present in the object’s workspace.

Parameters:

  • filename (String)

Returns:

  • (Boolean)

    whether the file in question is present in the object’s workspace



164
165
166
167
# File 'lib/dor/models/concerns/contentable.rb', line 164

def is_file_in_workspace?(filename)
  druid_obj = DruidTools::Druid.new(pid, Dor::Config.stacks.local_workspace_root)
  !druid_obj.find_content(filename).nil?
end

#list_filesArray

list files in the workspace

Returns:

  • (Array)

    workspace files



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dor/models/concerns/contentable.rb', line 138

def list_files
  filename = 'none'
  files = []
  sftp = Net::SFTP.start(Config.content.content_server, Config.content.content_user, auth_methods: ['publickey'])
  druid_tools = DruidTools::Druid.new(pid, Config.content.content_base_dir)
  location = druid_tools.path(filename).gsub(filename, '')
  oldlocation = location.gsub('/' + pid.gsub('druid:', ''), '')
  begin
    sftp.dir.entries(location, '*') do |file|
      files << file.name
    end
  rescue StandardError
    begin
      sftp.dir.glob(oldlocation, '*') do |file|
        files << file.name
      end
    rescue Net::SFTP::StatusException
      return files
    end
  end
  files
end

#remove_file(filename) ⇒ Object

Parameters:

  • filename (String)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dor/models/concerns/contentable.rb', line 91

def remove_file(filename)
  druid_tools = DruidTools::Druid.new(pid, Config.content.content_base_dir)
  location = druid_tools.path(filename)
  oldlocation = location.gsub('/' + pid.gsub('druid:', ''), '')
  sftp = Net::SFTP.start(Config.content.content_server, Config.content.content_user, auth_methods: ['publickey'])
  begin
    sftp.remove!(location)
  rescue StandardError
    # if the file doesnt exist, that is ok, not all files will be present in the workspace
    begin
      sftp.remove!(oldlocation)
    rescue Net::SFTP::StatusException
    end
  end
  .remove_file filename
end

#remove_resource(resource_name) ⇒ Object

Parameters:

  • resource_name (String)

    ID of the resource elememnt



126
127
128
129
130
131
132
133
# File 'lib/dor/models/concerns/contentable.rb', line 126

def remove_resource(resource_name)
  # run delete for all of the files in the resource
  .ng_xml.search('//resource[@id=\'' + resource_name + '\']/file').each do |file|
    remove_file(file['id'])
  end
  # remove the resource record from the metadata and renumber the resource sequence
  .remove_resource resource_name
end

#rename_file(old_name, new_name) ⇒ Object

Parameters:

  • old_name (String)
  • new_name (String)


111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dor/models/concerns/contentable.rb', line 111

def rename_file(old_name, new_name)
  druid_tools = DruidTools::Druid.new(pid, Config.content.content_base_dir)
  location = druid_tools.path(old_name)
  oldlocation = location.gsub('/' + pid.gsub('druid:', ''), '')
  sftp = Net::SFTP.start(Config.content.content_server, Config.content.content_user, auth_methods: ['publickey'])
  begin
    sftp.rename!(location, location.gsub(old_name, new_name))
  rescue StandardError
    sftp.rename!(oldlocation, oldlocation.gsub(old_name, new_name))
  end
  .rename_file(old_name, new_name)
end

#replace_file(file, file_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/dor/models/concerns/contentable.rb', line 48

def replace_file(file, file_name)
  sftp = Net::SFTP.start(Config.content.content_server, Config.content.content_user, auth_methods: ['publickey'])
  item = Dor.find(pid)
  druid_tools = DruidTools::Druid.new(pid, Config.content.content_base_dir)
  location = druid_tools.path(file_name)
  oldlocation = location.gsub('/' + pid.gsub('druid:', ''), '')
  md5  = Digest::MD5.file(file.path).hexdigest
  sha1 = Digest::SHA1.file(file.path).hexdigest
  size = File.size?(file.path)
  # update contentmd
  file_hash = { name: file_name, md5: md5, size: size.to_s, sha1: sha1 }
  begin
    sftp.stat!(location)
    sftp.upload!(file.path, location)
    # this doesnt allow renaming files
    item..update_file(file_hash, file_name)
  rescue StandardError
    sftp.upload!(file.path, oldlocation)
    item..update_file(file_hash, file_name)
  end
end