daily pastebin goal
55%
SHARE
TWEET

Untitled

a guest Jan 24th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # encoding: utf-8
  2.  
  3. class ImageUploader < CarrierWave::Uploader::Base
  4.  
  5.   # Include RMagick or ImageScience support:
  6.   #include CarrierWave::RMagick
  7.   # include CarrierWave::ImageScience
  8.  
  9.   # Choose what kind of storage to use for this uploader:
  10.   storage :file
  11.   # storage :s3
  12.  
  13.   # Override the directory where uploaded files will be stored.
  14.   # This is a sensible default for uploaders that are meant to be mounted:
  15.   def store_dir
  16.     #"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  17.     #"uploads/#{model.class.to_s.underscore}/#{model.company_id}"
  18.     "uploads/#{model.company_id}"
  19.   end
  20. before_save :convert_to_mp3
  21.  
  22. def reconvert_to_mp3
  23.   wavfile = Tempfile.new(".wav")
  24.   wavfile.binmode
  25.  
  26.   open(wav.url) do |f|
  27.     wavfile << f.read
  28.   end
  29.  
  30.   wavfile.close
  31.  
  32.   convert_tempfile(wavfile)
  33. end
  34.  
  35. def convert_to_mp3
  36.   tempfile = wav.queued_for_write[:original]
  37.  
  38.   unless tempfile.nil?
  39.     convert_tempfile(tempfile)
  40.   end
  41. end
  42.  
  43. def convert_tempfile(tempfile)
  44.   dst = Tempfile.new(".mp3")
  45.  
  46.   cmd_args = [File.expand_path(tempfile.path), File.expand_path(dst.path)]
  47.   system("lame", *cmd_args)
  48.  
  49.   dst.binmode
  50.   io = StringIO.new(dst.read)
  51.   dst.close
  52.  
  53.   io.original_filename = "sound.mp3"
  54.   io.content_type = "audio/mpeg"
  55.  
  56.   self.mp3 = io
  57. end
  58.   # Provide a default URL as a default if there hasn't been a file uploaded:
  59.   # def default_url
  60.   #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  61.   # end
  62.  
  63.   # Process files as they are uploaded:
  64.   # process :scale => [200, 300]
  65.   #
  66.   # def scale(width, height)
  67.   #   # do something
  68.   # end
  69.  
  70.   # Create different versions of your uploaded files:
  71.   #version :thumb do
  72.     #process :resize_to_limit => [200, 200]
  73.   #end
  74.  
  75.   # Add a white list of extensions which are allowed to be uploaded.
  76.   # For images you might use something like this:
  77.   # def extension_white_list
  78.   #   %w(jpg jpeg gif png)
  79.   # end
  80.  
  81.   # Override the filename of the uploaded files:
  82.   # def filename
  83.   #   "something.jpg" if original_filename
  84.   # end
  85.  
  86. end
RAW Paste Data
Top