Skip to content
Open
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
17 changes: 11 additions & 6 deletions app/parsers/bulkrax/csv_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,17 @@ def file_paths
raise StandardError, "Record references local files but no files directory could be resolved from the import path" if files_dir.nil?

r[file_mapping].split(split_pattern).map do |f|
file = File.join(files_dir, f.strip.tr(' ', '_'))
if File.exist?(file) # rubocop:disable Style/GuardClause
file
else
raise "File #{file} does not exist"
end
reference = f.strip
# Try the reference as written first, then the space->underscore
# variant produced by #remove_spaces_from_filenames. The rename only
# rewrites the top level of files/ (see unzip_spec), so nested
# attachments keep their spaces on disk and only match as written.
file = [reference, reference.tr(' ', '_')].uniq
.map { |candidate| File.join(files_dir, candidate) }
.find { |candidate| File.exist?(candidate) }
raise "File #{File.join(files_dir, reference)} does not exist" if file.nil?

file
end
end.flatten.compact.uniq
end
Expand Down
24 changes: 24 additions & 0 deletions spec/parsers/bulkrax/csv_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,30 @@ module Bulkrax
end
end

context 'when a record references file names containing spaces' do
before do
allow(subject).to receive(:records).and_return([{ file: 'subdir/has space.jpg|top space.jpg' }])
allow(subject).to receive(:path_to_files).and_return('spec/fixtures/csv/files')
end

it 'resolves nested files as written and top-level files via the underscored variant' do
allow(File).to receive(:exist?).and_return(false)
allow(File).to receive(:exist?).with('spec/fixtures/csv/files/subdir/has space.jpg').and_return(true)
allow(File).to receive(:exist?).with('spec/fixtures/csv/files/top_space.jpg').and_return(true)

expect(subject.file_paths).to contain_exactly(
'spec/fixtures/csv/files/subdir/has space.jpg',
'spec/fixtures/csv/files/top_space.jpg'
)
end

it 'raises naming the as-written reference when neither variant exists' do
allow(File).to receive(:exist?).and_return(false)

expect { subject.file_paths }.to raise_error(RuntimeError, %r{subdir/has space\.jpg does not exist})
end
end

context 'when a record file value is blank' do
before do
allow(subject).to receive(:records).and_return([{ file: '' }, { file: nil }])
Expand Down
Loading