class Image attr_accessor :cgi_file_obj, :product_id, :image_type attr_reader :to_path, :msg_tail def initialize @cgi_file_obj = nil @image_type = nil @product_id = nil @to_path = nil @msg_tail = '' end def save if self.validate if (@to_path && @cgi_file_obj && @product_id) File.open(@to_path+"#{@product_id}", "wb") do |f| f.write(@cgi_file_obj.read) end end end end def delete if @to_path && @product_id && File.file?(@to_path+@product_id) File.delete(@to_path+@product_id) end end def validate self.check_file_ext && self.check_file_size end def check_file_ext @cgi_file_obj.content_type[/image\/(jpg|jpeg|pjpeg|gif|png|x-png)/] end def check_file_size if @cgi_file_obj.local_path File.size(@cgi_file_obj.local_path) < 2097152 end end def cgi_file_obj=(cgi_file_obj) @cgi_file_obj = cgi_file_obj end def product_id=(product_id) @product_id = product_id end def image_type=(image_type) @image_type = image_type @to_path = "#{RAILS_ROOT}/public/images/"+@image_type+'/' end private def msg_tail=(msg_tail) @msg_tail = msg_tail endend