Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 3.86 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class Wrapper < ActiveRecord::Base
  2.  
  3.   TypesWrapper = [ 'application/x-docx',
  4.       'application/x-doc', 'application/x-rtf',
  5.       'application/x-pps',  'application/x-ppt', 'application/x-pptx',
  6.       'application/x-xls',  'application/x-xlsx','application/x-csv',
  7.       'application/x-msg',
  8.       'application/x-zip',
  9.       'text/plain',  
  10.       'application/x-pdf','application/pdf',
  11.       'application/mp3',  'application/x-mp3',  'audio/mpeg',   'audio/mp3',
  12.       'image/gif',        'image/png',          'image/x-png',  'image/jpeg', 'image/jpg',
  13.       'video/mpeg',       'application/x-flv',  'video/quicktime' ,'application/x-wmv',
  14.       'application/x-avi']
  15.  
  16.   acts_as_commentable
  17.   acts_as_textiled :name, :info
  18.   belongs_to :user
  19.  
  20.         has_attached_file :data,
  21.     :url => "/:zfull_path",
  22.     :path => ":rails_root/public/:zfull_path",
  23.     :whiny => true
  24.  
  25.  
  26.   attr_protected :data_file_name, :data_content_type, :data_size
  27.  
  28.   before_post_process :validate_processor
  29.  
  30.   validates_attachment_size :data, :less_than => 10.megabytes
  31.   validates_attachment_content_type :data, :content_type => TypesWrapper
  32.  
  33.  
  34.   def proc_name
  35.     type, detail = data.content_type.scan(/^(video|audio|application|text|image)\/(.*?)$/).flatten
  36.     case type
  37.       when 'image'          then 'image'
  38.       when 'video','audio'  then type
  39.       when 'text'           then 'text'
  40.       when 'application'
  41.         case detail
  42.           when /pdf$/              then 'pdf'
  43.           when /mp3$/              then 'audio'
  44.           when /mov|flv|wmv|avi$/  then 'video'
  45.           when /csv|xls.?$/        then 'excel'
  46.           when /doc.?|txt|rtf$/    then 'word'
  47.           when /pp..?/             then 'powerpoint'
  48.           when 'msg'               then 'message'
  49.           when /^(?:x-)?zip$/      then 'zip'
  50.           else 'document'
  51.         end
  52.     end
  53.   end
  54.  
  55.   def validate_processor
  56.     case proc_name
  57.       when "image"    then
  58.          data.styles[:small] = { :geometry => "64x64>",   :format => "png", :processors => "thumbnail" }
  59.          data.styles[:thumb] = { :geometry => "128x128#", :format => "png", :processors => "thumbnail" }
  60.          data.styles[:large] = { :geometry => "300x400#", :format => "png", :processors => "thumbnail" }
  61.       when "document",'excel','word','powerpoint','text'
  62.          data.styles[:small] = { :geometry => "64x64>",   :format => "png", :processors => "empty" }
  63.          data.styles[:thumb] = { :geometry => "128x128#", :format => "png", :processors => "empty" }
  64.          data.styles[:large] = { :geometry => "300x400#", :format => "png", :processors => "empty" }
  65.       when "zip"
  66.          data.styles[:small] = { :geometry => "64x64>",   :format => "png", :processors => "empty" }
  67.          data.styles[:thumb] = { :geometry => "128x128#", :format => "png", :processors => "empty" }
  68.          data.styles[:large] = { :geometry => "300x400#", :format => "png", :processors => "zip_info" }
  69.       when "video"
  70.          data.styles[:small] = { :geometry => "64x64>",   :format => "png", :processors => "empty" }
  71.          data.styles[:thumb] = { :geometry => "128x128#", :format => "png", :processors => "video_thumb" }
  72.          data.styles[:large] = { :geometry => "300x400#", :format => "flv", :processors => "video_convert"}
  73.       when "audio"
  74.          data.styles[:small] = { :geometry => "64x64>",   :format => "png", :processors => "audio_tag" }
  75.          data.styles[:thumb] = { :geometry => "128x128#", :format => "png", :processors => "empty" }
  76.          data.styles[:large] = { :geometry => "300x400#", :format => "png", :processors => "empty" }      
  77.       when "pdf"
  78.          data.styles[:small] = { :geometry => "64x64>",   :format => "png", :processors => "empty" }
  79.          data.styles[:thumb] = { :geometry => "128x128#", :format => "png", :processors => "pdf" }
  80.          data.styles[:large] = { :geometry => "300x400#", :format => "png", :processors => "pdf" }  
  81.     end
  82.     true
  83.   end
  84.  
  85. end