Skip to content

Commit

Permalink
Merge pull request #5326 from avalonmediasystem/sgurnick-issue-5286
Browse files Browse the repository at this point in the history
Switch out calls to open() with URI#open()
  • Loading branch information
cjcolvar authored Aug 25, 2023
2 parents d1dc1c7 + c6d84b7 commit ff79c69
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/models/master_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ def create_frame_source_hls_temp_file(offset)
# Fixes https://github.com/avalonmediasystem/avalon/issues/3474
target_location = File.basename(details[:location]).split('?')[0]
target = File.join(Dir.tmpdir, target_location)
File.open(target,'wb') { |f| open(details[:location]) { |io| f.write(io.read) } }
File.open(target,'wb') { |f| URI.open(details[:location]) { |io| f.write(io.read) } }
return target, details[:offset]
end

Expand Down
4 changes: 2 additions & 2 deletions lib/avalon/m3u8_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def self.read(io, recursive: true)
new(io.read, recursive: recursive)
elsif io.is_a?(String)
if io =~ /^https?:/
open(io, "Referer" => Rails.application.routes.url_helpers.root_url) { |resp| new(resp, Addressable::URI.parse(io), recursive: recursive) }
URI.open(io, "Referer" => Rails.application.routes.url_helpers.root_url) { |resp| new(resp, Addressable::URI.parse(io), recursive: recursive) }
elsif io =~ /\.m3u8?$/i
new(File.read(io), io, recursive: recursive)
else
Expand All @@ -37,7 +37,7 @@ def initialize(playlist, base = '', recursive: true)
@base = base
@playlist = { files: [], playlists: [] }
tags = {}
playlist.lines.each do |l|
playlist.each_line do |l|
line = l.chomp
if line =~ /^#EXTM3U/
# ignore
Expand Down
83 changes: 73 additions & 10 deletions spec/lib/avalon/m3u8_read_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,84 @@
require 'avalon/m3u8_reader'

describe Avalon::M3U8Reader do
let(:m3u_file) { File.expand_path('../../../fixtures/The_Fox.mp4.m3u',__FILE__) }
let(:m3u) { Avalon::M3U8Reader.read(m3u_file) }
let(:m3u_filename) { File.expand_path('../../../fixtures/The_Fox.mp4.m3u',__FILE__) }
let(:m3u) { Avalon::M3U8Reader.read(m3u_filename) }
let(:framespec) { m3u.at(127000) }

it "should know how many files it has" do
expect(m3u.files.length).to eq(23)
context 'with a filepath' do
it "should know how many files it has" do
expect(m3u.files.length).to eq(23)
end

it "should know its duration" do
expect(m3u.duration.round(2)).to eq(225.14)
end

it "should be able to locate a frame" do
expect(framespec[:location]).to match(%r{/The_Fox.mp4-012.ts$})
expect(framespec[:filename]).to eq('The_Fox.mp4-012.ts')
expect(framespec[:offset].round(2)).to eq(6818.91)
end
end

context 'with a url' do
let(:m3u_url) { Rails.application.routes.url_helpers.hls_manifest_master_file_url(id: 1, quality: "auto") }
let(:m3u) { Avalon::M3U8Reader.read(m3u_url) }

before do
stub_request(:get, m3u_url).to_return(body: File.read(m3u_filename))
end

it "should know how many files it has" do
expect(m3u.files.length).to eq(23)
end

it "should know its duration" do
expect(m3u.duration.round(2)).to eq(225.14)
end

it "should be able to locate a frame" do
expect(framespec[:location]).to match(%r{/The_Fox.mp4-012.ts$})
expect(framespec[:filename]).to eq('The_Fox.mp4-012.ts')
expect(framespec[:offset].round(2)).to eq(6818.91)
end
end

it "should know its duration" do
expect(m3u.duration.round(2)).to eq(225.14)
context 'with a file' do
let(:m3u_file) { File.open(m3u_filename) }
let(:m3u) { Avalon::M3U8Reader.read(m3u_file) }

it "should know how many files it has" do
expect(m3u.files.length).to eq(23)
end

it "should know its duration" do
expect(m3u.duration.round(2)).to eq(225.14)
end

it "should be able to locate a frame" do
expect(framespec[:location]).to match(%r{/The_Fox.mp4-012.ts$})
expect(framespec[:filename]).to eq('The_Fox.mp4-012.ts')
expect(framespec[:offset].round(2)).to eq(6818.91)
end
end

it "should be able to locate a frame" do
expect(framespec[:location]).to match(%r{/The_Fox.mp4-012.ts$})
expect(framespec[:filename]).to eq('The_Fox.mp4-012.ts')
expect(framespec[:offset].round(2)).to eq(6818.91)
context 'with a string' do
let(:m3u_file_contents) { File.read(m3u_filename) }
let(:m3u) { Avalon::M3U8Reader.read(m3u_file_contents) }

it "should know how many files it has" do
expect(m3u.files.length).to eq(23)
end

it "should know its duration" do
expect(m3u.duration.round(2)).to eq(225.14)
end

it "should be able to locate a frame" do
expect(framespec[:location]).to match(%r{/The_Fox.mp4-012.ts$})
expect(framespec[:filename]).to eq('The_Fox.mp4-012.ts')
expect(framespec[:offset].round(2)).to eq(6818.91)
end
end
end
17 changes: 14 additions & 3 deletions spec/models/master_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -671,13 +671,24 @@

context 'when derivatives are not accessible' do
let(:high_derivative_locator) { FileLocator.new(video_master_file.derivatives.where(quality_ssi: 'high').first.absolute_location) }
let(:hls_temp_file) { "/tmp/temp_segment.ts" }
let(:hls_file) { "temp_segment.ts" }
let(:hls_url) { "http://streaming.server.com/path/#{hls_file}" }
let(:reader) { double(Avalon::M3U8Reader) }
let(:m3u8) { "HLSDATA" }

before do
stub_request(:get, hls_url).to_return(body: m3u8)
allow(Avalon::M3U8Reader).to receive(:read).and_return(reader)
allow(reader).to receive(:at).and_return({ location: hls_url, filename: hls_file, offset: 0 })
end

it 'falls back to HLS' do
expect(video_master_file).to receive(:create_frame_source_hls_temp_file).and_return(hls_temp_file)
expect(File).to receive(:exist?).with(high_derivative_locator.location).and_return(false)
#expect(video_master_file).to receive(:create_frame_source_hls_temp_file).and_return(hls_temp_file)
allow(File).to receive(:exist?).and_call_original
allow(File).to receive(:exist?).with(high_derivative_locator.location).and_return(false)
expect(source[:source]).to eq '/tmp/temp_segment.ts'
expect(source[:non_temp_file]).to eq false
expect(File.read(source[:source])).to eq m3u8
end
end
end
Expand Down

0 comments on commit ff79c69

Please sign in to comment.