annotate .svn/pristine/d8/d84028e9377754df4951a789fc085f8e2eb7bb46.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 038ba2d95de8
children
rev   line source
Chris@1296 1 # Redmine - project management software
Chris@1296 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1296 3 #
Chris@1296 4 # This program is free software; you can redistribute it and/or
Chris@1296 5 # modify it under the terms of the GNU General Public License
Chris@1296 6 # as published by the Free Software Foundation; either version 2
Chris@1296 7 # of the License, or (at your option) any later version.
Chris@1296 8 #
Chris@1296 9 # This program is distributed in the hope that it will be useful,
Chris@1296 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1296 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1296 12 # GNU General Public License for more details.
Chris@1296 13 #
Chris@1296 14 # You should have received a copy of the GNU General Public License
Chris@1296 15 # along with this program; if not, write to the Free Software
Chris@1296 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1296 17
Chris@1296 18 require "digest/md5"
Chris@1296 19
Chris@1296 20 class Attachment < ActiveRecord::Base
Chris@1296 21 belongs_to :container, :polymorphic => true
Chris@1296 22 belongs_to :author, :class_name => "User", :foreign_key => "author_id"
Chris@1296 23
Chris@1296 24 validates_presence_of :filename, :author
Chris@1296 25 validates_length_of :filename, :maximum => 255
Chris@1296 26 validates_length_of :disk_filename, :maximum => 255
Chris@1296 27 validates_length_of :description, :maximum => 255
Chris@1296 28 validate :validate_max_file_size
Chris@1296 29
Chris@1296 30 acts_as_event :title => :filename,
Chris@1296 31 :url => Proc.new {|o| {:controller => 'attachments', :action => 'download', :id => o.id, :filename => o.filename}}
Chris@1296 32
Chris@1296 33 acts_as_activity_provider :type => 'files',
Chris@1296 34 :permission => :view_files,
Chris@1296 35 :author_key => :author_id,
Chris@1296 36 :find_options => {:select => "#{Attachment.table_name}.*",
Chris@1296 37 :joins => "LEFT JOIN #{Version.table_name} ON #{Attachment.table_name}.container_type='Version' AND #{Version.table_name}.id = #{Attachment.table_name}.container_id " +
Chris@1296 38 "LEFT JOIN #{Project.table_name} ON #{Version.table_name}.project_id = #{Project.table_name}.id OR ( #{Attachment.table_name}.container_type='Project' AND #{Attachment.table_name}.container_id = #{Project.table_name}.id )"}
Chris@1296 39
Chris@1296 40 acts_as_activity_provider :type => 'documents',
Chris@1296 41 :permission => :view_documents,
Chris@1296 42 :author_key => :author_id,
Chris@1296 43 :find_options => {:select => "#{Attachment.table_name}.*",
Chris@1296 44 :joins => "LEFT JOIN #{Document.table_name} ON #{Attachment.table_name}.container_type='Document' AND #{Document.table_name}.id = #{Attachment.table_name}.container_id " +
Chris@1296 45 "LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id"}
Chris@1296 46
Chris@1296 47 cattr_accessor :storage_path
Chris@1296 48 @@storage_path = Redmine::Configuration['attachments_storage_path'] || File.join(Rails.root, "files")
Chris@1296 49
Chris@1296 50 cattr_accessor :thumbnails_storage_path
Chris@1296 51 @@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails")
Chris@1296 52
Chris@1296 53 before_save :files_to_final_location
Chris@1296 54 after_destroy :delete_from_disk
Chris@1296 55
Chris@1296 56 # Returns an unsaved copy of the attachment
Chris@1296 57 def copy(attributes=nil)
Chris@1296 58 copy = self.class.new
Chris@1296 59 copy.attributes = self.attributes.dup.except("id", "downloads")
Chris@1296 60 copy.attributes = attributes if attributes
Chris@1296 61 copy
Chris@1296 62 end
Chris@1296 63
Chris@1296 64 def validate_max_file_size
Chris@1296 65 if @temp_file && self.filesize > Setting.attachment_max_size.to_i.kilobytes
Chris@1296 66 errors.add(:base, l(:error_attachment_too_big, :max_size => Setting.attachment_max_size.to_i.kilobytes))
Chris@1296 67 end
Chris@1296 68 end
Chris@1296 69
Chris@1296 70 def file=(incoming_file)
Chris@1296 71 unless incoming_file.nil?
Chris@1296 72 @temp_file = incoming_file
Chris@1296 73 if @temp_file.size > 0
Chris@1296 74 if @temp_file.respond_to?(:original_filename)
Chris@1296 75 self.filename = @temp_file.original_filename
Chris@1296 76 self.filename.force_encoding("UTF-8") if filename.respond_to?(:force_encoding)
Chris@1296 77 end
Chris@1296 78 if @temp_file.respond_to?(:content_type)
Chris@1296 79 self.content_type = @temp_file.content_type.to_s.chomp
Chris@1296 80 end
Chris@1296 81 if content_type.blank? && filename.present?
Chris@1296 82 self.content_type = Redmine::MimeType.of(filename)
Chris@1296 83 end
Chris@1296 84 self.filesize = @temp_file.size
Chris@1296 85 end
Chris@1296 86 end
Chris@1296 87 end
Chris@1296 88
Chris@1296 89 def file
Chris@1296 90 nil
Chris@1296 91 end
Chris@1296 92
Chris@1296 93 def filename=(arg)
Chris@1296 94 write_attribute :filename, sanitize_filename(arg.to_s)
Chris@1296 95 if new_record? && disk_filename.blank?
Chris@1296 96 self.disk_filename = Attachment.disk_filename(filename)
Chris@1296 97 end
Chris@1296 98 filename
Chris@1296 99 end
Chris@1296 100
Chris@1296 101 # Copies the temporary file to its final location
Chris@1296 102 # and computes its MD5 hash
Chris@1296 103 def files_to_final_location
Chris@1296 104 if @temp_file && (@temp_file.size > 0)
Chris@1296 105 logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)")
Chris@1296 106 md5 = Digest::MD5.new
Chris@1296 107 File.open(diskfile, "wb") do |f|
Chris@1296 108 if @temp_file.respond_to?(:read)
Chris@1296 109 buffer = ""
Chris@1296 110 while (buffer = @temp_file.read(8192))
Chris@1296 111 f.write(buffer)
Chris@1296 112 md5.update(buffer)
Chris@1296 113 end
Chris@1296 114 else
Chris@1296 115 f.write(@temp_file)
Chris@1296 116 md5.update(@temp_file)
Chris@1296 117 end
Chris@1296 118 end
Chris@1296 119 self.digest = md5.hexdigest
Chris@1296 120 end
Chris@1296 121 @temp_file = nil
Chris@1296 122 # Don't save the content type if it's longer than the authorized length
Chris@1296 123 if self.content_type && self.content_type.length > 255
Chris@1296 124 self.content_type = nil
Chris@1296 125 end
Chris@1296 126 end
Chris@1296 127
Chris@1296 128 # Deletes the file from the file system if it's not referenced by other attachments
Chris@1296 129 def delete_from_disk
Chris@1296 130 if Attachment.where("disk_filename = ? AND id <> ?", disk_filename, id).empty?
Chris@1296 131 delete_from_disk!
Chris@1296 132 end
Chris@1296 133 end
Chris@1296 134
Chris@1296 135 # Returns file's location on disk
Chris@1296 136 def diskfile
Chris@1296 137 File.join(self.class.storage_path, disk_filename.to_s)
Chris@1296 138 end
Chris@1296 139
Chris@1296 140 def title
Chris@1296 141 title = filename.to_s
Chris@1296 142 if description.present?
Chris@1296 143 title << " (#{description})"
Chris@1296 144 end
Chris@1296 145 title
Chris@1296 146 end
Chris@1296 147
Chris@1296 148 def increment_download
Chris@1296 149 increment!(:downloads)
Chris@1296 150 end
Chris@1296 151
Chris@1296 152 def project
Chris@1296 153 container.try(:project)
Chris@1296 154 end
Chris@1296 155
Chris@1296 156 def visible?(user=User.current)
Chris@1296 157 container && container.attachments_visible?(user)
Chris@1296 158 end
Chris@1296 159
Chris@1296 160 def deletable?(user=User.current)
Chris@1296 161 container && container.attachments_deletable?(user)
Chris@1296 162 end
Chris@1296 163
Chris@1296 164 def image?
Chris@1296 165 !!(self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i)
Chris@1296 166 end
Chris@1296 167
Chris@1296 168 def thumbnailable?
Chris@1296 169 image?
Chris@1296 170 end
Chris@1296 171
Chris@1296 172 # Returns the full path the attachment thumbnail, or nil
Chris@1296 173 # if the thumbnail cannot be generated.
Chris@1296 174 def thumbnail(options={})
Chris@1296 175 if thumbnailable? && readable?
Chris@1296 176 size = options[:size].to_i
Chris@1296 177 if size > 0
Chris@1296 178 # Limit the number of thumbnails per image
Chris@1296 179 size = (size / 50) * 50
Chris@1296 180 # Maximum thumbnail size
Chris@1296 181 size = 800 if size > 800
Chris@1296 182 else
Chris@1296 183 size = Setting.thumbnails_size.to_i
Chris@1296 184 end
Chris@1296 185 size = 100 unless size > 0
Chris@1296 186 target = File.join(self.class.thumbnails_storage_path, "#{id}_#{digest}_#{size}.thumb")
Chris@1296 187
Chris@1296 188 begin
Chris@1296 189 Redmine::Thumbnail.generate(self.diskfile, target, size)
Chris@1296 190 rescue => e
Chris@1296 191 logger.error "An error occured while generating thumbnail for #{disk_filename} to #{target}\nException was: #{e.message}" if logger
Chris@1296 192 return nil
Chris@1296 193 end
Chris@1296 194 end
Chris@1296 195 end
Chris@1296 196
Chris@1296 197 # Deletes all thumbnails
Chris@1296 198 def self.clear_thumbnails
Chris@1296 199 Dir.glob(File.join(thumbnails_storage_path, "*.thumb")).each do |file|
Chris@1296 200 File.delete file
Chris@1296 201 end
Chris@1296 202 end
Chris@1296 203
Chris@1296 204 def is_text?
Chris@1296 205 Redmine::MimeType.is_type?('text', filename)
Chris@1296 206 end
Chris@1296 207
Chris@1296 208 def is_diff?
Chris@1296 209 self.filename =~ /\.(patch|diff)$/i
Chris@1296 210 end
Chris@1296 211
Chris@1296 212 # Returns true if the file is readable
Chris@1296 213 def readable?
Chris@1296 214 File.readable?(diskfile)
Chris@1296 215 end
Chris@1296 216
Chris@1296 217 # Returns the attachment token
Chris@1296 218 def token
Chris@1296 219 "#{id}.#{digest}"
Chris@1296 220 end
Chris@1296 221
Chris@1296 222 # Finds an attachment that matches the given token and that has no container
Chris@1296 223 def self.find_by_token(token)
Chris@1296 224 if token.to_s =~ /^(\d+)\.([0-9a-f]+)$/
Chris@1296 225 attachment_id, attachment_digest = $1, $2
Chris@1296 226 attachment = Attachment.where(:id => attachment_id, :digest => attachment_digest).first
Chris@1296 227 if attachment && attachment.container.nil?
Chris@1296 228 attachment
Chris@1296 229 end
Chris@1296 230 end
Chris@1296 231 end
Chris@1296 232
Chris@1296 233 # Bulk attaches a set of files to an object
Chris@1296 234 #
Chris@1296 235 # Returns a Hash of the results:
Chris@1296 236 # :files => array of the attached files
Chris@1296 237 # :unsaved => array of the files that could not be attached
Chris@1296 238 def self.attach_files(obj, attachments)
Chris@1296 239 result = obj.save_attachments(attachments, User.current)
Chris@1296 240 obj.attach_saved_attachments
Chris@1296 241 result
Chris@1296 242 end
Chris@1296 243
Chris@1296 244 def self.latest_attach(attachments, filename)
Chris@1296 245 attachments.sort_by(&:created_on).reverse.detect {
Chris@1296 246 |att| att.filename.downcase == filename.downcase
Chris@1296 247 }
Chris@1296 248 end
Chris@1296 249
Chris@1296 250 def self.prune(age=1.day)
Chris@1296 251 Attachment.where("created_on < ? AND (container_type IS NULL OR container_type = '')", Time.now - age).destroy_all
Chris@1296 252 end
Chris@1296 253
Chris@1296 254 private
Chris@1296 255
Chris@1296 256 # Physically deletes the file from the file system
Chris@1296 257 def delete_from_disk!
Chris@1296 258 if disk_filename.present? && File.exist?(diskfile)
Chris@1296 259 File.delete(diskfile)
Chris@1296 260 end
Chris@1296 261 end
Chris@1296 262
Chris@1296 263 def sanitize_filename(value)
Chris@1296 264 # get only the filename, not the whole path
Chris@1296 265 just_filename = value.gsub(/^.*(\\|\/)/, '')
Chris@1296 266
Chris@1296 267 # Finally, replace invalid characters with underscore
Chris@1296 268 @filename = just_filename.gsub(/[\/\?\%\*\:\|\"\'<>]+/, '_')
Chris@1296 269 end
Chris@1296 270
Chris@1296 271 # Returns an ASCII or hashed filename
Chris@1296 272 def self.disk_filename(filename)
Chris@1296 273 timestamp = DateTime.now.strftime("%y%m%d%H%M%S")
Chris@1296 274 ascii = ''
Chris@1296 275 if filename =~ %r{^[a-zA-Z0-9_\.\-]*$}
Chris@1296 276 ascii = filename
Chris@1296 277 else
Chris@1296 278 ascii = Digest::MD5.hexdigest(filename)
Chris@1296 279 # keep the extension if any
Chris@1296 280 ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
Chris@1296 281 end
Chris@1296 282 while File.exist?(File.join(@@storage_path, "#{timestamp}_#{ascii}"))
Chris@1296 283 timestamp.succ!
Chris@1296 284 end
Chris@1296 285 "#{timestamp}_#{ascii}"
Chris@1296 286 end
Chris@1296 287 end