From c29a9cc97053628a27fe8884b8289babd893eb43 Mon Sep 17 00:00:00 2001 From: Jan Schulz-Hofen Date: Wed, 17 Feb 2016 16:24:03 +0700 Subject: [PATCH 3/4] Add inline image preview/display for attachments and repository entries --- 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 6113957..cec4820 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.new(@attachment.diskfile, "rb").read 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 ded172a..734b439 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 b6216b4..7d577dc 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 a71a722..797f8bb 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 0000000..3064583 --- /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 0000000..ab73a23 --- /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 c9698f1..1f11183 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 55cb040..135f80e 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;} -- 2.8.0