Skip to content

Commit

Permalink
Merge pull request #626 from sul-dlss/escape-uri
Browse files Browse the repository at this point in the history
Add support for files with spaces
  • Loading branch information
justinlittman committed Mar 25, 2020
2 parents ba16e3e + 12853fb commit 1e01757
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 28 deletions.
30 changes: 30 additions & 0 deletions lib/file_uris.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# Builds the list of URIs for files in an object
class FileUris
# @param [String] druid
# @param [Cocina::Models::DRO]
def self.build(druid, obj)
filenames = extract_filenames(obj)

workspace = DruidTools::Druid.new(druid, File.absolute_path(Settings.sdr.local_workspace_root))
content_dir = workspace.find_filelist_parent('content', filenames)

filenames.map do |filename|
URI::File.build(path: File.join(content_dir, filename).gsub(' ', '%20')).to_s
end
end

def self.extract_filenames(obj)
filenames = []
obj.structural.contains.each do |fileset|
next if fileset.structural.contains.blank?

fileset.structural.contains.each do |file|
filenames << file.label
end
end
filenames
end
private_class_method :extract_filenames
end
29 changes: 1 addition & 28 deletions lib/robots/dor_repo/accession/technical_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,14 @@ def perform(druid)
# skip if no files
return LyberCore::Robot::ReturnState.new(status: :skipped, note: 'object has no files') if obj.structural.contains.blank?

file_uris = extract_file_uris(druid, obj)
file_uris = FileUris.build(druid, obj)
invoke_techmd_service(druid, file_uris)

LyberCore::Robot::ReturnState.new(status: :noop, note: 'Initiated technical metadata generation from technical-metadata-service.')
end

private

def object_for_druid
Dor::Services::Client.object(druid).find
end

def extract_filenames(obj)
filenames = []
obj.structural.contains.each do |fileset|
next if fileset.structural.contains.blank?

fileset.structural.contains.each do |file|
filenames << file.label
end
end
filenames
end

def extract_file_uris(druid, obj)
filenames = extract_filenames(obj)

workspace = DruidTools::Druid.new(druid, File.absolute_path(Settings.sdr.local_workspace_root))
content_dir = workspace.find_filelist_parent('content', filenames)

# In future Ruby's, this could be: filenames.map { |filename| URI::File.build(path: File.join(content_dir, filename)).to_s }
# However, in 2.5.3, URI::File does not exist.
filenames.map { |filename| "file://#{File.join(content_dir, filename)}" }
end

def invoke_techmd_service(druid, file_uris)
req = JSON.generate(druid: druid, files: file_uris)
resp = Faraday.post("#{Settings.tech_md_service.url}/v1/technical-metadata", req,
Expand Down
Empty file.
52 changes: 52 additions & 0 deletions spec/lib/file_uris_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

RSpec.describe FileUris do
let(:workspace) { File.absolute_path('spec/fixtures/workspace') }
let(:druid) { 'druid:dd116zh0343' }
let(:root) { File.absolute_path(Settings.sdr.local_workspace_root) }
let(:object) do
Cocina::Models::DRO.new(externalIdentifier: druid,
type: Cocina::Models::Vocab.object,
label: 'my repository object',
version: 1,
structural: {
contains: [{
externalIdentifier: '222',
type: Cocina::Models::Vocab.fileset,
label: 'my repository object',
version: 1,
structural: {
contains: [
{
externalIdentifier: '222-1',
label: filename,
type: Cocina::Models::Vocab.file,
version: 1
}
]
}
}]
})
end

before do
# For File URIs, need to use absolute paths
allow(Settings.sdr).to receive(:local_workspace_root).and_return(workspace)
end

describe '.build' do
subject { described_class.build(druid, object) }

context 'with a sub folder' do
let(:filename) { 'folder1PuSu/story1u.txt' }

it { is_expected.to eq ["file://#{root}/dd/116/zh/0343/dd116zh0343/content/folder1PuSu/story1u.txt"] }
end

context 'with a space' do
let(:filename) { 'file with space.txt' }

it { is_expected.to eq ["file://#{root}/dd/116/zh/0343/dd116zh0343/content/file%20with%20space.txt"] }
end
end
end

0 comments on commit 1e01757

Please sign in to comment.