From 2fbce6515d6f87cae8c4f02d082f208595821bdd Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Mon, 11 Apr 2016 19:17:48 +0000 Subject: Add inline image preview/display for attachments and repository entries (#22058). Patch by Jan Schulz-Hofen. git-svn-id: http://svn.redmine.org/redmine/trunk@15324 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/attachments_controller.rb | 2 ++ app/controllers/repositories_controller.rb | 4 +++- app/models/attachment.rb | 4 ++++ app/views/attachments/_links.html.erb | 2 +- app/views/attachments/image.html.erb | 3 +++ app/views/common/_image.html.erb | 1 + app/views/repositories/entry.html.erb | 4 ++++ public/stylesheets/scm.css | 2 ++ 8 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 app/views/attachments/image.html.erb create mode 100644 app/views/common/_image.html.erb diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index a4aea5cf7..d81b3d9b2 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -40,6 +40,8 @@ class AttachmentsController < ApplicationController elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte @content = File.read(@attachment.diskfile, :mode => "rb") render :action => 'file' + elsif @attachment.is_image? + render :action => 'image' else download end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index ded172a36..734b43983 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -170,7 +170,9 @@ class RepositoriesController < ApplicationController @content = @repository.cat(@path, @rev) (show_error_not_found; return) unless @content - if is_raw || + if !is_raw && Redmine::MimeType.is_type?('image', @path) + # simply render + elsif is_raw || (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) || ! is_entry_text_data?(@content, @path) # Force the download diff --git a/app/models/attachment.rb b/app/models/attachment.rb index b6216b428..7d577dc18 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -237,6 +237,10 @@ class Attachment < ActiveRecord::Base Redmine::MimeType.is_type?('text', filename) end + def is_image? + Redmine::MimeType.is_type?('image', filename) + end + def is_diff? self.filename =~ /\.(patch|diff)$/i end diff --git a/app/views/attachments/_links.html.erb b/app/views/attachments/_links.html.erb index a71a722ba..797f8bb43 100644 --- a/app/views/attachments/_links.html.erb +++ b/app/views/attachments/_links.html.erb @@ -8,7 +8,7 @@ <% for attachment in attachments %>

<%= link_to_attachment attachment, :class => 'icon icon-attachment', :download => true -%> - <% if attachment.is_text? %> + <% if attachment.is_text? || attachment.is_image? %> <%= link_to l(:button_view), { :controller => 'attachments', :action => 'show', :id => attachment, :filename => attachment.filename }, diff --git a/app/views/attachments/image.html.erb b/app/views/attachments/image.html.erb new file mode 100644 index 000000000..306458366 --- /dev/null +++ b/app/views/attachments/image.html.erb @@ -0,0 +1,3 @@ +<%= render :layout => 'layouts/file' do %> + <%= render :partial => 'common/image', :locals => {:path => download_named_attachment_url(@attachment, @attachment.filename), :alt => @attachment.filename} %> +<% end %> diff --git a/app/views/common/_image.html.erb b/app/views/common/_image.html.erb new file mode 100644 index 000000000..ab73a2354 --- /dev/null +++ b/app/views/common/_image.html.erb @@ -0,0 +1 @@ +<%= image_tag path, :alt => alt, :class => 'filecontent image' %> diff --git a/app/views/repositories/entry.html.erb b/app/views/repositories/entry.html.erb index c9698f1d1..1f1118356 100644 --- a/app/views/repositories/entry.html.erb +++ b/app/views/repositories/entry.html.erb @@ -8,7 +8,11 @@ <%= render :partial => 'link_to_functions' %> +<% if Redmine::MimeType.is_type?('image', @path) %> + <%= render :partial => 'common/image', :locals => {:path => url_for(params.merge(:action => 'raw')), :alt => @path} %> +<% else %> <%= render :partial => 'common/file', :locals => {:filename => @path, :content => @content} %> +<% end %> <% content_for :header_tags do %> <%= stylesheet_link_tag "scm" %> diff --git a/public/stylesheets/scm.css b/public/stylesheets/scm.css index 55cb04062..135f80e05 100644 --- a/public/stylesheets/scm.css +++ b/public/stylesheets/scm.css @@ -67,6 +67,8 @@ 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.image { max-width: 100%; } + /* 12 different colors for the annonate view */ table.annotate tr.bloc-0 {background: #FFFFBF;} table.annotate tr.bloc-1 {background: #EABFFF;} -- cgit v1.2.3