@@ -1,16 +1,16 | |||
|
1 |
# |
|
|
2 |
# Copyright (C) 2006-20 |
|
|
1 | # Redmine - project management software | |
|
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. |
@@ -20,7 +20,7 require "digest/md5" | |||
|
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 |
@@ -31,20 +31,20 class Attachment < ActiveRecord::Base | |||
|
31 | 31 | acts_as_activity_provider :type => 'files', |
|
32 | 32 | :permission => :view_files, |
|
33 | 33 | :author_key => :author_id, |
|
34 |
:find_options => {:select => "#{Attachment.table_name}.*", |
|
|
34 | :find_options => {:select => "#{Attachment.table_name}.*", | |
|
35 | 35 | :joins => "LEFT JOIN #{Version.table_name} ON #{Attachment.table_name}.container_type='Version' AND #{Version.table_name}.id = #{Attachment.table_name}.container_id " + |
|
36 | 36 | "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 )"} |
|
37 | ||
|
37 | ||
|
38 | 38 | acts_as_activity_provider :type => 'documents', |
|
39 | 39 | :permission => :view_documents, |
|
40 | 40 | :author_key => :author_id, |
|
41 |
:find_options => {:select => "#{Attachment.table_name}.*", |
|
|
41 | :find_options => {:select => "#{Attachment.table_name}.*", | |
|
42 | 42 | :joins => "LEFT JOIN #{Document.table_name} ON #{Attachment.table_name}.container_type='Document' AND #{Document.table_name}.id = #{Attachment.table_name}.container_id " + |
|
43 | 43 | "LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id"} |
|
44 | 44 | |
|
45 | 45 | cattr_accessor :storage_path |
|
46 | 46 | @@storage_path = Redmine::Configuration['attachments_storage_path'] || "#{RAILS_ROOT}/files" |
|
47 | ||
|
47 | ||
|
48 | 48 | def validate |
|
49 | 49 | if self.filesize > Setting.attachment_max_size.to_i.kilobytes |
|
50 | 50 | errors.add(:base, :too_long, :count => Setting.attachment_max_size.to_i.kilobytes) |
@@ -76,7 +76,7 class Attachment < ActiveRecord::Base | |||
|
76 | 76 | if @temp_file && (@temp_file.size > 0) |
|
77 | 77 | logger.debug("saving '#{self.diskfile}'") |
|
78 | 78 | md5 = Digest::MD5.new |
|
79 |
File.open(diskfile, "wb") do |f| |
|
|
79 | File.open(diskfile, "wb") do |f| | |
|
80 | 80 | buffer = "" |
|
81 | 81 | while (buffer = @temp_file.read(8192)) |
|
82 | 82 | f.write(buffer) |
@@ -100,7 +100,7 class Attachment < ActiveRecord::Base | |||
|
100 | 100 | def diskfile |
|
101 | 101 | "#{@@storage_path}/#{self.disk_filename}" |
|
102 | 102 | end |
|
103 | ||
|
103 | ||
|
104 | 104 | def increment_download |
|
105 | 105 | increment!(:downloads) |
|
106 | 106 | end |
@@ -108,27 +108,27 class Attachment < ActiveRecord::Base | |||
|
108 | 108 | def project |
|
109 | 109 | container.project |
|
110 | 110 | end |
|
111 | ||
|
111 | ||
|
112 | 112 | def visible?(user=User.current) |
|
113 | 113 | container.attachments_visible?(user) |
|
114 | 114 | end |
|
115 | ||
|
115 | ||
|
116 | 116 | def deletable?(user=User.current) |
|
117 | 117 | container.attachments_deletable?(user) |
|
118 | 118 | end |
|
119 | ||
|
119 | ||
|
120 | 120 | def image? |
|
121 | 121 | self.filename =~ /\.(jpe?g|gif|png)$/i |
|
122 | 122 | end |
|
123 | ||
|
123 | ||
|
124 | 124 | def is_text? |
|
125 | 125 | Redmine::MimeType.is_type?('text', filename) |
|
126 | 126 | end |
|
127 | ||
|
127 | ||
|
128 | 128 | def is_diff? |
|
129 | 129 | self.filename =~ /\.(patch|diff)$/i |
|
130 | 130 | end |
|
131 | ||
|
131 | ||
|
132 | 132 | # Returns true if the file is readable |
|
133 | 133 | def readable? |
|
134 | 134 | File.readable?(diskfile) |
@@ -145,7 +145,7 class Attachment < ActiveRecord::Base | |||
|
145 | 145 | attachments.each_value do |attachment| |
|
146 | 146 | file = attachment['file'] |
|
147 | 147 | next unless file && file.size > 0 |
|
148 |
a = Attachment.create(:container => obj, |
|
|
148 | a = Attachment.create(:container => obj, | |
|
149 | 149 | :file => file, |
|
150 | 150 | :description => attachment['description'].to_s.strip, |
|
151 | 151 | :author => User.current) |
@@ -160,18 +160,18 class Attachment < ActiveRecord::Base | |||
|
160 | 160 | end |
|
161 | 161 | {:files => attached, :unsaved => obj.unsaved_attachments} |
|
162 | 162 | end |
|
163 | ||
|
163 | ||
|
164 | 164 | private |
|
165 | 165 | def sanitize_filename(value) |
|
166 | 166 | # get only the filename, not the whole path |
|
167 | 167 | just_filename = value.gsub(/^.*(\\|\/)/, '') |
|
168 | 168 | # NOTE: File.basename doesn't work right with Windows paths on Unix |
|
169 |
# INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/')) |
|
|
169 | # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/')) | |
|
170 | 170 | |
|
171 | 171 | # Finally, replace all non alphanumeric, hyphens or periods with underscore |
|
172 |
@filename = just_filename.gsub(/[^\w\.\-]/,'_') |
|
|
172 | @filename = just_filename.gsub(/[^\w\.\-]/,'_') | |
|
173 | 173 | end |
|
174 | ||
|
174 | ||
|
175 | 175 | # Returns an ASCII or hashed filename |
|
176 | 176 | def self.disk_filename(filename) |
|
177 | 177 | timestamp = DateTime.now.strftime("%y%m%d%H%M%S") |
General Comments 0
You need to be logged in to leave comments.
Login now