Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change content type back to application/json for H2 files #585

Merged
merged 6 commits into from
Jun 15, 2023
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
10 changes: 9 additions & 1 deletion app/controllers/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ def get_fileset_uuid(external_id)
def metadata_for_blob(blob, file)
file.delete(:externalIdentifier)
file[:size] = blob.byte_size
file[:hasMimeType] = blob.content_type || 'application/octet-stream'
# Invalid JSON files uploaded for deposit with a JSON content type will trigger 400 errors in sdr-api since they are
# parsed as JSON and rejected. The work around is to change the content_type in the request for uploads like this
# to something specific that will be changed back to application/json after upload is complete.
# There is a corresponding translation in sdr-client. See https://github.com/sul-dlss/happy-heron/issues/3075
file[:hasMimeType] = if blob.content_type == 'application/x-stanford-json'
'application/json'
else
blob.content_type || 'application/octet-stream'
end
declared_md5 = file[:hasMessageDigests].find { |digest| digest.fetch(:type) == 'md5' }.fetch(:digest)
calculated_md5 = base64_to_hexdigest(blob.checksum)
raise BlobError, "MD5 mismatch for #{file[:filename]}" if declared_md5 != calculated_md5
Expand Down
7 changes: 6 additions & 1 deletion openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ paths:
put:
tags:
- files
summary: Upload the binary file resource
summary: |
Upload the binary file resource
Note that the 'content_type' of the file posted will be set to 'application/octet-stream' if not supplied.
Additional values in 'content_type' after a semicolon will be removed (e.g. ';v15' is removed in 'application/text;v15')
You may post JSON files with 'content_type' of 'application/x-stanford-json' to avoid 400 errors if posted JSON file is invalid.
The system will automatically change the content type to 'application/json' after upload.
responses:
'200':
description: OK
Expand Down
55 changes: 53 additions & 2 deletions spec/requests/create_dro_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@
let(:checksum) { 'f5nXiniiM+u/gexbNkOA/A==' }

