<NME> poll_zencoder_job.rb
<BEF> module Pageflow
  class PollZencoderJob < ApplicationJob
    queue_as :resizing

    include StateMachineJob

    def perform_with_result(file, options, api = ZencoderApi.instance)
      options ||= {}

      catch(:halt) do
        poll_zencoder(file, api)
        fetch_input_details(file, api)
        file.post_process_encoded_files unless options[:skip_post_processing]

        :ok
      end
    ensure
      file.save!
    end

    private

    def poll_zencoder(file, api)
      info = api.get_info(file.job_id)

      file.encoding_progress = info[:finished] ? 100 : info[:progress];
      file.encoding_error_message = nil

      if info[:state] === 'failed'
        throw(:halt, :error)
      elsif !info[:finished]
        throw(:halt, :pending)
      end
    rescue ZencoderApi::RecoverableError => e
      file.encoding_error_message = e.message
      throw(:halt, :pending)
    rescue ZencoderApi::Error => e
      file.encoding_error_message = e.message
      raise
    end

    def fetch_thumbnail(file)
      return unless file.respond_to?(:thumbnail)
      file.thumbnail = URI.parse(file.zencoder_thumbnail.url(default_protocol: 'http'))
      file.poster = URI.parse(file.zencoder_poster.url(default_protocol: 'http'))
    rescue OpenURI::HTTPError
      throw(:halt, :pending)
    end
      raise
    end
  end
end

<MSG> Support CDN that redirects to HTTPS

Previously, when using protocol relative URLs for paperclip and
zencoder attachments, HTTP was used as default protocol in social
share meta tag image URLs and when downloading thumbnails and posters
generated by Zencoder.

Also HTTPS URLs were actively rewritten to HTTP due to some cargo
culted workaround for a problem where Facebook had problems with HTTPS
urls.

In both cases a redirect to HTTPS served by the CDN caused problems:

* Facebook does not appear to follow redirects and instead complains
  about invalid content type.

* `open-uri` which is used by Paperclip to perform the image download
  also does not follow redirects. Video Files stay in `encoding` state
  forever since `PollZencoderJob` assumes thumbnails are not there
  yet.

It should be safe to assume that the CDN supports HTTPS if protocol
relative URLs were configured in the first place. We therefore use
HTTPS as default protocol to solve the issue.

It will be a good idea for the host application to use HTTPS protocols
instead of protocol relative URLs to prevent extra redirects, but
still this change ensures Pageflow at least works in this case.

There is a third place that supplies HTTP as default protocol: URLs in
SMIL files used to have Akamai generate HLS files (see
`ZencoderVideoOutputDefinition`). Since the known Akamai setups using
this option do not redirect to HTTPS, the default protocol was left
unchanged to prevent regressions.

REDMINE-16967

<DFF> @@ -41,8 +41,8 @@ module Pageflow
 
     def fetch_thumbnail(file)
       return unless file.respond_to?(:thumbnail)
-      file.thumbnail = URI.parse(file.zencoder_thumbnail.url(default_protocol: 'http'))
-      file.poster = URI.parse(file.zencoder_poster.url(default_protocol: 'http'))
+      file.thumbnail = URI.parse(file.zencoder_thumbnail.url(default_protocol: 'https'))
+      file.poster = URI.parse(file.zencoder_poster.url(default_protocol: 'https'))
     rescue OpenURI::HTTPError
       throw(:halt, :pending)
     end

{'addition_count': 2, 'commit_subject': 'Support CDN that redirects to HTTPS', 'deletion_count': 2, 'file_extension': '.rb', 'lang': 'rb', 'license': 'mit', 'repo_name': 'codevise/pageflow'}