Class: MediaUploader

Inherits:
CarrierWave::Uploader::Base
  • Object
show all
Includes:
CarrierWave::MimeTypes, CarrierWave::MiniMagick, DmCore::AccountHelper
Defined in:
app/uploaders/media_uploader.rb

Overview

For uploading files into the media library. Creates versions based on settings in the Account. Will also create retina versions automatically. Files are stored in the theme’s ‘media’ folder. User can specify a single subfolder to store the file, giving a little extra flexibility. At the moment, files are in the public folders. Future version will allow uploading protected files.

make sure ghostscript is installed for PDF thumbnailing on OSX, ‘brew install ghostscript`


Instance Method Summary collapse

Instance Method Details

#extension_white_listObject

Add a white list of extensions which are allowed to be uploaded. For images you might use something like this:




143
144
145
# File 'app/uploaders/media_uploader.rb', line 143

def extension_white_list
  %w(jpg jpeg gif png mp3 mp4 m4v ogg webm pdf css js)
end

#resize_to_width(width) ⇒ Object

We basically want the width to be the max, allowing the height to grow




31
32
33
34
35
36
37
# File 'app/uploaders/media_uploader.rb', line 31

def resize_to_width(width)
  manipulate! do |img|
    img.resize "#{width}>"
    img = yield(img) if block_given?
    img
  end
end

#retina_quality(percentage) ⇒ Object

From: github.com/jhnvz/retina_rails Process retina quality of the image. Works with ImageMagick and MiniMagick

Parameters

percentage (Int)

quality in percentage



65
66
67
68
69
70
71
72
73
74
75
# File 'app/uploaders/media_uploader.rb', line 65

def retina_quality(percentage)
  manipulate! do |img|
    if defined?(Magick)
      img.write(current_path) { self.quality = percentage } unless img.quality == percentage
    elsif defined?(MiniMagick)
      img.quality(percentage.to_s)
    end
    img = yield(img) if block_given?
    img
  end
end

#size_image_pdf(width) ⇒ Object

Convert to png if a pdf, then size to a specfic width




53
54
55
56
# File 'app/uploaders/media_uploader.rb', line 53

def size_image_pdf(width)
  self.convert(:jpg) if pdf?(self)
  self.resize_to_width(width)
end

#store_dirObject

Everything gets stored in the ‘media’ folder




24
25
26
27
# File 'app/uploaders/media_uploader.rb', line 24

def store_dir
  partition_dir = model.folder
  "#{(false)}/#{partition_dir}"
end

#thumb_image_pdf(width, height) ⇒ Object

If a pdf, convert to jpg and size, maintain aspect ration and pad to square If an image, resize it to a cropped square




42
43
44
45
46
47
48
49
# File 'app/uploaders/media_uploader.rb', line 42

def thumb_image_pdf(width, height)
  if pdf?(self)
    self.convert(:jpg)
    self.resize_and_pad(width, height)
  else
    self.resize_to_fill(width, height)
  end
end