From 38a0b515639d266e5d9e11dcf974e5d0a1394f53 Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Wed, 16 Aug 2017 16:36:30 +0200 Subject: [PATCH] fixup! Add url_relative_to method to ZencoderAttachment --- app/models/pageflow/zencoder_attachment.rb | 12 ++++++++---- spec/models/pageflow/zencoder_attachment_spec.rb | 11 +++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/pageflow/zencoder_attachment.rb b/app/models/pageflow/zencoder_attachment.rb index 97943845b6..6b874fe415 100644 --- a/app/models/pageflow/zencoder_attachment.rb +++ b/app/models/pageflow/zencoder_attachment.rb @@ -1,3 +1,5 @@ +require 'uri' + module Pageflow class ZencoderAttachment cattr_accessor :default_options @@ -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 diff --git a/spec/models/pageflow/zencoder_attachment_spec.rb b/spec/models/pageflow/zencoder_attachment_spec.rb index a275988542..4ff0ba623f 100644 --- a/spec/models/pageflow/zencoder_attachment_spec.rb +++ b/spec/models/pageflow/zencoder_attachment_spec.rb @@ -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')