Guest User

Untitled

a guest
May 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. class Attachment < ActiveRecord::Base
  2. set_table_name 'uploads'
  3. attr_accessor :temp_file # this is for holding the file field reference during the creation process.
  4. before_create {|a| a.uploaded ||= Time.now}
  5. before_create :backwards_compat
  6. #after_create :save_file
  7. after_destroy :delete_file
  8.  
  9. has_and_belongs_to_many :tags, :join_table => 'upload_tags', :foreign_key => 'upload_id', :uniq => true
  10. belongs_to :attachable, :polymorphic => true
  11. belongs_to :user
  12.  
  13. named_scope :having_any_tags, lambda {|tags|
  14. {:include => [:tags],:conditions=>{:tags => {:name => tags}}}
  15. }
  16.  
  17. #validates_presence_of :file, :on => :create
  18. validates_presence_of :name, :origin, :attachable_type, :attachable_id
  19. validates_associated :user
  20.  
  21. def self.update_to_new
  22. att = find(:all)
  23. att.each do |a|
  24. spl = a.relation.split '_'
  25. a.attachable_type = spl[0]
  26. a.attachable_id = spl[1]
  27. a.save
  28. end
  29. end
  30.  
  31. # This is a virtual field that will recieve the file info from the controller.
  32. def file=(file_field)
  33. self.file_name = sanitize_filename(file_field.original_filename)
  34. self.mime_type = file_field.content_type.chomp
  35. self.file_size = file_field.size.to_i
  36. # store the file field in temp_file for later processing.
  37. self.temp_file = file_field
  38. end
  39. def file
  40. self.temp_file
  41. end
  42.  
  43. def full_path
  44. config = (AppConfig.uploads || {})
  45. config['dir'] + self.url_path
  46. end
  47.  
  48. def get_resized_image(width,height)
  49. require 'RMagick'
  50. valid_formats = ["image/bmp", "image/gif", "image/jpeg", "image/pipeg", "image/tiff", "image/png"]
  51. raise "Attachment is not a valid image." unless valid_formats.include? self.mime_type
  52. cached_file = AppConfig.image_resizing['cache_dir'] + "#{self.id}/#{width}x#{height}/#{self.file_name}"
  53. unless File.exists? cached_file
  54. # resize image
  55. img = Magick::Image.read(self.full_path).first
  56. new_width = width || height
  57. new_height = height || width
  58.  
  59. if width.blank? or height.blank?
  60. # make image fit the height or width
  61. img.resize_to_fit!(new_width.to_f,new_height.to_f)
  62. else
  63. # make image fill height and width, (that means crop if necessary.)
  64. img.resize_to_fill!(new_width.to_f,new_height.to_f)
  65. end
  66. FileUtils.mkdir_p File.dirname(cached_file)
  67. img.write(cached_file)
  68. end
  69. cached_file
  70. end
  71.  
  72. # clean extraction of filename
  73. protected
  74. def sanitize_filename(file_name)
  75. # get only the filename, not the whole path (from IE)
  76. just_filename = File.basename(file_name)
  77. # replace all non alphanumeric, underscore or perioids with underscore
  78. just_filename.gsub(/[^\w\.\_]/,'_')
  79. end
  80. def backwards_compat
  81. self.relation = self.attachable_type + '_' + self.attachable_id.to_s
  82. end
  83. def save_file
  84. return false unless self.temp_file
  85. config = (AppConfig.uploads || {}).symbolize_keys
  86. self.url_path = "#{self.attachable_type}/#{self.attachable_id}/#{self.id}_#{self.file_name}"
  87. self.file_system_path = self.url_path.gsub(/\//,'\\') # .NET likes backslashes for file paths (Backwards compat)
  88. FileUtils.mkdir_p config[:dir] + File.dirname(self.url_path)
  89. FileUtils.copy(self.temp_file.local_path, config[:dir] + '/' + self.url_path)
  90. self.save!
  91. end
  92. def delete_file
  93. FileUtils.remove(self.full_path) if File.exists? self.full_path
  94. end
  95. end
Add Comment
Please, Sign In to add comment