Skip to content

Commit

Permalink
Merge pull request #5718 from avalonmediasystem/caption_validation
Browse files Browse the repository at this point in the history
Set content_type explicitly based on file extension
  • Loading branch information
masaball authored Mar 11, 2024
2 parents 62d12f8 + 1359a56 commit 3e818f6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/supplemental_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def validate_file_type

def attach_file(new_file)
file.attach(new_file)
extension = File.extname(new_file.original_filename)
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
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/supplemental_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/captions.srt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1
00:00:03,498 --> 00:00:05,000
- Example Captions
8 changes: 7 additions & 1 deletion spec/models/supplemental_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/support/supplemental_files_controller_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3e818f6

Please sign in to comment.