- begin
- src_manifest = src_collection.manifest_text || ''
-
- # Get all the locators from the original manifest
- locators = []
- src_manifest.each_line do |line|
- line.split(' ').each do |word|
- if match = Keep::Locator::LOCATOR_REGEXP.match(word)
- word = word.split('+')[0..1].join('+') # get rid of any hints
- locators << word if !word.start_with?('00000000000000000000000000000000')
- end
- end
- end
- locators << 'd41d8cd98f00b204e9800998ecf8427e+0' if !locators.any?
-
- # create new collection using 'arv-put' with original manifest_text as the data
- temp_file = Tempfile.new('temp')
- temp_file.write(src_manifest)
- temp_file.close
-
- new_manifest = salvage_collection_arv_put temp_file
-
- temp_file.unlink
-
- if !new_manifest.present?
- $stderr.puts "arv-put --as-stream failed for #{uuid}"
- return false
- end
-
- words = []
- new_manifest.split(' ').each do |word|
- if match = Keep::Locator::LOCATOR_REGEXP.match(word)
- word = word.split('+')[0..1].join('+') # get rid of any hints
- words << word
- else
- words << word
- end
- end
-
- new_manifest = words.join(' ') + "\n"
- new_collection = Collection.new
-
- total_size = 0
- locators.each do |locator|
- total_size += locator.split('+')[1].to_i
- end
- new_manifest += (". #{locators.join(' ')} 0:#{total_size}:salvaged_data\n")
-
- new_collection.name = "salvaged from #{src_collection.uuid}, #{src_collection.portable_data_hash}"
- new_collection.manifest_text = new_manifest
- new_collection.portable_data_hash = Digest::MD5.hexdigest(new_collection.manifest_text)
-
- created = new_collection.save!
- raise "New collection creation failed." if !created
-
- $stderr.puts "Salvaged manifest and data for #{uuid} are in #{new_collection.uuid}."
- puts "Created new collection #{new_collection.uuid}"
- rescue => error
- $stderr.puts "Error creating collection for #{uuid}: #{error}"
- return false
- end