Skip to content

Commit

Permalink
Merge pull request #373 from WGBH/player
Browse files Browse the repository at this point in the history
Player
  • Loading branch information
afred committed Apr 29, 2015
2 parents e64bef3 + 6e7903c commit dd3315b
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 15 deletions.
18 changes: 11 additions & 7 deletions app/controllers/media_controller.rb
Expand Up @@ -2,15 +2,19 @@

class MediaController < ApplicationController
include Blacklight::Catalog

CREDENTIALS_PATH = Rails.root + 'config/ci.yml'

def show
_response, document = fetch(params['id'])
xml = document.instance_variable_get('@_source')['xml']
pbcore = PBCore.new(xml)

ci = CiCore.new(credentials_path: File.dirname(File.dirname(File.dirname(__FILE__))) + '/config/ci.yml')
# OAuth credentials expire: otherwise it would make sense to cache this instance.
if AccessControl.authorized_ip?(request.remote_ip)
if AccessControl.authorized_ip?(request.remote_ip) &&
URI.parse(request.referer).host =~ /^(.+\.)?americanarchive\.org$/
ci = CiCore.new(credentials_path: CREDENTIALS_PATH)
# OAuth credentials expire: otherwise it would make sense to cache this instance.

_response, document = fetch(params['id'])
xml = document.instance_variable_get('@_source')['xml']
pbcore = PBCore.new(xml)

redirect_to ci.download(pbcore.ci_id)
else
render nothing: true, status: :unauthorized
Expand Down
3 changes: 3 additions & 0 deletions app/views/catalog/_player.html.erb
Expand Up @@ -22,4 +22,7 @@
<source src="<%= @pbcore.media_src %>" type='TODO' />
<p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 audio</a></p>
</audio>
<% else %>
<!-- It should have been either video or audio. -->
<% logger.error("#{@pbcore.id} is neither video nor audio.") %>
<% end %>
7 changes: 5 additions & 2 deletions app/views/catalog/show.erb
Expand Up @@ -35,9 +35,12 @@

<h1 class="record-title"><%= @pbcore.title %></h1>

<%#= TODO: render partial: 'catalog/player' if @pbcore.media_src %>
<div class="well well-sm media-thumb">
<img src="<%= @pbcore.img_src %>" alt="thumbnail of <%= @pbcore.title %>">
<% if @pbcore.media_src && AccessControl.authorized_ip?(request.remote_ip) %>
<%= render partial: 'catalog/player' %>
<% else %>
<img src="<%= @pbcore.img_src %>" alt="thumbnail of <%= @pbcore.title %>">
<% end %>
</div>

<dl>
Expand Down
2 changes: 1 addition & 1 deletion lib/access_control.rb
Expand Up @@ -5,7 +5,7 @@ module AccessControl
AUTHORIZED_RANGES = Set[
IPAddr.new('198.147.175.0/24'), # WGBH
IPAddr.new('140.147.0.0/16') # LoC
]
] + ( Rails.env.production? ? [] : [IPAddr.new('127.0.0.1')] )
def self.authorized_ip?(ip)
AUTHORIZED_RANGES.map { |range| range.include?(ip) }.any?
end
Expand Down
10 changes: 8 additions & 2 deletions spec/features/catalog_spec.rb
Expand Up @@ -24,6 +24,11 @@ def expect_thumbnail(id)
url = "http://mlamedia01.wgbh.org/aapb/thumbnail/#{id}.jpg"
expect(page).to have_css("img[src='#{url}']")
end

def expect_poster(id)
url = "http://mlamedia01.wgbh.org/aapb/thumbnail/#{id}.jpg"
expect(page).to have_css("video[poster='#{url}']")
end

describe '#index' do
it 'can find one item' do
Expand Down Expand Up @@ -170,10 +175,11 @@ def expect_thumbnail(id)
expect(page.status_code).to eq(200)
target = PBCore.new(File.read('spec/fixtures/pbcore/clean-MOCK.xml'))
target.send(:text).each do |field|
# #text is only used for #to_solr, so it's private...
# so we need the #send to get at it.
expect(page).to have_text(field)
end

expect_thumbnail(1234)
expect_poster(1234)
end
end

Expand Down
20 changes: 17 additions & 3 deletions spec/support/validation_helper.rb
Expand Up @@ -2,6 +2,19 @@
require_relative 'link_checker'

module ValidationHelper
# http://www.w3.org/TR/REC-xml/#NT-NameStartChar
NAME_START_CHARS = <<END
: A-Z _ a-z \u{C0}-\u{D6} \u{D8}-\u{F6} \u{F8}-\u{2FF}
\u{370}-\u{37D} \u{37F}-\u{1FFF} \u{200C}-\u{200D} \u{2070}-\u{218F}
\u{2C00}-\u{2FEF} \u{3001}-\u{D7FF} \u{F900}-\u{FDCF} \u{FDF0}-\u{FFFD}
\u{10000}-\u{EFFFF}
END
.gsub(/\s/,'')
NAME_CHARS = "#{NAME_START_CHARS} . 0-9 \u{B7} \u{0300}-\u{036F} \u{203F}-\u{2040} -".gsub(/\s/,'')
XML_NAME_RE = /[#{NAME_START_CHARS}][#{NAME_CHARS}]*/
ATTR_VAL_RE = /\s+#{XML_NAME_RE}\s*=\s*(?:"[^"]*"|'[^']*')/ # includes leading space
MISSING_VAL_RE = /(<#{XML_NAME_RE}+#{ATTR_VAL_RE}*\s+#{XML_NAME_RE}+)(#{ATTR_VAL_RE}*\/?>)/

def expect_fuzzy_xml
xhtml = page.body
# Kludge valid HTML5 to make it into valid XML.
Expand All @@ -10,10 +23,11 @@ def expect_fuzzy_xml
xhtml.gsub!(/<((meta|link|img|hr|br)([^>]+[^\/])?)>/, '<\2/>')

# give values to attributes
attribute_re = %q{(?:\\s+\\w+\\s*=\\s*(?:"[^"]*"|'[^']*'))}
xhtml.gsub!(/(<\w+#{attribute_re}*\s+\w+)(#{attribute_re}*\/?>)/, '\1=""\2')

xhtml.gsub!(MISSING_VAL_RE, '\1="FILLER"\2')

# iframe kludge: What's the problem here? TODO
xhtml.gsub!(/<iframe[^>]+><\/iframe>/, '<!-- iframe was here -->')

REXML::Document.new(xhtml)

page.all('a').map { |element| element['href'] }.each { |url| LinkChecker.instance.check(url) }
Expand Down
22 changes: 22 additions & 0 deletions spec/support/validation_helper_spec.rb
Expand Up @@ -68,5 +68,27 @@ def page
end
expect_fuzzy_xml
end

it 'handles iframe' do
def page
Fake.new(body: '<iframe src="/iframe.html" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>')
end
end

it 'handles video' do
# "-" in attribute name was tripping us up.
def page
Fake.new(body: <<END
<video class="video-js vjs-default-skin" controls preload="none" width="400" height="300"
poster="/poster.jpg"
data-setup="{}">
<source src="/media.mp4" type='video/mp4' />
<p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
</video>
END
)
end
expect_fuzzy_xml
end
end
end

0 comments on commit dd3315b

Please sign in to comment.