Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Attachment < ActiveRecord::Base
- set_table_name 'uploads'
- attr_accessor :temp_file # this is for holding the file field reference during the creation process.
- before_create {|a| a.uploaded ||= Time.now}
- before_create :backwards_compat
- #after_create :save_file
- after_destroy :delete_file
- has_and_belongs_to_many :tags, :join_table => 'upload_tags', :foreign_key => 'upload_id', :uniq => true
- belongs_to :attachable, :polymorphic => true
- belongs_to :user
- named_scope :having_any_tags, lambda {|tags|
- {:include => [:tags],:conditions=>{:tags => {:name => tags}}}
- }
- #validates_presence_of :file, :on => :create
- validates_presence_of :name, :origin, :attachable_type, :attachable_id
- validates_associated :user
- def self.update_to_new
- att = find(:all)
- att.each do |a|
- spl = a.relation.split '_'
- a.attachable_type = spl[0]
- a.attachable_id = spl[1]
- a.save
- end
- end
- # This is a virtual field that will recieve the file info from the controller.
- def file=(file_field)
- self.file_name = sanitize_filename(file_field.original_filename)
- self.mime_type = file_field.content_type.chomp
- self.file_size = file_field.size.to_i
- # store the file field in temp_file for later processing.
- self.temp_file = file_field
- end
- def file
- self.temp_file
- end
- def full_path
- config = (AppConfig.uploads || {})
- config['dir'] + self.url_path
- end
- def get_resized_image(width,height)
- require 'RMagick'
- valid_formats = ["image/bmp", "image/gif", "image/jpeg", "image/pipeg", "image/tiff", "image/png"]
- raise "Attachment is not a valid image." unless valid_formats.include? self.mime_type
- cached_file = AppConfig.image_resizing['cache_dir'] + "#{self.id}/#{width}x#{height}/#{self.file_name}"
- unless File.exists? cached_file
- # resize image
- img = Magick::Image.read(self.full_path).first
- new_width = width || height
- new_height = height || width
- if width.blank? or height.blank?
- # make image fit the height or width
- img.resize_to_fit!(new_width.to_f,new_height.to_f)
- else
- # make image fill height and width, (that means crop if necessary.)
- img.resize_to_fill!(new_width.to_f,new_height.to_f)
- end
- FileUtils.mkdir_p File.dirname(cached_file)
- img.write(cached_file)
- end
- cached_file
- end
- # clean extraction of filename
- protected
- def sanitize_filename(file_name)
- # get only the filename, not the whole path (from IE)
- just_filename = File.basename(file_name)
- # replace all non alphanumeric, underscore or perioids with underscore
- just_filename.gsub(/[^\w\.\_]/,'_')
- end
- def backwards_compat
- self.relation = self.attachable_type + '_' + self.attachable_id.to_s
- end
- def save_file
- return false unless self.temp_file
- config = (AppConfig.uploads || {}).symbolize_keys
- self.url_path = "#{self.attachable_type}/#{self.attachable_id}/#{self.id}_#{self.file_name}"
- self.file_system_path = self.url_path.gsub(/\//,'\\') # .NET likes backslashes for file paths (Backwards compat)
- FileUtils.mkdir_p config[:dir] + File.dirname(self.url_path)
- FileUtils.copy(self.temp_file.local_path, config[:dir] + '/' + self.url_path)
- self.save!
- end
- def delete_file
- FileUtils.remove(self.full_path) if File.exists? self.full_path
- end
- end
Add Comment
Please, Sign In to add comment