let(:blob) do
ActiveStorage::Blob.create!(key: 'tozuehlw6e8du20vn1xfzmiifyok',
ActiveStorage::Blob.create!(key: 'tozuehlw6e8du20vn1xfzmiifyok', content_type: 'application/text',
filename: 'file2.txt', byte_size: 10, checksum:)
end
let(:signed_id) do
ActiveStorage.verifier.generate(blob.id, purpose: :blob_id)
end

let(:expected_content_type) { 'application/text' }
let(:expected_model_params) do
model_params = dro.to_h
file_params = model_params.dig(:structural, :contains, 0, :structural, :contains, 0)
file_params.delete(:externalIdentifier)
file_params[:hasMimeType] = 'application/octet-stream'
file_params[:hasMimeType] = expected_content_type
file_params[:size] = 10
model_params.with_indifferent_access
end
Expand Down Expand Up @@ -218,6 +219,56 @@
end
end

context 'when the file is application/x-stanford-json' do
let(:blob) do
ActiveStorage::Blob.create!(key: 'tozuehlw6e8du20vn1xfzmiifyok',
filename: 'file2.txt', content_type: 'application/x-stanford-json',
byte_size: 10, checksum:)
end
let(:expected_content_type) { 'application/json' }

it 'switches the content type to application/json' do
post '/v1/resources',
params: request,
headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{jwt}" }
jmartin-sul marked this conversation as resolved.
Show resolved Hide resolved
expect(response).to be_created
expect(response.location).to be_present
expect(JSON.parse(response.body)['jobId']).to be_present
expect(IngestJob).to have_received(:perform_later).with(model_params: expected_model_params,
background_job_result: instance_of(BackgroundJobResult),
signed_ids: { 'file2.txt' => signed_id },
globus_ids: {},
start_workflow: false,
assign_doi: false,
priority: 'default')
end
end

context 'when the file supplies a nil content_type' do
let(:blob) do
ActiveStorage::Blob.create!(key: 'tozuehlw6e8du20vn1xfzmiifyok',
filename: 'file2.txt', content_type: nil,
byte_size: 10, checksum:)
end
let(:expected_content_type) { 'application/octet-stream' }

it 'switches the content type to application/octet-stream' do
post '/v1/resources',
params: request,
headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{jwt}" }
jmartin-sul marked this conversation as resolved.
Show resolved Hide resolved
expect(response).to be_created
expect(response.location).to be_present
expect(JSON.parse(response.body)['jobId']).to be_present
expect(IngestJob).to have_received(:perform_later).with(model_params: expected_model_params,
background_job_result: instance_of(BackgroundJobResult),
signed_ids: { 'file2.txt' => signed_id },
globus_ids: {},
start_workflow: false,
assign_doi: false,
priority: 'default')
end
end

context 'when limited user is authorized for the collection' do
let(:limited_user) { create(:user, collections: ['druid:fg123hj4567'], full_access: false) }

Expand Down
86 changes: 82 additions & 4 deletions spec/requests/direct_uploads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
require 'rails_helper'

RSpec.describe 'Direct upload' do
let(:json) do
'{"blob":{"filename":"Gemfile.lock","byte_size":1751,"checksum":"vQ0xN+GwJBg9iEAcD4v73g==",' \
'"content_type":"text/html"}}'
end
let(:filename) { 'test.txt' }
let(:data) { 'text' }
let(:checksum) { OpenSSL::Digest::MD5.base64digest(data) }
let(:byte_size) { data.length }
let(:content_type) { 'text/plain' }
let(:json) { JSON.dump({ blob: { filename:, byte_size:, checksum:, content_type: } }) }

context 'when unauthorized' do
it 'returns 401' do
Expand All @@ -25,4 +27,80 @@
expect(response).to be_successful
end
end

context 'when uploading a file' do
it 'returns 204' do
post '/v1/direct_uploads',
params: json,
headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{jwt}" }

expect(response).to be_successful
expect(response.media_type).to eq 'application/json'
direct_upload = response.parsed_body

signed_id = direct_upload['signed_id']
expect(signed_id).to be_truthy

direct_upload_uri = URI.parse(direct_upload['direct_upload']['url'])
expect(direct_upload_uri.path).to start_with('/v1/disk/')

put direct_upload_uri.path,
params: data,
headers: { 'Content-Type' => content_type, 'Authorization' => "Bearer #{jwt}" }

expect(response).to have_http_status(:no_content) # Status: 204
end
end

context 'when uploading an invalid json file with application/json' do
let(:content_type) { 'application/json' }

it 'returns 400' do
post '/v1/direct_uploads',
params: json,
headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{jwt}" }

expect(response).to be_successful
expect(response.media_type).to eq 'application/json'
direct_upload = response.parsed_body

signed_id = direct_upload['signed_id']
expect(signed_id).to be_truthy

direct_upload_uri = URI.parse(direct_upload['direct_upload']['url'])
expect(direct_upload_uri.path).to start_with('/v1/disk/')

put direct_upload_uri.path,
params: data,
headers: { 'Content-Type' => content_type, 'Authorization' => "Bearer #{jwt}" }

expect(response).to have_http_status(:bad_request) # Status: 400
end
end

context 'when uploading an invalid json file with application/x-stanford-json' do
let(:content_type) { 'application/x-stanford-json' }

it 'returns 204' do
post '/v1/direct_uploads',
params: json,
headers: { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{jwt}" }

expect(response).to be_successful
expect(response.media_type).to eq 'application/json'
direct_upload = response.parsed_body

signed_id = direct_upload['signed_id']
expect(signed_id).to be_truthy

direct_upload_uri = URI.parse(direct_upload['direct_upload']['url'])
expect(direct_upload_uri.path).to start_with('/v1/disk/')

put direct_upload_uri.path,
params: data,
headers: { 'Content-Type' => content_type, 'Authorization' => "Bearer #{jwt}" }

expect(response).to have_http_status(:no_content) # Status: 204
end
end
end