summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2012-07-07 13:48:07 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2012-07-07 13:48:07 +0000
commita0c495b953b84d90f5c87f089eaefbf310b9dfeb (patch)
tree80206825ef5f1674b9330aa2688e3bb0d3c48f24 /app
parentb0bd50620195b19dcc1b7f2f82de73fceaa13692 (diff)
downloadredmine-a0c495b953b84d90f5c87f089eaefbf310b9dfeb.tar.gz
redmine-a0c495b953b84d90f5c87f089eaefbf310b9dfeb.zip
Displays thumbnails of attached images of the issue view (#1006).
This behaviour can be turned on/off in Settings -> Display (off by default). Thumbnail size can be configured there too. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9933 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r--app/controllers/attachments_controller.rb14
-rw-r--r--app/helpers/application_helper.rb6
-rw-r--r--app/helpers/attachments_helper.rb6
-rw-r--r--app/models/attachment.rb33
-rw-r--r--app/views/attachments/_links.html.erb10
-rw-r--r--app/views/issues/show.html.erb2
-rw-r--r--app/views/settings/_display.html.erb4
7 files changed, 70 insertions, 5 deletions
diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb
index a52024d14..4ef929896 100644
--- a/app/controllers/attachments_controller.rb
+++ b/app/controllers/attachments_controller.rb
@@ -17,7 +17,7 @@
class AttachmentsController < ApplicationController
before_filter :find_project, :except => :upload
- before_filter :file_readable, :read_authorize, :only => [:show, :download]
+ before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail]
before_filter :delete_authorize, :only => :destroy
before_filter :authorize_global, :only => :upload
@@ -59,6 +59,18 @@ class AttachmentsController < ApplicationController
end
+ def thumbnail
+ if @attachment.thumbnailable? && Setting.thumbnails_enabled? && thumbnail = @attachment.thumbnail
+ send_file thumbnail,
+ :filename => filename_for_content_disposition(@attachment.filename),
+ :type => detect_content_type(@attachment),
+ :disposition => 'inline'
+ else
+ # No thumbnail for the attachment or thumbnail could not be created
+ render :nothing => true, :status => 404
+ end
+ end
+
def upload
# Make sure that API users get used to set this content type
# as it won't trigger Rails' automatic parsing of the request body for parameters
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index eaf89da8a..9963408fb 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -153,6 +153,12 @@ module ApplicationHelper
end
end
+ def thumbnail_tag(attachment)
+ link_to image_tag(url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment)),
+ {:controller => 'attachments', :action => 'show', :id => attachment, :filename => attachment.filename},
+ :title => attachment.filename
+ end
+
def toggle_link(name, id, options={})
onclick = "Element.toggle('#{id}'); "
onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb
index 73cbaf01d..7b7bdf5da 100644
--- a/app/helpers/attachments_helper.rb
+++ b/app/helpers/attachments_helper.rb
@@ -21,12 +21,14 @@ module AttachmentsHelper
# Displays view/delete links to the attachments of the given object
# Options:
# :author -- author names are not displayed if set to false
+ # :thumbails -- display thumbnails if enabled in settings
def link_to_attachments(container, options = {})
- options.assert_valid_keys(:author)
+ options.assert_valid_keys(:author, :thumbnails)
if container.attachments.any?
options = {:deletable => container.attachments_deletable?, :author => true}.merge(options)
- render :partial => 'attachments/links', :locals => {:attachments => container.attachments, :options => options}
+ render :partial => 'attachments/links',
+ :locals => {:attachments => container.attachments, :options => options, :thumbnails => (options[:thumbnails] && Setting.thumbnails_enabled?)}
end
end
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index b61db87e5..1fd0a5b82 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -46,6 +46,9 @@ class Attachment < ActiveRecord::Base
cattr_accessor :storage_path
@@storage_path = Redmine::Configuration['attachments_storage_path'] || File.join(Rails.root, "files")
+ cattr_accessor :thumbnails_storage_path
+ @@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails")
+
before_save :files_to_final_location
after_destroy :delete_from_disk
@@ -150,7 +153,35 @@ class Attachment < ActiveRecord::Base
end
def image?
- self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i
+ !!(self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i)
+ end
+
+ def thumbnailable?
+ image?
+ end
+
+ # Returns the full path the attachment thumbnail, or nil
+ # if the thumbnail cannot be generated.
+ def thumbnail
+ if thumbnailable? && readable?
+ size = Setting.thumbnails_size.to_i
+ size = 100 unless size > 0
+ target = File.join(self.class.thumbnails_storage_path, "#{id}_#{digest}_#{size}.thumb")
+
+ begin
+ Redmine::Thumbnail.generate(self.diskfile, target, size)
+ rescue => e
+ logger.error "An error occured while generating thumbnail for #{disk_filename} to #{target}\nException was: #{e.message}" if logger
+ return nil
+ end
+ end
+ end
+
+ # Deletes all thumbnails
+ def self.clear_thumbnails
+ Dir.glob(File.join(thumbnails_storage_path, "*.thumb")).each do |file|
+ File.delete file
+ end
end
def is_text?
diff --git a/app/views/attachments/_links.html.erb b/app/views/attachments/_links.html.erb
index 779702fb7..f77cfb1eb 100644
--- a/app/views/attachments/_links.html.erb
+++ b/app/views/attachments/_links.html.erb
@@ -20,4 +20,14 @@
<% end %>
</p>
<% end %>
+<% if defined?(thumbnails) && thumbnails %>
+ <% images = attachments.select(&:thumbnailable?) %>
+ <% if images.any? %>
+ <div class="thumbnails">
+ <% images.each do |attachment| %>
+ <div><%= thumbnail_tag(attachment) %></div>
+ <% end %>
+ </div>
+ <% end %>
+<% end %>
</div>
diff --git a/app/views/issues/show.html.erb b/app/views/issues/show.html.erb
index 4f53592b0..5a60b0326 100644
--- a/app/views/issues/show.html.erb
+++ b/app/views/issues/show.html.erb
@@ -81,7 +81,7 @@ end %>
<%= textilizable @issue, :description, :attachments => @issue.attachments %>
</div>
<% end %>
-<%= link_to_attachments @issue %>
+<%= link_to_attachments @issue, :thumbnails => true %>
<% end -%>
<%= call_hook(:view_issues_show_description_bottom, :issue => @issue) %>
diff --git a/app/views/settings/_display.html.erb b/app/views/settings/_display.html.erb
index 2eb0e2560..1ae5a351a 100644
--- a/app/views/settings/_display.html.erb
+++ b/app/views/settings/_display.html.erb
@@ -16,6 +16,10 @@
<p><%= setting_check_box :gravatar_enabled %></p>
<p><%= setting_select :gravatar_default, [["Wavatars", 'wavatar'], ["Identicons", 'identicon'], ["Monster ids", 'monsterid'], ["Retro", 'retro'], ["Mystery man", 'mm']], :blank => :label_none %></p>
+
+<p><%= setting_check_box :thumbnails_enabled %></p>
+
+<p><%= setting_text_field :thumbnails_size, :size => 6 %></p>
</div>
<%= submit_tag l(:button_save) %>