Files
nairobi/app/helpers/embed_videos_helper.rb
Angel Perez 2181a65bc2 Allow only YouTube/Vimeo URLs on 'video_url' attribute
Fixes #1841

On branch aperez-valid-video-url
  Changes to be committed:
    modified:   app/models/proposal.rb
    modified:   spec/factories.rb
    modified:   spec/models/proposal_spec.rb

Allow only YouTube/Vimeo URLs on 'video_url' attribute

Fixes #1841

On branch aperez-valid-video-url
  Changes to be committed:
    modified:   app/helpers/embed_videos_helper.rb
    modified:   app/models/proposal.rb
    modified:   spec/features/management/proposals_spec.rb
    modified:   spec/features/proposals_spec.rb
    modified:   spec/features/tags/proposals_spec.rb
    modified:   spec/models/proposal_spec.rb
2017-09-13 08:35:59 -04:00

42 lines
1.1 KiB
Ruby

module EmbedVideosHelper
VIMEO_REGEX = /vimeo.*(staffpicks\/|channels\/|videos\/|video\/|\/)([^#\&\?]*).*/
YOUTUBE_REGEX = /youtu.*(be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/
def embedded_video_code
link = @proposal.video_url
title = t('proposals.show.embed_video_title', proposal: @proposal.title)
if link.match(/vimeo.*/)
server = "Vimeo"
elsif link.match(/youtu*.*/)
server = "YouTube"
end
if server == "Vimeo"
reg_exp = VIMEO_REGEX
src = "https://player.vimeo.com/video/"
elsif server == "YouTube"
reg_exp = YOUTUBE_REGEX
src = "https://www.youtube.com/embed/"
end
if reg_exp
match = link.match(reg_exp)
end
if match && match[2]
'<iframe src="' + src + match[2] + '" style="border:0;" allowfullscreen title="' + title + '"></iframe>'
else
''
end
end
def valid_video_url?
return if video_url.blank?
return if video_url.match(VIMEO_REGEX)
return if video_url.match(YOUTUBE_REGEX)
errors.add(:video_url, :invalid)
end
end