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