@@ -1,194 +1,196 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 :container, :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 | before_save :files_to_final_location | |
|
50 | ||
|
49 | 51 | def validate_max_file_size |
|
50 | 52 | if self.filesize > Setting.attachment_max_size.to_i.kilobytes |
|
51 | 53 | errors.add(:base, :too_long, :count => Setting.attachment_max_size.to_i.kilobytes) |
|
52 | 54 | end |
|
53 | 55 | end |
|
54 | 56 | |
|
55 | 57 | def file=(incoming_file) |
|
56 | 58 | unless incoming_file.nil? |
|
57 | 59 | @temp_file = incoming_file |
|
58 | 60 | if @temp_file.size > 0 |
|
59 | 61 | self.filename = sanitize_filename(@temp_file.original_filename) |
|
60 | 62 | self.disk_filename = Attachment.disk_filename(filename) |
|
61 | 63 | self.content_type = @temp_file.content_type.to_s.chomp |
|
62 | 64 | if content_type.blank? |
|
63 | 65 | self.content_type = Redmine::MimeType.of(filename) |
|
64 | 66 | end |
|
65 | 67 | self.filesize = @temp_file.size |
|
66 | 68 | end |
|
67 | 69 | end |
|
68 | 70 | end |
|
69 | 71 | |
|
70 | 72 | def file |
|
71 | 73 | nil |
|
72 | 74 | end |
|
73 | 75 | |
|
74 | 76 | # Copies the temporary file to its final location |
|
75 | 77 | # and computes its MD5 hash |
|
76 | def before_save | |
|
78 | def files_to_final_location | |
|
77 | 79 | if @temp_file && (@temp_file.size > 0) |
|
78 | 80 | logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)") |
|
79 | 81 | md5 = Digest::MD5.new |
|
80 | 82 | File.open(diskfile, "wb") do |f| |
|
81 | 83 | buffer = "" |
|
82 | 84 | while (buffer = @temp_file.read(8192)) |
|
83 | 85 | f.write(buffer) |
|
84 | 86 | md5.update(buffer) |
|
85 | 87 | end |
|
86 | 88 | end |
|
87 | 89 | self.digest = md5.hexdigest |
|
88 | 90 | end |
|
89 | 91 | @temp_file = nil |
|
90 | 92 | # Don't save the content type if it's longer than the authorized length |
|
91 | 93 | if self.content_type && self.content_type.length > 255 |
|
92 | 94 | self.content_type = nil |
|
93 | 95 | end |
|
94 | 96 | end |
|
95 | 97 | |
|
96 | 98 | # Deletes file on the disk |
|
97 | 99 | def after_destroy |
|
98 | 100 | File.delete(diskfile) if !filename.blank? && File.exist?(diskfile) |
|
99 | 101 | end |
|
100 | 102 | |
|
101 | 103 | # Returns file's location on disk |
|
102 | 104 | def diskfile |
|
103 | 105 | "#{@@storage_path}/#{self.disk_filename}" |
|
104 | 106 | end |
|
105 | 107 | |
|
106 | 108 | def increment_download |
|
107 | 109 | increment!(:downloads) |
|
108 | 110 | end |
|
109 | 111 | |
|
110 | 112 | def project |
|
111 | 113 | container.project |
|
112 | 114 | end |
|
113 | 115 | |
|
114 | 116 | def visible?(user=User.current) |
|
115 | 117 | container.attachments_visible?(user) |
|
116 | 118 | end |
|
117 | 119 | |
|
118 | 120 | def deletable?(user=User.current) |
|
119 | 121 | container.attachments_deletable?(user) |
|
120 | 122 | end |
|
121 | 123 | |
|
122 | 124 | def image? |
|
123 | 125 | self.filename =~ /\.(jpe?g|gif|png)$/i |
|
124 | 126 | end |
|
125 | 127 | |
|
126 | 128 | def is_text? |
|
127 | 129 | Redmine::MimeType.is_type?('text', filename) |
|
128 | 130 | end |
|
129 | 131 | |
|
130 | 132 | def is_diff? |
|
131 | 133 | self.filename =~ /\.(patch|diff)$/i |
|
132 | 134 | end |
|
133 | 135 | |
|
134 | 136 | # Returns true if the file is readable |
|
135 | 137 | def readable? |
|
136 | 138 | File.readable?(diskfile) |
|
137 | 139 | end |
|
138 | 140 | |
|
139 | 141 | # Bulk attaches a set of files to an object |
|
140 | 142 | # |
|
141 | 143 | # Returns a Hash of the results: |
|
142 | 144 | # :files => array of the attached files |
|
143 | 145 | # :unsaved => array of the files that could not be attached |
|
144 | 146 | def self.attach_files(obj, attachments) |
|
145 | 147 | attached = [] |
|
146 | 148 | if attachments && attachments.is_a?(Hash) |
|
147 | 149 | attachments.each_value do |attachment| |
|
148 | 150 | file = attachment['file'] |
|
149 | 151 | next unless file && file.size > 0 |
|
150 | 152 | a = Attachment.create(:container => obj, |
|
151 | 153 | :file => file, |
|
152 | 154 | :description => attachment['description'].to_s.strip, |
|
153 | 155 | :author => User.current) |
|
154 | 156 | obj.attachments << a |
|
155 | 157 | |
|
156 | 158 | if a.new_record? |
|
157 | 159 | obj.unsaved_attachments ||= [] |
|
158 | 160 | obj.unsaved_attachments << a |
|
159 | 161 | else |
|
160 | 162 | attached << a |
|
161 | 163 | end |
|
162 | 164 | end |
|
163 | 165 | end |
|
164 | 166 | {:files => attached, :unsaved => obj.unsaved_attachments} |
|
165 | 167 | end |
|
166 | 168 | |
|
167 | 169 | private |
|
168 | 170 | def sanitize_filename(value) |
|
169 | 171 | # get only the filename, not the whole path |
|
170 | 172 | just_filename = value.gsub(/^.*(\\|\/)/, '') |
|
171 | 173 | # NOTE: File.basename doesn't work right with Windows paths on Unix |
|
172 | 174 | # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/')) |
|
173 | 175 | |
|
174 | 176 | # Finally, replace all non alphanumeric, hyphens or periods with underscore |
|
175 | 177 | @filename = just_filename.gsub(/[^\w\.\-]/,'_') |
|
176 | 178 | end |
|
177 | 179 | |
|
178 | 180 | # Returns an ASCII or hashed filename |
|
179 | 181 | def self.disk_filename(filename) |
|
180 | 182 | timestamp = DateTime.now.strftime("%y%m%d%H%M%S") |
|
181 | 183 | ascii = '' |
|
182 | 184 | if filename =~ %r{^[a-zA-Z0-9_\.\-]*$} |
|
183 | 185 | ascii = filename |
|
184 | 186 | else |
|
185 | 187 | ascii = Digest::MD5.hexdigest(filename) |
|
186 | 188 | # keep the extension if any |
|
187 | 189 | ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$} |
|
188 | 190 | end |
|
189 | 191 | while File.exist?(File.join(@@storage_path, "#{timestamp}_#{ascii}")) |
|
190 | 192 | timestamp.succ! |
|
191 | 193 | end |
|
192 | 194 | "#{timestamp}_#{ascii}" |
|
193 | 195 | end |
|
194 | 196 | end |
General Comments 0
You need to be logged in to leave comments.
Login now