Project

General

Profile

Patch #903 ยป attachment_model_cleanup.diff

Rocco Stanzione, 2008-03-20 18:32

View differences:

app/models/attachment.rb (working copy)
35 35
    errors.add_to_base :too_long if self.filesize > Setting.attachment_max_size.to_i.kilobytes
36 36
  end
37 37

  
38
	def file=(incomming_file)
39
		unless incomming_file.nil?
40
			@temp_file = incomming_file
38
	def file=(incoming_file)
39
		unless incoming_file.nil?
40
			@temp_file = incoming_file
41 41
			if @temp_file.size > 0
42 42
				self.filename = sanitize_filename(@temp_file.original_filename)
43 43
				self.disk_filename = DateTime.now.strftime("%y%m%d%H%M%S") + "_" + self.filename
......
48 48
	end
49 49
	
50 50
	def file
51
	 nil
51
	  nil
52 52
	end
53 53
	
54 54
	# Copy temp file to its final location
......
87 87
  end
88 88
  
89 89
  def image?
90
    self.filename =~ /\.(jpeg|jpg|gif|png)$/i
90
    self.filename =~ /\.(jpe?g|gif|png)$/i
91 91
  end
92 92
  
93 93
private
94 94
  def sanitize_filename(value)
95
      # get only the filename, not the whole path
96
      just_filename = value.gsub(/^.*(\\|\/)/, '')
97
      # NOTE: File.basename doesn't work right with Windows paths on Unix
98
      # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/')) 
95
    # get only the filename, not the whole path
96
    just_filename = value.gsub(/^.*(\\|\/)/, '')
97
    # NOTE: File.basename doesn't work right with Windows paths on Unix
98
    # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/')) 
99 99

  
100
      # Finally, replace all non alphanumeric, underscore or periods with underscore
101
      @filename = just_filename.gsub(/[^\w\.\-]/,'_') 
100
    # Finally, replace all non alphanumeric, hyphens or periods with underscore
101
    @filename = just_filename.gsub(/[^\w\.\-]/,'_') 
102 102
  end
103 103
    
104 104
end
    (1-1/1)