mirror of
https://github.com/discourse/discourse.git
synced 2025-06-05 14:07:30 +08:00
FEATURE: onebox internal audio or video files
This commit is contained in:
@ -247,6 +247,9 @@ Discourse.Utilities = {
|
|||||||
getUploadMarkdown: function(upload) {
|
getUploadMarkdown: function(upload) {
|
||||||
if (Discourse.Utilities.isAnImage(upload.original_filename)) {
|
if (Discourse.Utilities.isAnImage(upload.original_filename)) {
|
||||||
return '<img src="' + upload.url + '" width="' + upload.width + '" height="' + upload.height + '">';
|
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 {
|
} else {
|
||||||
return '<a class="attachment" href="' + upload.url + '">' + upload.original_filename + '</a> (' + I18n.toHumanSize(upload.filesize) + ')';
|
return '<a class="attachment" href="' + upload.url + '">' + upload.original_filename + '</a> (' + I18n.toHumanSize(upload.filesize) + ')';
|
||||||
}
|
}
|
||||||
|
@ -333,7 +333,7 @@ Discourse::Application.routes.draw do
|
|||||||
|
|
||||||
# used to download original images
|
# used to download original images
|
||||||
get "uploads/:site/:sha" => "uploads#show", constraints: { site: /\w+/, sha: /[a-f0-9]{40}/ }
|
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}/ }
|
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)
|
# used to download attachments (old route)
|
||||||
get "uploads/:site/:id/:sha" => "uploads#show", constraints: { site: /\w+/, id: /\d+/, sha: /[a-f0-9]{16}/ }
|
get "uploads/:site/:id/:sha" => "uploads#show", constraints: { site: /\w+/, id: /\d+/, sha: /[a-f0-9]{16}/ }
|
||||||
|
@ -16,6 +16,8 @@ module Onebox
|
|||||||
begin
|
begin
|
||||||
route = Rails.application.routes.recognize_path(uri.path)
|
route = Rails.application.routes.recognize_path(uri.path)
|
||||||
case route[:controller]
|
case route[:controller]
|
||||||
|
when 'uploads'
|
||||||
|
super
|
||||||
when 'topics'
|
when 'topics'
|
||||||
# super will use matches_regexp to match the domain name
|
# super will use matches_regexp to match the domain name
|
||||||
super
|
super
|
||||||
@ -38,6 +40,16 @@ module Onebox
|
|||||||
|
|
||||||
# Figure out what kind of onebox to show based on the URL
|
# Figure out what kind of onebox to show based on the URL
|
||||||
case route[:controller]
|
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'
|
when 'topics'
|
||||||
|
|
||||||
linked = "<a href='#{url}'>#{url}</a>"
|
linked = "<a href='#{url}'>#{url}</a>"
|
||||||
|
@ -74,4 +74,24 @@ describe Onebox::Engine::DiscourseLocalOnebox do
|
|||||||
expect(html).to include("topic-info")
|
expect(html).to include("topic-info")
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
Reference in New Issue
Block a user