##// END OF EJS Templates
Attachment content-type is now chomped before being saved....
Jean-Philippe Lang -
r614:5478e5cbae98
parent child
Show More
@@ -1,98 +1,98
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
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
19
20 class Attachment < ActiveRecord::Base
20 class Attachment < ActiveRecord::Base
21 belongs_to :container, :polymorphic => true
21 belongs_to :container, :polymorphic => true
22 belongs_to :author, :class_name => "User", :foreign_key => "author_id"
22 belongs_to :author, :class_name => "User", :foreign_key => "author_id"
23
23
24 validates_presence_of :container, :filename
24 validates_presence_of :container, :filename
25 validates_length_of :filename, :maximum => 255
25 validates_length_of :filename, :maximum => 255
26 validates_length_of :disk_filename, :maximum => 255
26 validates_length_of :disk_filename, :maximum => 255
27
27
28 cattr_accessor :storage_path
28 cattr_accessor :storage_path
29 @@storage_path = "#{RAILS_ROOT}/files"
29 @@storage_path = "#{RAILS_ROOT}/files"
30
30
31 def validate
31 def validate
32 errors.add_to_base :too_long if self.filesize > Setting.attachment_max_size.to_i.kilobytes
32 errors.add_to_base :too_long if self.filesize > Setting.attachment_max_size.to_i.kilobytes
33 end
33 end
34
34
35 def file=(incomming_file)
35 def file=(incomming_file)
36 unless incomming_file.nil?
36 unless incomming_file.nil?
37 @temp_file = incomming_file
37 @temp_file = incomming_file
38 if @temp_file.size > 0
38 if @temp_file.size > 0
39 self.filename = sanitize_filename(@temp_file.original_filename)
39 self.filename = sanitize_filename(@temp_file.original_filename)
40 self.disk_filename = DateTime.now.strftime("%y%m%d%H%M%S") + "_" + self.filename
40 self.disk_filename = DateTime.now.strftime("%y%m%d%H%M%S") + "_" + self.filename
41 self.content_type = @temp_file.content_type
41 self.content_type = @temp_file.content_type.chomp
42 self.filesize = @temp_file.size
42 self.filesize = @temp_file.size
43 end
43 end
44 end
44 end
45 end
45 end
46
46
47 def file
47 def file
48 nil
48 nil
49 end
49 end
50
50
51 # Copy temp file to its final location
51 # Copy temp file to its final location
52 def before_save
52 def before_save
53 if @temp_file && (@temp_file.size > 0)
53 if @temp_file && (@temp_file.size > 0)
54 logger.debug("saving '#{self.diskfile}'")
54 logger.debug("saving '#{self.diskfile}'")
55 File.open(diskfile, "wb") do |f|
55 File.open(diskfile, "wb") do |f|
56 f.write(@temp_file.read)
56 f.write(@temp_file.read)
57 end
57 end
58 self.digest = Digest::MD5.hexdigest(File.read(diskfile))
58 self.digest = Digest::MD5.hexdigest(File.read(diskfile))
59 end
59 end
60 end
60 end
61
61
62 # Deletes file on the disk
62 # Deletes file on the disk
63 def after_destroy
63 def after_destroy
64 if self.filename?
64 if self.filename?
65 File.delete(diskfile) if File.exist?(diskfile)
65 File.delete(diskfile) if File.exist?(diskfile)
66 end
66 end
67 end
67 end
68
68
69 # Returns file's location on disk
69 # Returns file's location on disk
70 def diskfile
70 def diskfile
71 "#{@@storage_path}/#{self.disk_filename}"
71 "#{@@storage_path}/#{self.disk_filename}"
72 end
72 end
73
73
74 def increment_download
74 def increment_download
75 increment!(:downloads)
75 increment!(:downloads)
76 end
76 end
77
77
78 # returns last created projects
78 # returns last created projects
79 def self.most_downloaded
79 def self.most_downloaded
80 find(:all, :limit => 5, :order => "downloads DESC")
80 find(:all, :limit => 5, :order => "downloads DESC")
81 end
81 end
82
82
83 def project
83 def project
84 container.is_a?(Project) ? container : container.project
84 container.is_a?(Project) ? container : container.project
85 end
85 end
86
86
87 private
87 private
88 def sanitize_filename(value)
88 def sanitize_filename(value)
89 # get only the filename, not the whole path
89 # get only the filename, not the whole path
90 just_filename = value.gsub(/^.*(\\|\/)/, '')
90 just_filename = value.gsub(/^.*(\\|\/)/, '')
91 # NOTE: File.basename doesn't work right with Windows paths on Unix
91 # NOTE: File.basename doesn't work right with Windows paths on Unix
92 # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/'))
92 # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/'))
93
93
94 # Finally, replace all non alphanumeric, underscore or periods with underscore
94 # Finally, replace all non alphanumeric, underscore or periods with underscore
95 @filename = just_filename.gsub(/[^\w\.\-]/,'_')
95 @filename = just_filename.gsub(/[^\w\.\-]/,'_')
96 end
96 end
97
97
98 end
98 end
General Comments 0
You need to be logged in to leave comments. Login now