Guest User

Untitled

a guest
Feb 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. class Work < ActiveRecord::Base
  2. belongs_to :medium
  3.  
  4. validates_presence_of :filename, :message => "is blank, indicating that no file was selected to be uploaded."
  5. validates_uniqueness_of :filename, :message => "already exists, try uploading another file or deleting first."
  6. validates_format_of :pagenumber, :with => /\d+[A-Za-z]+?/, :allow_nil => true, :message => 'must be digits followed by letters.'
  7.  
  8. def before_validation
  9. if self.title.nil? || self.title.empty?
  10. self.title = 'Untitled'
  11. end
  12. if self.pagenumber == ''
  13. self.pagenumber = nil
  14. end
  15. if self.size.nil? || self.title.empty?
  16. self.size = nil
  17. end
  18. end
  19.  
  20. def file=(uploaded_file)
  21. @uploaded_file = uploaded_file
  22. fn=self.filename=sanitize_filename(File.basename(@uploaded_file.path))
  23. fn = fn.to_s if File.exists?(self.path_to_tempfile)
  24. fn = fn.split('_')
  25. fn.pop
  26. self.filename = fn.join('_')
  27. end
  28.  
  29. def before_create
  30. require 'ImageUpload'
  31. File.open(self.path_to_tempfile, "w") { |f| f.write(@uploaded_file.read) }
  32.  
  33. full = ImageUpload::upload_image(self.path_to_tempfile, self.filename, 'full', 'exhibition', 720, 600)
  34. medium = ImageUpload::upload_image(self.path_to_tempfile, self.filename, 'med', 'exhibition', 600, 500)
  35. thumb = ImageUpload::upload_image(self.path_to_tempfile, self.filename, 'thumb', 'exhibition', 200, 167)
  36.  
  37. File.delete(self.path_to_tempfile)
  38. end
  39.  
  40. def before_destroy
  41. # delete the file from the filesystem
  42. File.delete self.path_to_images + self.filename + '_full.jpg'
  43. File.delete self.path_to_images + self.filename + '_med.jpg'
  44. File.delete self.path_to_images + self.filename + '_thumb.jpg'
  45. end
  46.  
  47. protected
  48. def sanitize_filename(value)
  49. just_filename = value.gsub(/^.*(\\|\/)/, '')
  50. just_filename.gsub(/[\W]/,'_')
  51. end
  52.  
  53. def path_to_tempfile
  54. RAILS_ROOT + '/public/images/exhibition/' + self.filename + '_temp.jpg'
  55. end
  56.  
  57. def path_to_images
  58. RAILS_ROOT + '/public/images/exhibition/'
  59. end
  60. end
  61.  
  62.  
  63. class ImageUpload
  64. def ImageUpload.upload_image(uploaded_file, filename, imagetype, category, maxwidth = 10000, maxheight = 100000)
  65. require 'RMagick'
  66. aspectratio = maxwidth.to_f / maxheight.to_f
  67. #pic = Magick::Image.read(imgfile).first
  68. pic = Magick::Image.read(uploaded_file).first
  69. imgwidth = pic.columns
  70. imgheight = pic.rows
  71. if (imgwidth > maxwidth) || (imgheight > maxheight)
  72. imgratio = imgwidth.to_f / imgheight.to_f
  73. imgratio > aspectratio ? scaleratio = maxwidth.to_f / imgwidth : scaleratio = maxheight.to_f / imgheight
  74. pic.thumbnail!(scaleratio)
  75. end
  76. pic.write(RAILS_ROOT + "/public/images/" + category + '/' + filename + '_' + imagetype + '.jpg')
  77. end
  78. end
Add Comment
Please, Sign In to add comment