Skip to content

Commit

Permalink
Support for S3 https urls + better error reporting using open3
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseGaud committed Mar 15, 2019
1 parent d119526 commit 92d45b4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
36 changes: 20 additions & 16 deletions lib/mediainfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ def self.xml_parser
return xml_parser
end

def self.run(input = nil)
raise ArgumentError, 'Your input cannot be blank.' if input.nil?
command = "#{location} #{input} --Output=XML 2>&1"
raw_response = `#{command}`
unless $? == 0
raise ExecutionError, "Execution of '#{command}' failed. #{raw_response.inspect}"
end
return raw_response
end

def self.from(input)
return from_uri(input) if input.is_a?(URI)
return from_string(input) if input.is_a?(String)
Expand Down Expand Up @@ -96,13 +86,27 @@ def self.from_link(input)
from_uri(URI(input))
end

def self.run(input = nil)
raise ArgumentError, 'Your input cannot be blank.' if input.nil?
command = "#{location} '#{input}' --Output=XML"
raw_response, errors, status = Open3.capture3(command)
unless errors.empty? && status.exitstatus == 0
raise ExecutionError, "Execution of '#{command}' failed: \n #{errors.red}"
end
return raw_response
end

def self.from_uri(input)
http = Net::HTTP.new(input.host, input.port) # Check if input is valid
request = Net::HTTP::Head.new(input.request_uri) # Only grab the Headers to be sure we don't try and download the whole file
http.use_ssl = true if input.is_a? URI::HTTPS # For https support
http_request = http.request(request)
raise RemoteUrlError, "HTTP call to #{input} is not working : #{http_request.value}" unless http_request.is_a?(Net::HTTPOK)
MediaInfo::Tracks.new(MediaInfo.run(URI.escape(input.to_s)))
if input.host.include?('amazonaws.com')
MediaInfo::Tracks.new(MediaInfo.run(input.to_s)) # Removed URI.escape due to Error parsing the X-Amz-Credential parameter; the Credential is mal-formed; expecting "<YOUR-AKID>/YYYYMMDD/REGION/SERVICE/aws4_request"
else
http = Net::HTTP.new(input.host, input.port) # Check if input is valid
request = Net::HTTP::Head.new(input.request_uri) # Only grab the Headers to be sure we don't try and download the whole file; Doesn't work with presigned_urls in aws/s3
http.use_ssl = true if input.is_a? URI::HTTPS # For https support
http_request = http.request(request)
raise RemoteUrlError, "HTTP call to #{input} is not working : #{http_request.value}" unless http_request.is_a?(Net::HTTPOK)
MediaInfo::Tracks.new(MediaInfo.run(URI.escape(input.to_s)))
end
end

def self.set_singleton_method(object,name,parameters)
Expand Down
20 changes: 19 additions & 1 deletion lib/mediainfo/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ class String
#
# see: http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_02_03
def shell_escape_double_quotes
'"'+gsub(/\\|"|\$|`/, '\\\\\0')+'"'
''+gsub(/\\|"|\$|`/, '\\\\\0')+''
end unless method_defined?(:shell_escape_double_quotes)

# colorization
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end

def red
colorize(31)
end

def green
colorize(32)
end

def yellow
colorize(33)
end

end
2 changes: 2 additions & 0 deletions mediainfo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rake', '~> 12.3'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'nokogiri', '>= 1.8', '< 2.0' # Ability to parse XML response # Optional and should be included by user in their own gemfile
s.add_development_dependency 'aws-sdk-s3'
s.add_development_dependency 'pry'

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
require 'spec_shared_contexts'
require 'spec_shared_examples'
require 'mediainfo/errors'
require 'pry'
require 'aws-sdk-s3'

RSpec.configure do |config|
# Aws.config.update(stub_responses: true)

# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_shared_contexts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
end

let(:http_valid_video_url) { 'http://techslides.com/demos/sample-videos/small.mp4' }
let(:https_valid_video_url) { 'http://techslides.com/demos/sample-videos/small.mp4' }
# Using the S3 test for this instead let(:https_valid_video_url) { 'https://www.sample-videos.com/video123/mp4/240/big_buck_bunny_240p_1mb.mp4' }
let(:http_invalid_url) { 'http://urlthatdoesnotexist/file.mov' }
end

Expand Down
25 changes: 12 additions & 13 deletions spec/spec_shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
end

RSpec.shared_examples 'expected from class method for a url' do
context 'when submitted a valid http valid url' do
context 'when submitted a valid http url' do
let(:input) { http_valid_video_url }

it 'does not raise an error' do
Expand All @@ -48,19 +48,18 @@
end
end

context 'when submitted a valid https valid url' do
let(:input) { https_valid_video_url }

it 'does not raise an error' do
expect{MediaInfo.from(input)}.not_to raise_error
end

context 'when a valid s3 object https url' do
it 'returns an instance of MediaInfo::Tracks' do
expect(MediaInfo.from(input)).to be_an_instance_of(MediaInfo::Tracks)
end

it 'returns an object with a valid xml output' do
expect(MediaInfo.from(input).xml.include?('?xml')).to be true
region = 'us-east-2'
bucket = 'github-mediainfo'
object_key = 'small.mp4'
s3_resource = Aws::S3::Resource.new(
region: region,
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
)
signed_url = s3_resource.bucket(bucket).object(object_key).presigned_url(:get, expires_in: 3600)
expect(MediaInfo.from(signed_url)).to be_an_instance_of(MediaInfo::Tracks)
end
end

Expand Down

0 comments on commit 92d45b4

Please sign in to comment.