Skip to content

Commit

Permalink
Merge pull request #5124 from sul-dlss/metadata_transfer_tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo authored Jul 11, 2024
2 parents 678744c + c966480 commit 2c3d045
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/services/publish/metadata_transfer_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ def workspace_content_pathname
def copy(src_pathname, dest_pathname)
return if dest_pathname.exist?

Rails.logger.info("Copying #{src_pathname} (#{src_pathname.size} bytes) to #{dest_pathname} for #{druid}")
FileUtils.copy(src_pathname, dest_pathname)

raise "Copy #{src_pathname} to #{dest_pathname} failed" unless dest_pathname.exist? && dest_pathname.size == src_pathname.size
end

def transfer_stage_pathname
Expand Down
15 changes: 13 additions & 2 deletions app/services/shelvable_files_stager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,26 @@ def stage

# Try to copy from preservation into the workspace
def copy_file_from_preservation(file_pathname:, filepath:)
Rails.logger.info("Copying #{filepath} from preservation to #{file_pathname} for #{druid}")
FileUtils.mkdir_p(file_pathname.dirname)
received_bytes = 0
File.open(file_pathname, 'wb') do |streamed|
writer = proc do |chunk, _overall_received_bytes|
writer = proc do |chunk, overall_received_bytes|
streamed.write chunk
received_bytes = overall_received_bytes
end
Preservation::Client.objects.content(druid:, filepath:, version: version - 1, on_data: writer)
response = Preservation::Client.objects.content(druid:, filepath:, version: version - 1, on_data: writer)
check_filesize(file_pathname:, filepath:, expected: response&.headers&.[]('content-length')&.to_i, received: received_bytes)
end
rescue Preservation::Client::NotFoundError, Faraday::ResourceNotFound
file_pathname.delete if file_pathname.exist? # 404 body is written to file
raise FileNotFound, "Unable to find #{filepath} in the content directory"
end

def check_filesize(file_pathname:, filepath:, expected:, received:)
return if expected.nil? || expected == received

file_pathname.delete if file_pathname.exist?
raise "File copied from preservation was not the expected size. Expected #{expected} bytes for #{filepath}; received #{received} bytes."
end
end
10 changes: 10 additions & 0 deletions spec/services/shelvable_files_stager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@
expect { stage }.to raise_error(ShelvableFilesStager::FileNotFound)
end
end

context 'when the content length does not match' do
before do
allow(Preservation::Client.objects).to receive(:content).and_return(instance_double(Faraday::Response, headers: { 'content-length' => '42' }))
end

it 'raises' do
expect { stage }.to raise_error('File copied from preservation was not the expected size. Expected 42 bytes for file1.txt; received 0 bytes.')
end
end
end

0 comments on commit 2c3d045

Please sign in to comment.