From aa80c278e993b36226de59ed4252282c833dd881 Mon Sep 17 00:00:00 2001 From: Mason Ballengee Date: Thu, 7 Mar 2024 11:40:44 -0500 Subject: [PATCH 1/2] Set content_type explicitly based on file extension --- app/models/supplemental_file.rb | 3 +++ spec/factories/supplemental_file.rb | 4 ++++ spec/fixtures/captions.srt | 3 +++ spec/models/supplemental_file_spec.rb | 8 +++++++- .../supplemental_files_controller_examples.rb | 19 +++++++++++++++++++ 5 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/captions.srt diff --git a/app/models/supplemental_file.rb b/app/models/supplemental_file.rb index ec22663acf..9ef41af6f5 100644 --- a/app/models/supplemental_file.rb +++ b/app/models/supplemental_file.rb @@ -28,6 +28,9 @@ def validate_file_type def attach_file(new_file) file.attach(new_file) + extension = File.extname(new_file.original_filename) + content_type = Mime::Type.lookup_by_extension(extension.slice(1..-1)).to_s + self.file.content_type = content_type self.label = file.filename.to_s if label.blank? self.language = tags.include?('caption') ? Settings.caption_default.language : 'eng' end diff --git a/spec/factories/supplemental_file.rb b/spec/factories/supplemental_file.rb index 7e76b26213..066476f77a 100644 --- a/spec/factories/supplemental_file.rb +++ b/spec/factories/supplemental_file.rb @@ -29,6 +29,10 @@ file { fixture_file_upload(Rails.root.join('spec', 'fixtures', 'captions.vtt'), 'text/vtt') } end + trait :with_caption_srt_file do + file { fixture_file_upload(Rails.root.join('spec', 'fixtures', 'captions.srt'), 'text/srt')} + end + trait :with_transcript_tag do tags { ['transcript'] } end diff --git a/spec/fixtures/captions.srt b/spec/fixtures/captions.srt new file mode 100644 index 0000000000..71ea3d55b2 --- /dev/null +++ b/spec/fixtures/captions.srt @@ -0,0 +1,3 @@ +1 +00:00:03,498 --> 00:00:05,000 +- Example Captions diff --git a/spec/models/supplemental_file_spec.rb b/spec/models/supplemental_file_spec.rb index 21897768dd..f5a4f6a430 100644 --- a/spec/models/supplemental_file_spec.rb +++ b/spec/models/supplemental_file_spec.rb @@ -25,12 +25,18 @@ expect(subject.valid?).to be_truthy end end - context 'VTT/SRT caption file' do + context 'VTT caption file' do let(:subject) { FactoryBot.create(:supplemental_file, :with_caption_file, :with_caption_tag) } it 'should validate' do expect(subject.valid?).to be_truthy end end + context 'SRT caption file' do + let(:subject) { FactoryBot.create(:supplemental_file, :with_caption_srt_file, :with_caption_tag) } + it 'should validate' do + expect(subject.valid?).to be_truthy + end + end context 'non-VTT/non-SRT caption file' do let(:subject) { FactoryBot.build(:supplemental_file, :with_attached_file, :with_caption_tag) } it 'should not validate' do diff --git a/spec/support/supplemental_files_controller_examples.rb b/spec/support/supplemental_files_controller_examples.rb index 51b5c08a9f..c79f1a44ad 100644 --- a/spec/support/supplemental_files_controller_examples.rb +++ b/spec/support/supplemental_files_controller_examples.rb @@ -134,6 +134,25 @@ expect(object.supplemental_files.first.tags).to eq tags expect(object.supplemental_files.first.file).to be_attached end + + context 'with mime type that does not match extension' do + let(:tags) { ['caption'] } + let(:extension) { 'srt' } + let(:uploaded_file) { fixture_file_upload(Rails.root.join('spec', 'fixtures', 'captions.srt'), 'text/plain') } + it "creates a SupplementalFile with correct content_type" do + expect{ + post :create, params: { class_id => object.id, supplemental_file: valid_create_attributes_with_tags, format: :json}, session: valid_session + }.to change { object.reload.supplemental_files.size }.by(1) + expect(response).to have_http_status(:created) + expect(response.location).to eq "/#{object_class.model_name.plural}/#{object.id}/supplemental_files/#{assigns(:supplemental_file).id}" + + expect(object.supplemental_files.first.id).to eq 1 + expect(object.supplemental_files.first.label).to eq 'label' + expect(object.supplemental_files.first.tags).to eq tags + expect(object.supplemental_files.first.file).to be_attached + expect(object.supplemental_files.first.file.content_type).to eq Mime::Type.lookup_by_extension(extension) + end + end end context "with invalid params" do From 1359a560e5493d4c357d2a5060691d46d8480154 Mon Sep 17 00:00:00 2001 From: Mason Ballengee Date: Mon, 11 Mar 2024 13:03:08 -0400 Subject: [PATCH 2/2] Limit explicitly setting mime-type to just SRT files --- app/models/supplemental_file.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/supplemental_file.rb b/app/models/supplemental_file.rb index 9ef41af6f5..42b012138c 100644 --- a/app/models/supplemental_file.rb +++ b/app/models/supplemental_file.rb @@ -29,8 +29,7 @@ def validate_file_type def attach_file(new_file) file.attach(new_file) extension = File.extname(new_file.original_filename) - content_type = Mime::Type.lookup_by_extension(extension.slice(1..-1)).to_s - self.file.content_type = content_type + self.file.content_type = Mime::Type.lookup_by_extension(extension.slice(1..-1)).to_s if extension == '.srt' self.label = file.filename.to_s if label.blank? self.language = tags.include?('caption') ? Settings.caption_default.language : 'eng' end