Skip to content

Commit 3b1ad5b

Browse files
authored
Merge pull request #482 from cul/solr-for-existing-asset-check
Use Hyacinth solr data when checking for an existing asset with a particular main resource uri (instead of using a Fedora risearch query)
2 parents 6ec4b0d + 105553c commit 3b1ad5b

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

app/models/concerns/digital_object/assets/validations.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,11 @@ def validate_import_file_data_if_present
4646
@errors.add(:import_file, "import_file.#{resource_name}.original_file_path contains invalid UTF-8 characters.")
4747
end
4848

49-
# Make sure that there isn't already another Asset with a main file that points to this same file
50-
pid = Hyacinth::Utils::FedoraUtils.find_object_pid_by_filesystem_path(import_location)
51-
if pid.present?
52-
# If this object is in Fedora but isn't in Hyacinth, then there's no problem here.
53-
# But if it's in Hyacinth AND that Hyacinth object is active, then that is a problem
54-
# and we should prevent a duplicate import.
55-
possible_hyacinth_object = DigitalObject::Base.find_by_pid(pid)
56-
if possible_hyacinth_object.present? && possible_hyacinth_object.state == ::DigitialObject::Base::STATE_ACTIVE
57-
@errors.add(:import_file, "Found existing active Hyacinth Asset (#{pid}) with main file path: #{import_location}")
58-
end
49+
# Make sure that there isn't already another active Asset in Hyacinth with a main file that points to this same file
50+
import_location_as_uri = import_location.start_with?('/') ? Hyacinth::Utils::UriUtils.file_path_to_location_uri(import_location) : import_location
51+
possible_asset = DigitalObject::Asset.find_by_resource_location_uri(::DigitalObject::Asset::MAIN_RESOURCE_NAME, import_location_as_uri)
52+
if possible_asset.present? && possible_asset.state == ::DigitalObject::Base::STATE_ACTIVE
53+
@errors.add(:import_file, "Found existing active Hyacinth Asset (#{possible_asset.pid}) with main file path: #{possible_asset.location_uri_for_resource(::DigitalObject::Asset::MAIN_RESOURCE_NAME)}")
5954
end
6055
end
6156

app/models/digital_object/asset.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ class DigitalObject::Asset < DigitalObject::Base
3636

3737
attr_accessor :restricted_size_image, :restricted_onsite
3838

39+
40+
def self.find_by_resource_location_uri(resource_name, location_uri)
41+
pid_for_possible_asset = DigitalObject::Base.search(
42+
'fq' => {"resource_#{resource_name}_location_si" => [{'equals' => location_uri}]},'fl' => 'pid','per_page' => 1
43+
).dig('results', 0, 'pid')
44+
45+
return nil if pid_for_possible_asset.nil?
46+
asset = DigitalObject::Base.find_by_pid(pid_for_possible_asset)
47+
48+
asset.state == ::DigitalObject::Base::STATE_ACTIVE ? asset : nil
49+
end
50+
3951
def initialize
4052
super
4153

0 commit comments

Comments
 (0)