diff --git a/lib/hyacinth/utils/fedora_utils.rb b/lib/hyacinth/utils/fedora_utils.rb index 8afa585a4..bc3137e5e 100644 --- a/lib/hyacinth/utils/fedora_utils.rb +++ b/lib/hyacinth/utils/fedora_utils.rb @@ -63,8 +63,12 @@ def self.get_or_create_namespace_object(namespace_string) namespace_fedora_object end + def self.escape_path_or_uri_for_risearch_query(path_or_uri) + path_or_uri.gsub(%q('), %q(\\\')).gsub(%q(:), %q(\\\:)) + end + def self.find_object_pid_by_filesystem_path(full_filesystem_path, active_only = true) - query = "select $pid from <#ri> where $pid '#{full_filesystem_path.gsub(%q('), %q(\\\'))}'" + query = "select $pid from <#ri> where $pid '#{escape_path_or_uri_for_risearch_query(full_filesystem_path)}'" query += " and $pid " if active_only ri_opts = { type: 'tuples', diff --git a/spec/lib/hyacinth/utils/fedora_utils_spec.rb b/spec/lib/hyacinth/utils/fedora_utils_spec.rb index 1a1abfd40..859fb27ae 100644 --- a/spec/lib/hyacinth/utils/fedora_utils_spec.rb +++ b/spec/lib/hyacinth/utils/fedora_utils_spec.rb @@ -1,8 +1,7 @@ require 'rails_helper' RSpec.describe Hyacinth::Utils::FedoraUtils do - - context ".find_object_pid_by_filesystem_path" do + describe ".find_object_pid_by_filesystem_path" do it "escapes single quotes in file paths" do full_filesystem_path = %q(/some/path/cool-o'something-irish-filename.pdf) escaped_full_filesystem_path = %q(/some/path/cool-o\\'something-irish-filename.pdf) @@ -31,4 +30,29 @@ end end + describe '.escape_path_or_uri_for_risearch_query' do + it 'does not modify a path with safe characters' do + expect( + described_class.escape_path_or_uri_for_risearch_query("/path/to/file.tiff") + ).to eq( + "/path/to/file.tiff" + ) + end + + it 'escapes a single quote in a path that contains single quote' do + expect( + described_class.escape_path_or_uri_for_risearch_query("/path/to/someone's-file.tiff") + ).to eq( + "/path/to/someone\\'s-file.tiff" + ) + end + + it 'escapes a colon in a path that contains a colon' do + expect( + described_class.escape_path_or_uri_for_risearch_query('s3://bucket_name/path/to/file.tiff') + ).to eq( + "s3\\://bucket_name/path/to/file.tiff" + ) + end + end end