annotate .svn/pristine/d9/d986f7b46fa5ce6dde5f77817cfb4867911e726d.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 require "digest/md5"
Chris@909 19
Chris@909 20 class Attachment < ActiveRecord::Base
Chris@909 21 belongs_to :container, :polymorphic => true
Chris@909 22 belongs_to :author, :class_name => "User", :foreign_key => "author_id"
Chris@909 23
Chris@909 24 validates_presence_of :container, :filename, :author
Chris@909 25 validates_length_of :filename, :maximum => 255
Chris@909 26 validates_length_of :disk_filename, :maximum => 255
Chris@909 27 validate :validate_max_file_size
Chris@909 28
Chris@909 29 acts_as_event :title => :filename,
Chris@909 30 :url => Proc.new {|o| {:controller => 'attachments', :action => 'download', :id => o.id, :filename => o.filename}}
Chris@909 31
Chris@909 32 acts_as_activity_provider :type => 'files',
Chris@909 33 :permission => :view_files,
Chris@909 34 :author_key => :author_id,
Chris@909 35 :find_options => {:select => "#{Attachment.table_name}.*",
Chris@909 36 :joins => "LEFT JOIN #{Version.table_name} ON #{Attachment.table_name}.container_type='Version' AND #{Version.table_name}.id = #{Attachment.table_name}.container_id " +
Chris@909 37 "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@909 38
Chris@909 39 acts_as_activity_provider :type => 'documents',
Chris@909 40 :permission => :view_documents,
Chris@909 41 :author_key => :author_id,
Chris@909 42 :find_options => {:select => "#{Attachment.table_name}.*",
Chris@909 43 :joins => "LEFT JOIN #{Document.table_name} ON #{Attachment.table_name}.container_type='Document' AND #{Document.table_name}.id = #{Attachment.table_name}.container_id " +
Chris@909 44 "LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id"}
Chris@909 45
Chris@909 46 cattr_accessor :storage_path
Chris@909 47 @@storage_path = Redmine::Configuration['attachments_storage_path'] || "#{Rails.root}/files"
Chris@909 48
Chris@909 49 before_save :files_to_final_location
Chris@909 50 after_destroy :delete_from_disk
Chris@909 51
Chris@909 52 def validate_max_file_size
Chris@909 53 if self.filesize > Setting.attachment_max_size.to_i.kilobytes
Chris@909 54 errors.add(:base, :too_long, :count => Setting.attachment_max_size.to_i.kilobytes)
Chris@909 55 end
Chris@909 56 end
Chris@909 57
Chris@909 58 def file=(incoming_file)
Chris@909 59 unless incoming_file.nil?
Chris@909 60 @temp_file = incoming_file
Chris@909 61 if @temp_file.size > 0
Chris@909 62 self.filename = sanitize_filename(@temp_file.original_filename)
Chris@909 63 self.disk_filename = Attachment.disk_filename(filename)
Chris@909 64 self.content_type = @temp_file.content_type.to_s.chomp
Chris@909 65 if content_type.blank?
Chris@909 66 self.content_type = Redmine::MimeType.of(filename)
Chris@909 67 end
Chris@909 68 self.filesize = @temp_file.size
Chris@909 69 end
Chris@909 70 end
Chris@909 71 end
Chris@909 72
Chris@909 73 def file
Chris@909 74 nil
Chris@909 75 end
Chris@909 76
Chris@909 77 # Copies the temporary file to its final location
Chris@909 78 # and computes its MD5 hash
Chris@909 79 def files_to_final_location
Chris@909 80 if @temp_file && (@temp_file.size > 0)
Chris@909 81 logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)")
Chris@909 82 md5 = Digest::MD5.new
Chris@909 83 File.open(diskfile, "wb") do |f|
Chris@909 84 buffer = ""
Chris@909 85 while (buffer = @temp_file.read(8192))
Chris@909 86 f.write(buffer)
Chris@909 87 md5.update(buffer)
Chris@909 88 end
Chris@909 89 end
Chris@909 90 self.digest = md5.hexdigest
Chris@909 91 end
Chris@909 92 @temp_file = nil
Chris@909 93 # Don't save the content type if it's longer than the authorized length
Chris@909 94 if self.content_type && self.content_type.length > 255
Chris@909 95 self.content_type = nil
Chris@909 96 end
Chris@909 97 end
Chris@909 98
Chris@909 99 # Deletes file on the disk
Chris@909 100 def delete_from_disk
Chris@909 101 File.delete(diskfile) if !filename.blank? && File.exist?(diskfile)
Chris@909 102 end
Chris@909 103
Chris@909 104 # Returns file's location on disk
Chris@909 105 def diskfile
Chris@909 106 "#{@@storage_path}/#{self.disk_filename}"
Chris@909 107 end
Chris@909 108
Chris@909 109 def increment_download
Chris@909 110 increment!(:downloads)
Chris@909 111 end
Chris@909 112
Chris@909 113 def project
Chris@909 114 container.project
Chris@909 115 end
Chris@909 116
Chris@909 117 def visible?(user=User.current)
Chris@909 118 container.attachments_visible?(user)
Chris@909 119 end
Chris@909 120
Chris@909 121 def deletable?(user=User.current)
Chris@909 122 container.attachments_deletable?(user)
Chris@909 123 end
Chris@909 124
Chris@909 125 def image?
Chris@909 126 self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i
Chris@909 127 end
Chris@909 128
Chris@909 129 def is_text?
Chris@909 130 Redmine::MimeType.is_type?('text', filename)
Chris@909 131 end
Chris@909 132
Chris@909 133 def is_diff?
Chris@909 134 self.filename =~ /\.(patch|diff)$/i
Chris@909 135 end
Chris@909 136
Chris@909 137 # Returns true if the file is readable
Chris@909 138 def readable?
Chris@909 139 File.readable?(diskfile)
Chris@909 140 end
Chris@909 141
Chris@909 142 # Bulk attaches a set of files to an object
Chris@909 143 #
Chris@909 144 # Returns a Hash of the results:
Chris@909 145 # :files => array of the attached files
Chris@909 146 # :unsaved => array of the files that could not be attached
Chris@909 147 def self.attach_files(obj, attachments)
Chris@909 148 attached = []
Chris@909 149 if attachments && attachments.is_a?(Hash)
Chris@909 150 attachments.each_value do |attachment|
Chris@909 151 file = attachment['file']
Chris@909 152 next unless file && file.size > 0
Chris@909 153 a = Attachment.create(:container => obj,
Chris@909 154 :file => file,
Chris@909 155 :description => attachment['description'].to_s.strip,
Chris@909 156 :author => User.current)
Chris@909 157 obj.attachments << a
Chris@909 158
Chris@909 159 if a.new_record?
Chris@909 160 obj.unsaved_attachments ||= []
Chris@909 161 obj.unsaved_attachments << a
Chris@909 162 else
Chris@909 163 attached << a
Chris@909 164 end
Chris@909 165 end
Chris@909 166 end
Chris@909 167 {:files => attached, :unsaved => obj.unsaved_attachments}
Chris@909 168 end
Chris@909 169
Chris@909 170 def self.latest_attach(attachments, filename)
Chris@909 171 attachments.sort_by(&:created_on).reverse.detect {
Chris@909 172 |att| att.filename.downcase == filename.downcase
Chris@909 173 }
Chris@909 174 end
Chris@909 175
Chris@909 176 private
Chris@909 177 def sanitize_filename(value)
Chris@909 178 # get only the filename, not the whole path
Chris@909 179 just_filename = value.gsub(/^.*(\\|\/)/, '')
Chris@909 180
Chris@909 181 # Finally, replace invalid characters with underscore
Chris@909 182 @filename = just_filename.gsub(/[\/\?\%\*\:\|\"\'<>]+/, '_')
Chris@909 183 end
Chris@909 184
Chris@909 185 # Returns an ASCII or hashed filename
Chris@909 186 def self.disk_filename(filename)
Chris@909 187 timestamp = DateTime.now.strftime("%y%m%d%H%M%S")
Chris@909 188 ascii = ''
Chris@909 189 if filename =~ %r{^[a-zA-Z0-9_\.\-]*$}
Chris@909 190 ascii = filename
Chris@909 191 else
Chris@909 192 ascii = Digest::MD5.hexdigest(filename)
Chris@909 193 # keep the extension if any
Chris@909 194 ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
Chris@909 195 end
Chris@909 196 while File.exist?(File.join(@@storage_path, "#{timestamp}_#{ascii}"))
Chris@909 197 timestamp.succ!
Chris@909 198 end
Chris@909 199 "#{timestamp}_#{ascii}"
Chris@909 200 end
Chris@909 201 end