Skip to content

Commit 6ec4b0d

Browse files
authored
Merge pull request #481 from cul/risearch-query-escape-dc-source-lookup
When performing an risearch query for a dc source path, escape colons in the path
2 parents edd8978 + 185373d commit 6ec4b0d

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

lib/hyacinth/utils/fedora_utils.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ def self.get_or_create_namespace_object(namespace_string)
6363
namespace_fedora_object
6464
end
6565

66+
def self.escape_path_or_uri_for_risearch_query(path_or_uri)
67+
path_or_uri.gsub(%q('), %q(\\\')).gsub(%q(:), %q(\\\:))
68+
end
69+
6670
def self.find_object_pid_by_filesystem_path(full_filesystem_path, active_only = true)
67-
query = "select $pid from <#ri> where $pid <http://purl.org/dc/elements/1.1/source> '#{full_filesystem_path.gsub(%q('), %q(\\\'))}'"
71+
query = "select $pid from <#ri> where $pid <http://purl.org/dc/elements/1.1/source> '#{escape_path_or_uri_for_risearch_query(full_filesystem_path)}'"
6872
query += " and $pid <info:fedora/fedora-system:def/model#state> <fedora-model:Active>" if active_only
6973
ri_opts = {
7074
type: 'tuples',

spec/lib/hyacinth/utils/fedora_utils_spec.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
require 'rails_helper'
22

33
RSpec.describe Hyacinth::Utils::FedoraUtils do
4-
5-
context ".find_object_pid_by_filesystem_path" do
4+
describe ".find_object_pid_by_filesystem_path" do
65
it "escapes single quotes in file paths" do
76
full_filesystem_path = %q(/some/path/cool-o'something-irish-filename.pdf)
87
escaped_full_filesystem_path = %q(/some/path/cool-o\\'something-irish-filename.pdf)
@@ -31,4 +30,29 @@
3130
end
3231
end
3332

33+
describe '.escape_path_or_uri_for_risearch_query' do
34+
it 'does not modify a path with safe characters' do
35+
expect(
36+
described_class.escape_path_or_uri_for_risearch_query("/path/to/file.tiff")
37+
).to eq(
38+
"/path/to/file.tiff"
39+
)
40+
end
41+
42+
it 'escapes a single quote in a path that contains single quote' do
43+
expect(
44+
described_class.escape_path_or_uri_for_risearch_query("/path/to/someone's-file.tiff")
45+
).to eq(
46+
"/path/to/someone\\'s-file.tiff"
47+
)
48+
end
49+
50+
it 'escapes a colon in a path that contains a colon' do
51+
expect(
52+
described_class.escape_path_or_uri_for_risearch_query('s3://bucket_name/path/to/file.tiff')
53+
).to eq(
54+
"s3\\://bucket_name/path/to/file.tiff"
55+
)
56+
end
57+
end
3458
end

0 commit comments

Comments
 (0)