1
|
# redMine - project management software
|
2
|
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
3
|
#
|
4
|
# This program is free software; you can redistribute it and/or
|
5
|
# modify it under the terms of the GNU General Public License
|
6
|
# as published by the Free Software Foundation; either version 2
|
7
|
# of the License, or (at your option) any later version.
|
8
|
#
|
9
|
# This program is distributed in the hope that it will be useful,
|
10
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
# GNU General Public License for more details.
|
13
|
#
|
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
|
16
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
17
|
|
18
|
require "digest/md5"
|
19
|
|
20
|
class Attachment < ActiveRecord::Base
|
21
|
belongs_to :container, :polymorphic => true
|
22
|
belongs_to :author, :class_name => "User", :foreign_key => "author_id"
|
23
|
|
24
|
validates_presence_of :container, :filename, :author
|
25
|
validates_length_of :filename, :maximum => 255
|
26
|
validates_length_of :disk_filename, :maximum => 255
|
27
|
|
28
|
acts_as_event :title => :filename,
|
29
|
:url => Proc.new {|o| {:controller => 'attachments', :action => 'download', :id => o.id, :filename => o.filename}}
|
30
|
|
31
|
acts_as_activity_provider :type => 'files',
|
32
|
:permission => :view_files,
|
33
|
:author_key => :author_id,
|
34
|
:find_options => {:select => "#{Attachment.table_name}.*",
|
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
|
"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
|
|
38
|
acts_as_activity_provider :type => 'documents',
|
39
|
:permission => :view_documents,
|
40
|
:author_key => :author_id,
|
41
|
:find_options => {:select => "#{Attachment.table_name}.*",
|
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
|
"LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id"}
|
44
|
|
45
|
cattr_accessor :storage_path
|
46
|
@@storage_path = "#{RAILS_ROOT}/files"
|
47
|
|
48
|
def validate
|
49
|
if self.filesize > Setting.attachment_max_size.to_i.kilobytes
|
50
|
errors.add(:base, :too_long, :count => Setting.attachment_max_size.to_i.kilobytes)
|
51
|
end
|
52
|
end
|
53
|
|
54
|
def file=(incoming_file)
|
55
|
unless incoming_file.nil?
|
56
|
@temp_file = incoming_file
|
57
|
if @temp_file.size > 0
|
58
|
self.filename = sanitize_filename(@temp_file.original_filename)
|
59
|
self.disk_filename = Attachment.disk_filename(filename)
|
60
|
self.content_type = @temp_file.content_type.to_s.chomp
|
61
|
self.filesize = @temp_file.size
|
62
|
end
|
63
|
end
|
64
|
end
|
65
|
|
66
|
def file
|
67
|
nil
|
68
|
end
|
69
|
|
70
|
# Copies the temporary file to its final location
|
71
|
# and computes its MD5 hash
|
72
|
def before_save
|
73
|
if @temp_file && (@temp_file.size > 0)
|
74
|
logger.debug("saving '#{self.diskfile}'")
|
75
|
md5 = Digest::MD5.new
|
76
|
File.open(diskfile, "wb") do |f|
|
77
|
buffer = ""
|
78
|
while (buffer = @temp_file.read(8192))
|
79
|
f.write(buffer)
|
80
|
md5.update(buffer)
|
81
|
end
|
82
|
end
|
83
|
self.digest = md5.hexdigest
|
84
|
end
|
85
|
# Don't save the content type if it's longer than the authorized length
|
86
|
if self.content_type && self.content_type.length > 255
|
87
|
self.content_type = nil
|
88
|
end
|
89
|
end
|
90
|
|
91
|
# Deletes file on the disk
|
92
|
def after_destroy
|
93
|
File.delete(diskfile) if !filename.blank? && File.exist?(diskfile)
|
94
|
end
|
95
|
|
96
|
# Returns file's location on disk
|
97
|
def diskfile
|
98
|
"#{@@storage_path}/#{self.disk_filename}"
|
99
|
end
|
100
|
|
101
|
def increment_download
|
102
|
increment!(:downloads)
|
103
|
end
|
104
|
|
105
|
def project
|
106
|
container.project
|
107
|
end
|
108
|
|
109
|
def visible?(user=User.current)
|
110
|
container.attachments_visible?(user)
|
111
|
end
|
112
|
|
113
|
def deletable?(user=User.current)
|
114
|
container.attachments_deletable?(user)
|
115
|
end
|
116
|
|
117
|
def image?
|
118
|
self.filename =~ /\.(jpe?g|gif|png)$/i
|
119
|
end
|
120
|
|
121
|
def is_text?
|
122
|
Redmine::MimeType.is_type?('text', filename)
|
123
|
end
|
124
|
|
125
|
def is_diff?
|
126
|
self.filename =~ /\.(patch|diff)$/i
|
127
|
end
|
128
|
|
129
|
# Returns true if the file is readable
|
130
|
def readable?
|
131
|
File.readable?(diskfile)
|
132
|
end
|
133
|
|
134
|
private
|
135
|
def sanitize_filename(value)
|
136
|
# get only the filename, not the whole path
|
137
|
just_filename = value.gsub(/^.*(\\|\/)/, '')
|
138
|
# NOTE: File.basename doesn't work right with Windows paths on Unix
|
139
|
# INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/'))
|
140
|
|
141
|
# Finally, replace all non alphanumeric, hyphens or periods with underscore
|
142
|
#@filename = just_filename.gsub(/[^\w\.\-]/,'_')
|
143
|
@filename = just_filename
|
144
|
end
|
145
|
|
146
|
# Returns an ASCII or hashed filename
|
147
|
def self.disk_filename(filename)
|
148
|
df = DateTime.now.strftime("%y%m%d%H%M%S") + "_"
|
149
|
if filename =~ %r{^[a-zA-Z0-9_\.\-]*$}
|
150
|
df << filename
|
151
|
else
|
152
|
df << Digest::MD5.hexdigest(filename)
|
153
|
# keep the extension if any
|
154
|
df << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
|
155
|
end
|
156
|
df
|
157
|
end
|
158
|
end
|