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
6 changes: 5 additions & 1 deletion lib/hyacinth/utils/fedora_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <http://purl.org/dc/elements/1.1/source> '#{full_filesystem_path.gsub(%q('), %q(\\\'))}'"
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)}'"
query += " and $pid <info:fedora/fedora-system:def/model#state> <fedora-model:Active>" if active_only
ri_opts = {
type: 'tuples',
Expand Down
28 changes: 26 additions & 2 deletions spec/lib/hyacinth/utils/fedora_utils_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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