@@ -0,0 +1,32 | |||||
|
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 File.dirname(__FILE__) + '/../test_helper' | |||
|
19 | ||||
|
20 | class AttachmentTest < Test::Unit::TestCase | |||
|
21 | ||||
|
22 | def setup | |||
|
23 | end | |||
|
24 | ||||
|
25 | def test_diskfilename | |||
|
26 | assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/ | |||
|
27 | assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1] | |||
|
28 | assert_equal '770c509475505f37c2b8fb6030434d6b.txt', Attachment.disk_filename("test_accentué.txt")[13..-1] | |||
|
29 | assert_equal 'f8139524ebb8f32e51976982cd20a85d', Attachment.disk_filename("test_accentué")[13..-1] | |||
|
30 | assert_equal 'cbb5b0f30978ba03731d61f9f6d10011', Attachment.disk_filename("test_accentué.ça")[13..-1] | |||
|
31 | end | |||
|
32 | end |
@@ -40,7 +40,7 class Attachment < ActiveRecord::Base | |||||
40 | @temp_file = incoming_file |
|
40 | @temp_file = incoming_file | |
41 | if @temp_file.size > 0 |
|
41 | if @temp_file.size > 0 | |
42 | self.filename = sanitize_filename(@temp_file.original_filename) |
|
42 | self.filename = sanitize_filename(@temp_file.original_filename) | |
43 | self.disk_filename = DateTime.now.strftime("%y%m%d%H%M%S") + "_" + self.filename |
|
43 | self.disk_filename = Attachment.disk_filename(filename) | |
44 | self.content_type = @temp_file.content_type.to_s.chomp |
|
44 | self.content_type = @temp_file.content_type.to_s.chomp | |
45 | self.filesize = @temp_file.size |
|
45 | self.filesize = @temp_file.size | |
46 | end |
|
46 | end | |
@@ -100,4 +100,17 private | |||||
100 | # Finally, replace all non alphanumeric, hyphens or periods with underscore |
|
100 | # Finally, replace all non alphanumeric, hyphens or periods with underscore | |
101 | @filename = just_filename.gsub(/[^\w\.\-]/,'_') |
|
101 | @filename = just_filename.gsub(/[^\w\.\-]/,'_') | |
102 | end |
|
102 | end | |
|
103 | ||||
|
104 | # Returns an ASCII or hashed filename | |||
|
105 | def self.disk_filename(filename) | |||
|
106 | df = DateTime.now.strftime("%y%m%d%H%M%S") + "_" | |||
|
107 | if filename =~ %r{^[a-zA-Z0-9_\.\-]*$} | |||
|
108 | df << filename | |||
|
109 | else | |||
|
110 | df << Digest::MD5.hexdigest(filename) | |||
|
111 | # keep the extension if any | |||
|
112 | df << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$} | |||
|
113 | end | |||
|
114 | df | |||
|
115 | end | |||
103 | end |
|
116 | end |
General Comments 0
You need to be logged in to leave comments.
Login now