FEATURE: onebox internal audio or video files

This commit is contained in:
Arpit Jalan 2015-12-25 01:52:14 +05:30
parent 14a86dd125
commit 3a28bafc0f
4 changed files with 36 additions and 1 deletions

View file

@ -247,6 +247,9 @@ Discourse.Utilities = {
getUploadMarkdown: function(upload) {
if (Discourse.Utilities.isAnImage(upload.original_filename)) {
return '<img src="' + upload.url + '" width="' + upload.width + '" height="' + upload.height + '">';
} else if (!Discourse.SiteSettings.prevent_anons_from_downloading_files && (/\.(mov|mp4|webm|ogv|mp3|ogg|wav)$/i).test(upload.original_filename)) {
// is Audio/Video
return "http://" + Discourse.BaseUrl + upload.url;
} else {
return '<a class="attachment" href="' + upload.url + '">' + upload.original_filename + '</a> (' + I18n.toHumanSize(upload.filesize) + ')';
}

View file

@ -333,7 +333,7 @@ Discourse::Application.routes.draw do
# used to download original images
get "uploads/:site/:sha" => "uploads#show", constraints: { site: /\w+/, sha: /[a-f0-9]{40}/ }
# used to dowwload attachments
# used to download attachments
get "uploads/:site/original/:tree:sha" => "uploads#show", constraints: { site: /\w+/, tree: /(\w+\/)+/i, sha: /[a-f0-9]{40}/ }
# used to download attachments (old route)
get "uploads/:site/:id/:sha" => "uploads#show", constraints: { site: /\w+/, id: /\d+/, sha: /[a-f0-9]{16}/ }

View file

@ -16,6 +16,8 @@ module Onebox
begin
route = Rails.application.routes.recognize_path(uri.path)
case route[:controller]
when 'uploads'
super
when 'topics'
# super will use matches_regexp to match the domain name
super
@ -38,6 +40,16 @@ module Onebox
# Figure out what kind of onebox to show based on the URL
case route[:controller]
when 'uploads'
url.gsub!("http:", "https:") if SiteSetting.use_https
if File.extname(uri.path) =~ /^.(mov|mp4|webm|ogv)$/
return "<video width='100%' height='100%' controls><source src='#{url}'><a href='#{url}'>#{url}</a></video>"
elsif File.extname(uri.path) =~ /^.(mp3|ogg|wav)$/
return "<audio controls><source src='#{url}'><a href='#{url}'>#{url}</a></audio>"
else
return false
end
when 'topics'
linked = "<a href='#{url}'>#{url}</a>"

View file

@ -74,4 +74,24 @@ describe Onebox::Engine::DiscourseLocalOnebox do
expect(html).to include("topic-info")
end
end
context "for a link to an internal audio or video file" do
it "returns nil if file type is not audio or video" do
url = "#{Discourse.base_url}/uploads/default/original/3X/5/c/24asdf42.pdf"
expect(Onebox.preview(url).to_s).to eq("")
end
it "returns some onebox goodness for audio file" do
url = "#{Discourse.base_url}/uploads/default/original/3X/5/c/24asdf42.mp3"
html = Onebox.preview(url).to_s
expect(html).to eq("<audio controls><source src='#{url}'><a href='#{url}'>#{url}</a></audio>")
end
it "returns some onebox goodness for video file" do
url = "#{Discourse.base_url}/uploads/default/original/3X/5/c/24asdf42.mp4"
html = Onebox.preview(url).to_s
expect(html).to eq("<video width='100%' height='100%' controls><source src='#{url}'><a href='#{url}'>#{url}</a></video>")
end
end
end