Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Handier GQL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Dec 2, 2019
1 parent fd68bad commit e1ad7e9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
62 changes: 37 additions & 25 deletions lib/dato/local/field_type/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,42 @@ def duration
@upload.duration
end

def gif_url
"https://image.mux.com/#{@upload.mux_playback_id}/animated.gif"
end

def hls_url
def streaming_url
"https://stream.mux.com/#{@upload.mux_playback_id}.m3u8"
end

def thumbnail_url(format = :jpg)
"https://image.mux.com/#{@upload.mux_playback_id}/thumbnail.#{format}"
end

def mp4_low_res_url
if @upload.mux_mp4_highest_res
"https://stream.mux.com/#{@upload.mux_playback_id}/low.mp4"
end
end

def mp4_medium_res_url
if ["medium", "low"].include?(@upload.mux_mp4_highest_res)
"https://stream.mux.com/#{@upload.mux_playback_id}/medium.mp4"
if format == :gif
"https://image.mux.com/#{@upload.mux_playback_id}/animated.gif"
else
"https://image.mux.com/#{@upload.mux_playback_id}/thumbnail.#{format}"
end
end

def mp4_high_res_url
if @upload.mux_mp4_highest_res == "high"
"https://stream.mux.com/#{@upload.mux_playback_id}/high.mp4"
def mp4_url(options = nil)
@upload.mux_mp4_highest_res or
return nil

if options && options[:exact_res]
if options[:exact_res] == :low
raw_mp4_url("low")
elsif options[:exact_res] == :medium
if %w[medium high].include?(@upload.mux_mp4_highest_res)
raw_mp4_url("medium")
end
elsif @upload.mux_mp4_highest_res == :high
raw_mp4_url("high")
end
elsif options && options[:res] == :low
raw_mp4_url("low")
elsif options && options[:res] == :medium
if %w[low medium].include?(@upload.mux_mp4_highest_res)
raw_mp4_url(@upload.mux_mp4_highest_res)
else
raw_mp4_url("medium")
end
else
raw_mp4_url(@upload.mux_mp4_highest_res)
end
end

Expand All @@ -189,14 +198,17 @@ def to_hash
mux_playback_id: mux_playback_id,
frame_rate: frame_rate,
duration: duration,
gif_url: gif_url,
hls_url: hls_url,
streaming_url: streaming_url,
thumbnail_url: thumbnail_url,
mp4_low_res_url: mp4_low_res_url,
mp4_medium_res_url: mp4_medium_res_url,
mp4_high_res_url: mp4_high_res_url
mp4_url: mp4_url,
}
end

private

def raw_mp4_url(res)
"https://stream.mux.com/#{@upload.mux_playback_id}/#{res}.mp4"
end
end

def video
Expand Down
9 changes: 5 additions & 4 deletions spec/dato/local/field_type/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ module FieldType
expect(file.video.duration).to eq(300)
expect(file.video.frame_rate).to eq(50)
expect(file.video.mux_playback_id).to eq('444')
expect(file.video.gif_url).to eq('https://image.mux.com/444/animated.gif')
expect(file.video.hls_url).to eq('https://stream.mux.com/444.m3u8')
expect(file.video.thumbnail_url(:gif)).to eq('https://image.mux.com/444/animated.gif')
expect(file.video.streaming_url).to eq('https://stream.mux.com/444.m3u8')
expect(file.video.thumbnail_url).to eq('https://image.mux.com/444/thumbnail.jpg')
expect(file.video.mp4_low_res_url).to eq('https://stream.mux.com/444/low.mp4')
expect(file.video.mp4_medium_res_url).to eq('https://stream.mux.com/444/medium.mp4')
expect(file.video.mp4_url).to eq('https://stream.mux.com/444/medium.mp4')
expect(file.video.mp4_url(exact_res: :high)).to be_nil
expect(file.video.mp4_url(exact_res: :low)).to eq('https://stream.mux.com/444/low.mp4')
end
end

Expand Down

0 comments on commit e1ad7e9

Please sign in to comment.