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