Skip to content

Commit

Permalink
convert SRT content to WBVTT
Browse files Browse the repository at this point in the history
fixes COMMS-1971

Test Plan:
** DO THESE BEFORE CHECKING OUT THE PS
- With notorious running create a bunch of videos with
  the rce or submission contents
- while watching the video click on the cc button
  on the video
- add a bunch of closed captions of SRT type (or media tracks
  as they are stored in the db)
- notice that the cc play on the video
CHECK OUT THE PS
- migrate your db
- notice the column on MediaTracks that you added called
  webvtt_content is now populated
- upload another srt file to a video
- notice it still plays

Change-Id: Iba0004ace0a248d8ed28ffe3015dbf5c5d3ddeee
Reviewed-on: https://gerrit.instructure.com/188126
Reviewed-by: Landon Gilbert-Bland <[email protected]>
QA-Review: Landon Gilbert-Bland <[email protected]>
Tested-by: Jenkins
Product-Review: Steven Burnett <[email protected]>
  • Loading branch information
sdb1228 committed Apr 11, 2019
1 parent 957a587 commit f37ef09
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 4 deletions.
23 changes: 19 additions & 4 deletions app/models/media_track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,27 @@
class MediaTrack < ActiveRecord::Base
belongs_to :user
belongs_to :media_object, :touch => true
validates_presence_of :media_object_id, :content
before_save :convert_srt_to_wvtt
validates :media_object_id, presence: true
validates :content, presence: true

RE_LOOKS_LIKE_TTMl = /<tt\s+xml/i
RE_LOOKS_LIKE_TTML = /<tt\s+xml/i
validates :content, format: {
without: RE_LOOKS_LIKE_TTMl,
message: "TTML tracks are not allowed because they are susceptible to xss attacks"
without: RE_LOOKS_LIKE_TTML,
message: 'TTML tracks are not allowed because they are susceptible to xss attacks'
}

def webvtt_content
self.read_attribute(:webvtt_content) || self.content
end

def convert_srt_to_wvtt
if self.content.exclude?('WEBVTT') && (self.content_changed? || self.read_attribute(:webvtt_content).nil?)
srt_content = self.content.dup
srt_content.gsub!(/(:|^)(\d)(,|:)/, '\10\2\3')
srt_content.gsub!(/([0-9]{2}:[0-9]{2}:[0-9]{2})([,])([0-9]{3})/, '\1.\3')
srt_content.gsub!("\r\n", "\n")
self.webvtt_content = "WEBVTT\n\n#{srt_content}".strip
end
end
end
24 changes: 24 additions & 0 deletions db/migrate/20190403131808_add_column_to_media_track.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Copyright (C) 2019 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

class AddColumnToMediaTrack < ActiveRecord::Migration[5.1]
tag :predeploy

def change
add_column :media_tracks, :webvtt_content, :text
end
end
29 changes: 29 additions & 0 deletions db/migrate/20190405134013_populate_media_tracks_web_vtt_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# Copyright (C) 2019 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

class PopulateMediaTracksWebVttContent < ActiveRecord::Migration[5.1]
tag :postdeploy

def change
DataFixup::PopulateMediaTracksWebVttContent.send_later_if_production_enqueue_args(
:run,
priority: Delayed::LOW_PRIORITY,
max_attempts: 1,
n_strand: "DataFixup::PopulateMediaTracksWebVttContent:#{Shard.current.database_server.id}"
)
end
end
25 changes: 25 additions & 0 deletions lib/data_fixup/populate_media_tracks_web_vtt_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# Copyright (C) 2019 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

module DataFixup::PopulateMediaTracksWebVttContent
def self.run
MediaTrack.find_each do |mt|
mt.convert_srt_to_wvtt
mt.save if mt.changed?
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#
# Copyright (C) 2019 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

require_relative '../../spec_helper'

RSpec.describe DataFixup::PopulateMediaTracksWebVttContent do
def webvtt_content
<<-HEREDOC
WEBVTT
00:01.000 --> 00:04.000
Never drink liquid nitrogen.
00:05.000 --> 00:09.000
- It will perforate your stomach.
- You could die.
end
HEREDOC
end

def srt_content
<<-HEREDOC
1
00:00:01,600 --> 00:00:04,200
English (US)
2
00:00:05,900 --> 00:00:07,999
This is a subtitle in American English
3
00:00:10,000 --> 00:00:14,000
Adding subtitles is very easy to do
HEREDOC
end

it 'converts content of type srt to web_vtt' do
mo = MediaObject.create!(media_id: 'm1234')
mt = MediaTrack.create!(content: srt_content, media_object: mo)
DataFixup::PopulateMediaTracksWebVttContent.run
expect(mt.read_attribute(:webvtt_content)).not_to be_nil
expect(mt.read_attribute(:webvtt_content)).to include('WEBVTT')
end

it 'will not convert content if already webvtt' do
mo = MediaObject.create!(media_id: 'm1234')
mt = MediaTrack.create!(content: webvtt_content, media_object: mo)
DataFixup::PopulateMediaTracksWebVttContent.run
expect(mt.read_attribute(:webvtt_content)).to be_nil
expect(mt.read_attribute(:content)).to include('WEBVTT')
end

it 'will attempt to convert bad formatted srt' do
mo = MediaObject.create!(media_id: 'm1234')
mt = MediaTrack.create!(content: '123$%blah#badmanbad', media_object: mo)
DataFixup::PopulateMediaTracksWebVttContent.run
expect(mt.read_attribute(:webvtt_content)).not_to be_nil
expect(mt.read_attribute(:webvtt_content)).to include('WEBVTT')
end
end

0 comments on commit f37ef09

Please sign in to comment.