Skip to content

Commit

Permalink
The IIIF probe service redirects to the streaming server
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Dec 19, 2023
1 parent 23d56e2 commit 749980d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/controllers/iiif/auth/v2/probe_service_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ def show # rubocop:disable Metrics:AbcSize
elsif !file.readable?
json[:status] = 404
elsif can? :access, file
json[:status] = 200
if file.streamable?
# See https://iiif.io/api/auth/2.0/#location
json[:status] = 302
json[:location] = {
id: "#{file.streaming_url}?stacks_token=#{current_user.token}",
type: "Video"
}
else
json[:status] = 200
end
else
json[:status] = 401
json.merge!(add_detail(file))
Expand Down
20 changes: 20 additions & 0 deletions app/models/stacks_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,24 @@ def stacks_rights
end
delegate :rights, :cocina_rights, :restricted_by_location?, :stanford_restricted?, :embargoed?,
:embargo_release_date, :location, to: :stacks_rights

def streamable?
accepted_formats = [".mov", ".mp4", ".mpeg", ".m4a", ".mp3"]
accepted_formats.include? File.extname(file_name)
end

def streaming_url
"#{Settings.stream.url}/#{File.join(druid_parts[1..4])}/#{streaming_url_file_segment}/playlist.m3u8"
end

private

def streaming_url_file_segment
case File.extname(file_name)
when '.mp3'
"mp3:#{file_name}"
else
"mp4:#{file_name}"
end
end
end
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ cdl:
stream:
# max_token_age is specified in seconds
max_token_age: 45
url: https://sul-mediaserver.stanford.edu/stacks/_definst_

# non-IIIF images
legacy:
Expand Down
43 changes: 43 additions & 0 deletions spec/requests/iiif/auth/v2/probe_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,49 @@
end
end

context 'when the user has access to the resource and it is streamable' do
let(:file_name) { 'SC0193_1982-013_b06_f01_1981-09-29.mp4' }

let(:public_json) do
{
'structural' => {
'contains' => [
{
'structural' => {
'contains' => [
{
'filename' => file_name,
'access' => {
'view' => 'world',
'download' => 'world'
}
}
]
}
}
]
}
}
end
let(:stacks_uri) { "https://stacks-uat.stanford.edu/file/#{id}/#{URI.encode_uri_component(file_name)}" }

before do
stub_rights_xml(world_readable_rights_xml)
get "/iiif/auth/v2/probe?id=#{stacks_uri_param}"
end

it 'returns a success response' do
expect(response).to have_http_status :ok

expect(response.parsed_body).to include({
"@context" => "http://iiif.io/api/auth/2/context.json",
"type" => "AuthProbeResult2",
"status" => 302
})
expect(response.parsed_body.dig('location', 'id')).to start_with 'https://sul-mediaserver.stanford.edu/stacks/_definst_/bb/461/xx/1037/mp4:SC0193_1982-013_b06_f01_1981-09-29.mp4/playlist.m3u8?stacks_token='
end
end

context 'when the requested file does not exist' do
let(:public_json) { {} }

Expand Down

0 comments on commit 749980d

Please sign in to comment.