@@ -1,98 +1,98 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 |
|
25 | 25 | validates_length_of :filename, :maximum => 255 |
|
26 | 26 | validates_length_of :disk_filename, :maximum => 255 |
|
27 | 27 | |
|
28 | 28 | cattr_accessor :storage_path |
|
29 | 29 | @@storage_path = "#{RAILS_ROOT}/files" |
|
30 | 30 | |
|
31 | 31 | def validate |
|
32 | 32 | errors.add_to_base :too_long if self.filesize > Setting.attachment_max_size.to_i.kilobytes |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | def file=(incomming_file) |
|
36 | 36 | unless incomming_file.nil? |
|
37 | 37 | @temp_file = incomming_file |
|
38 | 38 | if @temp_file.size > 0 |
|
39 | 39 | self.filename = sanitize_filename(@temp_file.original_filename) |
|
40 | 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 | 42 | self.filesize = @temp_file.size |
|
43 | 43 | end |
|
44 | 44 | end |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def file |
|
48 | 48 | nil |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | 51 | # Copy temp file to its final location |
|
52 | 52 | def before_save |
|
53 | 53 | if @temp_file && (@temp_file.size > 0) |
|
54 | 54 | logger.debug("saving '#{self.diskfile}'") |
|
55 | 55 | File.open(diskfile, "wb") do |f| |
|
56 | 56 | f.write(@temp_file.read) |
|
57 | 57 | end |
|
58 | 58 | self.digest = Digest::MD5.hexdigest(File.read(diskfile)) |
|
59 | 59 | end |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | # Deletes file on the disk |
|
63 | 63 | def after_destroy |
|
64 | 64 | if self.filename? |
|
65 | 65 | File.delete(diskfile) if File.exist?(diskfile) |
|
66 | 66 | end |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | # Returns file's location on disk |
|
70 | 70 | def diskfile |
|
71 | 71 | "#{@@storage_path}/#{self.disk_filename}" |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | def increment_download |
|
75 | 75 | increment!(:downloads) |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | # returns last created projects |
|
79 | 79 | def self.most_downloaded |
|
80 | 80 | find(:all, :limit => 5, :order => "downloads DESC") |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | def project |
|
84 | 84 | container.is_a?(Project) ? container : container.project |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | private |
|
88 | 88 | def sanitize_filename(value) |
|
89 | 89 | # get only the filename, not the whole path |
|
90 | 90 | just_filename = value.gsub(/^.*(\\|\/)/, '') |
|
91 | 91 | # NOTE: File.basename doesn't work right with Windows paths on Unix |
|
92 | 92 | # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/')) |
|
93 | 93 | |
|
94 | 94 | # Finally, replace all non alphanumeric, underscore or periods with underscore |
|
95 | 95 | @filename = just_filename.gsub(/[^\w\.\-]/,'_') |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | 98 | end |
General Comments 0
You need to be logged in to leave comments.
Login now