To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / .svn / pristine / d9 / d986f7b46fa5ce6dde5f77817cfb4867911e726d.svn-base @ 1297:0a574315af3e
History | View | Annotate | Download (6.82 KB)
| 1 |
# Redmine - project management software |
|---|---|
| 2 |
# Copyright (C) 2006-2011 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 |
validate :validate_max_file_size |
| 28 |
|
| 29 |
acts_as_event :title => :filename, |
| 30 |
:url => Proc.new {|o| {:controller => 'attachments', :action => 'download', :id => o.id, :filename => o.filename}}
|
| 31 |
|
| 32 |
acts_as_activity_provider :type => 'files', |
| 33 |
:permission => :view_files, |
| 34 |
:author_key => :author_id, |
| 35 |
:find_options => {:select => "#{Attachment.table_name}.*",
|
| 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 |
"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 |
|
| 39 |
acts_as_activity_provider :type => 'documents', |
| 40 |
:permission => :view_documents, |
| 41 |
:author_key => :author_id, |
| 42 |
:find_options => {:select => "#{Attachment.table_name}.*",
|
| 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 |
"LEFT JOIN #{Project.table_name} ON #{Document.table_name}.project_id = #{Project.table_name}.id"}
|
| 45 |
|
| 46 |
cattr_accessor :storage_path |
| 47 |
@@storage_path = Redmine::Configuration['attachments_storage_path'] || "#{Rails.root}/files"
|
| 48 |
|
| 49 |
before_save :files_to_final_location |
| 50 |
after_destroy :delete_from_disk |
| 51 |
|
| 52 |
def validate_max_file_size |
| 53 |
if self.filesize > Setting.attachment_max_size.to_i.kilobytes |
| 54 |
errors.add(:base, :too_long, :count => Setting.attachment_max_size.to_i.kilobytes) |
| 55 |
end |
| 56 |
end |
| 57 |
|
| 58 |
def file=(incoming_file) |
| 59 |
unless incoming_file.nil? |
| 60 |
@temp_file = incoming_file |
| 61 |
if @temp_file.size > 0 |
| 62 |
self.filename = sanitize_filename(@temp_file.original_filename) |
| 63 |
self.disk_filename = Attachment.disk_filename(filename) |
| 64 |
self.content_type = @temp_file.content_type.to_s.chomp |
| 65 |
if content_type.blank? |
| 66 |
self.content_type = Redmine::MimeType.of(filename) |
| 67 |
end |
| 68 |
self.filesize = @temp_file.size |
| 69 |
end |
| 70 |
end |
| 71 |
end |
| 72 |
|
| 73 |
def file |
| 74 |
nil |
| 75 |
end |
| 76 |
|
| 77 |
# Copies the temporary file to its final location |
| 78 |
# and computes its MD5 hash |
| 79 |
def files_to_final_location |
| 80 |
if @temp_file && (@temp_file.size > 0) |
| 81 |
logger.info("Saving attachment '#{self.diskfile}' (#{@temp_file.size} bytes)")
|
| 82 |
md5 = Digest::MD5.new |
| 83 |
File.open(diskfile, "wb") do |f| |
| 84 |
buffer = "" |
| 85 |
while (buffer = @temp_file.read(8192)) |
| 86 |
f.write(buffer) |
| 87 |
md5.update(buffer) |
| 88 |
end |
| 89 |
end |
| 90 |
self.digest = md5.hexdigest |
| 91 |
end |
| 92 |
@temp_file = nil |
| 93 |
# Don't save the content type if it's longer than the authorized length |
| 94 |
if self.content_type && self.content_type.length > 255 |
| 95 |
self.content_type = nil |
| 96 |
end |
| 97 |
end |
| 98 |
|
| 99 |
# Deletes file on the disk |
| 100 |
def delete_from_disk |
| 101 |
File.delete(diskfile) if !filename.blank? && File.exist?(diskfile) |
| 102 |
end |
| 103 |
|
| 104 |
# Returns file's location on disk |
| 105 |
def diskfile |
| 106 |
"#{@@storage_path}/#{self.disk_filename}"
|
| 107 |
end |
| 108 |
|
| 109 |
def increment_download |
| 110 |
increment!(:downloads) |
| 111 |
end |
| 112 |
|
| 113 |
def project |
| 114 |
container.project |
| 115 |
end |
| 116 |
|
| 117 |
def visible?(user=User.current) |
| 118 |
container.attachments_visible?(user) |
| 119 |
end |
| 120 |
|
| 121 |
def deletable?(user=User.current) |
| 122 |
container.attachments_deletable?(user) |
| 123 |
end |
| 124 |
|
| 125 |
def image? |
| 126 |
self.filename =~ /\.(bmp|gif|jpg|jpe|jpeg|png)$/i |
| 127 |
end |
| 128 |
|
| 129 |
def is_text? |
| 130 |
Redmine::MimeType.is_type?('text', filename)
|
| 131 |
end |
| 132 |
|
| 133 |
def is_diff? |
| 134 |
self.filename =~ /\.(patch|diff)$/i |
| 135 |
end |
| 136 |
|
| 137 |
# Returns true if the file is readable |
| 138 |
def readable? |
| 139 |
File.readable?(diskfile) |
| 140 |
end |
| 141 |
|
| 142 |
# Bulk attaches a set of files to an object |
| 143 |
# |
| 144 |
# Returns a Hash of the results: |
| 145 |
# :files => array of the attached files |
| 146 |
# :unsaved => array of the files that could not be attached |
| 147 |
def self.attach_files(obj, attachments) |
| 148 |
attached = [] |
| 149 |
if attachments && attachments.is_a?(Hash) |
| 150 |
attachments.each_value do |attachment| |
| 151 |
file = attachment['file'] |
| 152 |
next unless file && file.size > 0 |
| 153 |
a = Attachment.create(:container => obj, |
| 154 |
:file => file, |
| 155 |
:description => attachment['description'].to_s.strip, |
| 156 |
:author => User.current) |
| 157 |
obj.attachments << a |
| 158 |
|
| 159 |
if a.new_record? |
| 160 |
obj.unsaved_attachments ||= [] |
| 161 |
obj.unsaved_attachments << a |
| 162 |
else |
| 163 |
attached << a |
| 164 |
end |
| 165 |
end |
| 166 |
end |
| 167 |
{:files => attached, :unsaved => obj.unsaved_attachments}
|
| 168 |
end |
| 169 |
|
| 170 |
def self.latest_attach(attachments, filename) |
| 171 |
attachments.sort_by(&:created_on).reverse.detect {
|
| 172 |
|att| att.filename.downcase == filename.downcase |
| 173 |
} |
| 174 |
end |
| 175 |
|
| 176 |
private |
| 177 |
def sanitize_filename(value) |
| 178 |
# get only the filename, not the whole path |
| 179 |
just_filename = value.gsub(/^.*(\\|\/)/, '') |
| 180 |
|
| 181 |
# Finally, replace invalid characters with underscore |
| 182 |
@filename = just_filename.gsub(/[\/\?\%\*\:\|\"\'<>]+/, '_') |
| 183 |
end |
| 184 |
|
| 185 |
# Returns an ASCII or hashed filename |
| 186 |
def self.disk_filename(filename) |
| 187 |
timestamp = DateTime.now.strftime("%y%m%d%H%M%S")
|
| 188 |
ascii = '' |
| 189 |
if filename =~ %r{^[a-zA-Z0-9_\.\-]*$}
|
| 190 |
ascii = filename |
| 191 |
else |
| 192 |
ascii = Digest::MD5.hexdigest(filename) |
| 193 |
# keep the extension if any |
| 194 |
ascii << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
|
| 195 |
end |
| 196 |
while File.exist?(File.join(@@storage_path, "#{timestamp}_#{ascii}"))
|
| 197 |
timestamp.succ! |
| 198 |
end |
| 199 |
"#{timestamp}_#{ascii}"
|
| 200 |
end |
| 201 |
end |