Skip to content

Commit

Permalink
fixup! Add url_relative_to method to ZencoderAttachment
Browse files Browse the repository at this point in the history
  • Loading branch information
tf committed Aug 16, 2017
1 parent 11a2a0a commit 38a0b51
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/models/pageflow/zencoder_attachment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'uri'

module Pageflow
class ZencoderAttachment
cattr_accessor :default_options
Expand Down Expand Up @@ -44,11 +46,13 @@ def url(url_options = {})
end

def url_relative_to(attachment)
result = url
result.gsub!("#{File.dirname(attachment.url)}/", '') ||
raise("Could not generate relative url for #{result} based on #{attachment.url}.")
dir_path = File.dirname(URI.parse(attachment.url).path)

unless URI.parse(url).path.start_with?(dir_path)
raise("Could not generate relative url for #{url} based on #{attachment.url}.")
end

result
url.split("#{dir_path}/", 2).last
end

private
Expand Down
11 changes: 11 additions & 0 deletions spec/models/pageflow/zencoder_attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,17 @@ def file_double(id:)
expect(result).to eq('high/rendition.mpd')
end

it 'handles protocol relative urls correctly' do
Pageflow.config.zencoder_options[:s3_protocol] = ''
file = file_double(id: 5)
manifest = ZencoderAttachment.new(file, 'dash/manifest.mpd')
attachment = ZencoderAttachment.new(file, 'dash/high/rendition.mpd')

result = attachment.url_relative_to(manifest)

expect(result).to eq('high/rendition.mpd')
end

it 'fails if other attachment is in other directory' do
file = file_double(id: 5)
manifest = ZencoderAttachment.new(file, 'other/manifest.mpd')
Expand Down

0 comments on commit 38a0b51

Please sign in to comment.