From 7e2dd40a0ffccac6f2d8b8176307587b4c95f485 Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Tue, 31 Oct 2017 11:49:05 +0800 Subject: [PATCH] adds previews for audio / video attachments --- app/models/attachment.rb | 10 +++++++++- app/views/attachments/other.html.erb | 12 +++++++++++- app/views/common/_no_preview.html.erb | 8 ++++++++ app/views/common/_other.html.erb | 28 ++++++++++++++++++++++------ app/views/repositories/entry.html.erb | 7 +++++++ lib/redmine/mime_type.rb | 1 + public/stylesheets/scm.css | 2 +- 7 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 app/views/common/_no_preview.html.erb diff --git a/app/models/attachment.rb b/app/models/attachment.rb index b967e57..47955c0 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -250,8 +250,16 @@ class Attachment < ActiveRecord::Base Redmine::MimeType.of(filename) == "application/pdf" end + def is_video? + Redmine::MimeType.is_type?('video', filename) + end + + def is_audio? + Redmine::MimeType.is_type?('audio', filename) + end + def previewable? - is_text? || is_image? + is_text? || is_image? || is_video? || is_audio? end # Returns true if the file is readable diff --git a/app/views/attachments/other.html.erb b/app/views/attachments/other.html.erb index 608bbf2..f0f732f 100644 --- a/app/views/attachments/other.html.erb +++ b/app/views/attachments/other.html.erb @@ -1,6 +1,16 @@ <%= render :layout => 'layouts/file' do %> - <%= render :partial => 'common/other', + + <% kind = if @attachment.is_video? + 'video' + elsif @attachment.is_audio? + 'audio' + end %> + + <%= render :partial => "common/other", :locals => { + :kind => kind, + :path => download_named_attachment_url(@attachment, + @attachment.filename), :download_link => link_to_attachment( @attachment, :text => l(:label_no_preview_download), diff --git a/app/views/common/_no_preview.html.erb b/app/views/common/_no_preview.html.erb new file mode 100644 index 0000000..3eb9a95 --- /dev/null +++ b/app/views/common/_no_preview.html.erb @@ -0,0 +1,8 @@ +

+ <% if download_link %> + <%= t(:label_no_preview_alternative_html, link: download_link) %> + <% else %> + <%= l(:label_no_preview) %> + <% end %> +

+ diff --git a/app/views/common/_other.html.erb b/app/views/common/_other.html.erb index 74d87a6..bb72ec3 100644 --- a/app/views/common/_other.html.erb +++ b/app/views/common/_other.html.erb @@ -1,7 +1,23 @@ -

- <% if defined? download_link %> - <%= t(:label_no_preview_alternative_html, link: download_link) %> +<% download_link = nil unless defined? download_link %> +<% kind = nil unless defined? kind %> +<% path = nil unless defined? path %> + +<% if path.present? %> + + <% if kind == 'video' %> + <%= content_tag :video, class: 'filecontent', src: path, controls: true do %> + <%= render partial: 'common/no_preview', locals: { download_link: download_link } %> + <% end %> + <% elsif kind == 'audio' %> + <%= content_tag :audio, class: 'filecontent', src: path, controls: true do %> + <%= render partial: 'common/no_preview', locals: { download_link: download_link } %> + <% end %> <% else %> - <%= l(:label_no_preview) %> - <% end %> -

+ <%= render partial: 'common/no_preview', locals: { download_link: download_link } %> + <% end %> + +<% else %> + + <%= render partial: 'common/no_preview', locals: { download_link: download_link } %> + +<% end %> diff --git a/app/views/repositories/entry.html.erb b/app/views/repositories/entry.html.erb index 16bebc7..eb2e1d5 100644 --- a/app/views/repositories/entry.html.erb +++ b/app/views/repositories/entry.html.erb @@ -17,8 +17,15 @@ <% elsif @content %> <%= render :partial => 'common/file', :locals => {:filename => @path, :content => @content} %> <% else %> + <% kind = if Redmine::MimeType.is_type?('video', @path) + 'video' + elsif Redmine::MimeType.is_type?('audio', @path) + 'audio' + end %> <%= render :partial => 'common/other', :locals => { + :path => (url_for(params.merge(:action => 'raw')) if @allow_download), + :kind => kind, :download_link => @repository.supports_cat? ? link_to( l(:label_no_preview_download), { :action => 'raw', :id => @project, diff --git a/lib/redmine/mime_type.rb b/lib/redmine/mime_type.rb index 45e352a..38b6409 100644 --- a/lib/redmine/mime_type.rb +++ b/lib/redmine/mime_type.rb @@ -46,6 +46,7 @@ module Redmine 'image/x-ms-bmp' => 'bmp', 'application/javascript' => 'js', 'application/pdf' => 'pdf', + 'video/mp4' => 'mp4', }.freeze EXTENSIONS = MIME_TYPES.inject({}) do |map, (type, exts)| diff --git a/public/stylesheets/scm.css b/public/stylesheets/scm.css index 946b85e..d44b91d 100644 --- a/public/stylesheets/scm.css +++ b/public/stylesheets/scm.css @@ -71,7 +71,7 @@ table.filecontent td.line-code pre { table.filecontent tr:target th.line-num { background-color:#E0E0E0; color: #777; } table.filecontent tr:target td.line-code { background-color:#DDEEFF; } -img.filecontent { max-width: 100%; } +img.filecontent, video.filecontent { max-width: 100%; } /* 12 different colors for the annonate view */ table.annotate tr.bloc-0 td.author {border-right-color: #FFFFBF;} -- 2.1.4