Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions app/models/concerns/digital_object/assets/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,11 @@ def validate_import_file_data_if_present
@errors.add(:import_file, "import_file.#{resource_name}.original_file_path contains invalid UTF-8 characters.")
end

# Make sure that there isn't already another Asset with a main file that points to this same file
pid = Hyacinth::Utils::FedoraUtils.find_object_pid_by_filesystem_path(import_location)
if pid.present?
# If this object is in Fedora but isn't in Hyacinth, then there's no problem here.
# But if it's in Hyacinth AND that Hyacinth object is active, then that is a problem
# and we should prevent a duplicate import.
possible_hyacinth_object = DigitalObject::Base.find_by_pid(pid)
if possible_hyacinth_object.present? && possible_hyacinth_object.state == ::DigitialObject::Base::STATE_ACTIVE
@errors.add(:import_file, "Found existing active Hyacinth Asset (#{pid}) with main file path: #{import_location}")
end
# Make sure that there isn't already another active Asset in Hyacinth with a main file that points to this same file
import_location_as_uri = import_location.start_with?('/') ? Hyacinth::Utils::UriUtils.file_path_to_location_uri(import_location) : import_location
possible_asset = DigitalObject::Asset.find_by_resource_location_uri(::DigitalObject::Asset::MAIN_RESOURCE_NAME, import_location_as_uri)
if possible_asset.present? && possible_asset.state == ::DigitalObject::Base::STATE_ACTIVE
@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)}")
end
end

Expand Down
12 changes: 12 additions & 0 deletions app/models/digital_object/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ class DigitalObject::Asset < DigitalObject::Base

attr_accessor :restricted_size_image, :restricted_onsite


def self.find_by_resource_location_uri(resource_name, location_uri)
pid_for_possible_asset = DigitalObject::Base.search(
'fq' => {"resource_#{resource_name}_location_si" => [{'equals' => location_uri}]},'fl' => 'pid','per_page' => 1
).dig('results', 0, 'pid')

return nil if pid_for_possible_asset.nil?
asset = DigitalObject::Base.find_by_pid(pid_for_possible_asset)

asset.state == ::DigitalObject::Base::STATE_ACTIVE ? asset : nil
end

def initialize
super

Expand Down