Mercurial > hg > soundsoftware-site
comparison app/models/attachment.rb @ 1298:4f746d8966dd redmine_2.3_integration
Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:28:30 +0100 |
parents | 622f24f53b42 |
children |
comparison
equal
deleted
inserted
replaced
1297:0a574315af3e | 1298:4f746d8966dd |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2012 Jean-Philippe Lang | 2 # Copyright (C) 2006-2013 Jean-Philippe Lang |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or | 4 # This program is free software; you can redistribute it and/or |
5 # modify it under the terms of the GNU General Public License | 5 # modify it under the terms of the GNU General Public License |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
14 # You should have received a copy of the GNU General Public License | 14 # You should have received a copy of the GNU General Public License |
15 # along with this program; if not, write to the Free Software | 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. | 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | 17 |
18 require "digest/md5" | 18 require "digest/md5" |
19 require "fileutils" | |
19 | 20 |
20 class Attachment < ActiveRecord::Base | 21 class Attachment < ActiveRecord::Base |
21 belongs_to :container, :polymorphic => true | 22 belongs_to :container, :polymorphic => true |
22 belongs_to :author, :class_name => "User", :foreign_key => "author_id" | 23 belongs_to :author, :class_name => "User", :foreign_key => "author_id" |
23 | 24 |
90 nil | 91 nil |
91 end | 92 end |
92 | 93 |
93 def filename=(arg) | 94 def filename=(arg) |
94 write_attribute :filename, sanitize_filename(arg.to_s) | 95 write_attribute :filename, sanitize_filename(arg.to_s) |
95 if new_record? && disk_filename.blank? | |
96 self.disk_filename = Attachment.disk_filename(filename) | |
97 end | |
98 filename | 96 filename |
99 end | 97 end |
100 | 98 |
101 # Copies the temporary file to its final location | 99 # Copies the temporary file to its final location |
102 # and computes its MD5 hash | 100 # and computes its MD5 hash |
103 def files_to_final_location | 101 def files_to_final_location |
104 if @temp_file && (@temp_file.size > 0) | 102 if @temp_file && (@temp_file.size > 0) |
103 self.disk_directory = target_directory | |
104 self.disk_filename = Attachment.disk_filename(filename, disk_directory) | |
105 logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)") | 105 logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)") |
106 path = File.dirname(diskfile) | |
107 unless File.directory?(path) | |
108 FileUtils.mkdir_p(path) | |
109 end | |
106 md5 = Digest::MD5.new | 110 md5 = Digest::MD5.new |
107 File.open(diskfile, "wb") do |f| | 111 File.open(diskfile, "wb") do |f| |
108 if @temp_file.respond_to?(:read) | 112 if @temp_file.respond_to?(:read) |
109 buffer = "" | 113 buffer = "" |
110 while (buffer = @temp_file.read(8192)) | 114 while (buffer = @temp_file.read(8192)) |
132 end | 136 end |
133 end | 137 end |
134 | 138 |
135 # Returns file's location on disk | 139 # Returns file's location on disk |
136 def diskfile | 140 def diskfile |
137 File.join(self.class.storage_path, disk_filename.to_s) | 141 File.join(self.class.storage_path, disk_directory.to_s, disk_filename.to_s) |
138 end | 142 end |
139 | 143 |
140 def title | 144 def title |
141 title = filename.to_s | 145 title = filename.to_s |
142 if description.present? | 146 if description.present? |
152 def project | 156 def project |
153 container.try(:project) | 157 container.try(:project) |
154 end | 158 end |
155 | 159 |
156 def visible?(user=User.current) | 160 def visible?(user=User.current) |
157 container && container.attachments_visible?(user) | 161 if container_id |
162 container && container.attachments_visible?(user) | |
163 else | |
164 author == user | |
165 end | |
158 end | 166 end |
159 | 167 |
160 def deletable?(user=User.current) | 168 def deletable?(user=User.current) |
161 container && container.attachments_deletable?(user) | 169 if container_id |
170 container && container.attachments_deletable?(user) | |
171 else | |
172 author == user | |
173 end | |
162 end | 174 end |
163 | 175 |
164 def image? | 176 def image? |
165 !!(self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i) | 177 !!(self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i) |
166 end | 178 end |
249 | 261 |
250 def self.prune(age=1.day) | 262 def self.prune(age=1.day) |
251 Attachment.where("created_on < ? AND (container_type IS NULL OR container_type = '')", Time.now - age).destroy_all | 263 Attachment.where("created_on < ? AND (container_type IS NULL OR container_type = '')", Time.now - age).destroy_all |
252 end | 264 end |
253 | 265 |
266 # Moves an existing attachment to its target directory | |
267 def move_to_target_directory! | |
268 if !new_record? & readable? | |
269 src = diskfile | |
270 self.disk_directory = target_directory | |
271 dest = diskfile | |
272 if src != dest && FileUtils.mkdir_p(File.dirname(dest)) && FileUtils.mv(src, dest) | |
273 update_column :disk_directory, disk_directory | |
274 end | |
275 end | |
276 end | |
277 | |
278 # Moves existing attachments that are stored at the root of the files | |
279 # directory (ie. created before Redmine 2.3) to their target subdirectories | |
280 def self.move_from_root_to_target_directory | |
281 Attachment.where("disk_directory IS NULL OR disk_directory = ''").find_each do |attachment| | |
282 attachment.move_to_target_directory! | |
283 end | |
284 end | |
285 | |
254 private | 286 private |
255 | 287 |
256 # Physically deletes the file from the file system | 288 # Physically deletes the file from the file system |
257 def delete_from_disk! | 289 def delete_from_disk! |
258 if disk_filename.present? && File.exist?(diskfile) | 290 if disk_filename.present? && File.exist?(diskfile) |
266 | 298 |
267 # Finally, replace invalid characters with underscore | 299 # Finally, replace invalid characters with underscore |
268 @filename = just_filename.gsub(/[\/\?\%\*\:\|\"\'<>]+/, '_') | 300 @filename = just_filename.gsub(/[\/\?\%\*\:\|\"\'<>]+/, '_') |
269 end | 301 end |
270 | 302 |
271 # Returns an ASCII or hashed filename | 303 # Returns the subdirectory in which the attachment will be saved |
272 def self.disk_filename(filename) | 304 def target_directory |
305 time = created_on || DateTime.now | |
306 time.strftime("%Y/%m") | |
307 end | |
308 | |
309 # Returns an ASCII or hashed filename that do not | |
310 # exists yet in the given subdirectory | |
311 def self.disk_filename(filename, directory=nil) | |
273 timestamp = DateTime.now.strftime("%y%m%d%H%M%S") | 312 timestamp = DateTime.now.strftime("%y%m%d%H%M%S") |
274 ascii = '' | 313 ascii = '' |
275 if filename =~ %r{^[a-zA-Z0-9_\.\-]*$} | 314 if filename =~ %r{^[a-zA-Z0-9_\.\-]*$} |
276 ascii = filename | 315 ascii = filename |
277 else | 316 else |
278 ascii = Digest::MD5.hexdigest(filename) | 317 ascii = Digest::MD5.hexdigest(filename) |
279 # keep the extension if any | 318 # keep the extension if any |
280 ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$} | 319 ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$} |
281 end | 320 end |
282 while File.exist?(File.join(@@storage_path, "#{timestamp}_#{ascii}")) | 321 while File.exist?(File.join(storage_path, directory.to_s, "#{timestamp}_#{ascii}")) |
283 timestamp.succ! | 322 timestamp.succ! |
284 end | 323 end |
285 "#{timestamp}_#{ascii}" | 324 "#{timestamp}_#{ascii}" |
286 end | 325 end |
287 end | 326 end |