Guest User

Untitled

a guest
May 7th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. require 'ftools'
  2. require 'RMagick'
  3.  
  4. module SlideshowUpload
  5. @@image_types = ['image/jpeg', 'image/pjpeg', 'image/gif', 'image/png', 'image/x-png', 'image/jpg']
  6. @@slideshows_path = File.join(RAILS_ROOT, 'slideshows')
  7.  
  8. mattr_reader :image_types, :slideshows_path, :file_data
  9. mattr_accessor :slideshow_path
  10.  
  11. def self.included(base)
  12. base.send :extend, ImageProcessor
  13.  
  14. base.class_eval do
  15. after_save :write_file
  16. after_destroy :destroy_images
  17. end
  18. end
  19.  
  20. def image_data=(file_data)
  21. return nil if file_data.nil? || file_data.size == 0
  22. @image_data = file_data
  23. end
  24.  
  25. def file_is_image?
  26. @image_types.include?(@image_data.content_type)
  27. end
  28.  
  29. def write_image
  30. if @image_data && file_is_image?
  31. @slideshow_path = File.join(@slideshows_path, "slideshow_#{id}", "#{id}_original.#{@file_data}.#{extension}")
  32. File.makedirs(@slideshow_path) unless File.exist?(@slideshow_path)
  33. File.open(@slideshow_path, 'w') { |f| f.write(@image_data.read) }
  34. end
  35. end
  36.  
  37. def destroy_images
  38. FileUtils.rm_rf("#{@slideshows_path}/slideshow_#{id}")
  39. end
  40.  
  41. def extension
  42. @file_data.original_filename.split('.').last
  43. end
  44.  
  45.  
  46.  
  47. module ImageProcessor
  48. @@image_versions = {:thumb => 'c100x100', :default => '500x500'}
  49. mattr_reader :image_versions
  50.  
  51. def process!
  52. @image_versions.each do |iv|
  53.  
  54. end
  55. end
  56.  
  57. def resize(version, height, width, constrain)
  58.  
  59. end
  60. end
  61. end
Add Comment
Please, Sign In to add comment