Class: AttachmentUploader

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valid_versionsObject



144
145
146
# File 'app/uploaders/attachment_uploader.rb', line 144

def self.valid_versions
  [:thumb, :medium, :video_thumb]
end

Instance Method Details

#cache_dirObject



26
27
28
29
30
31
32
# File 'app/uploaders/attachment_uploader.rb', line 26

def cache_dir
  # model.id || raise('Model has no id. But need one to save the file.')
  # "#{Rails.root}/tmp/uploads/#{Rails.env}_env/#{model.class.to_s.underscore}s/#{model.id}"
  Rails.root || raise('no rails root')
  Rails.env || raise('no rails env')
  "#{Rails.root}/tmp/uploads/#{Rails.env}_env/"
end

#coverObject

This method filteres out all pages except for the cover page. This is used when making a thumbnail for pdf files. pdf files can have several pages, but only the first page should be used for the thumbnail, not one thumbnail for each page.



93
94
95
96
97
# File 'app/uploaders/attachment_uploader.rb', line 93

def cover 
  manipulate! do |frame, index|
    frame if (not index) || index.zero?
  end
end

#filetitleObject



139
140
141
# File 'app/uploaders/attachment_uploader.rb', line 139

def filetitle
  File.basename(to_s)
end

#image?(new_file) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/uploaders/attachment_uploader.rb', line 85

def image?( new_file )
  new_file && new_file.content_type.present? && new_file.content_type.include?('image')
end

#image_or_pdf?(new_file) ⇒ Boolean

version :video_thumb, :if => :video? do

process :create_video_thumb
process :set_content_type => [ "image/jpeg" ]
def full_filename( for_file = model.attachment.file )
  "video-thumb.jpg"
end

end

def video?( new_file )

new_file.content_type.include?('video')

end

Returns:

  • (Boolean)


80
81
82
83
# File 'app/uploaders/attachment_uploader.rb', line 80

def image_or_pdf?( new_file )
  new_file && new_file.content_type.present? && 
    (new_file.content_type.include?('image') || new_file.content_type.include?('pdf'))
end

#modify_content_type(*args) ⇒ Object



99
100
101
102
# File 'app/uploaders/attachment_uploader.rb', line 99

def modify_content_type( *args )
  type = args[0] || "image/png"
  self.file.instance_variable_set( :@content_type, type )
end

#store_dirObject

Override the directory where uploaded files will be stored. This is a sensible default for uploaders that are meant to be mounted:



22
23
24
25
# File 'app/uploaders/attachment_uploader.rb', line 22

def store_dir
  model.id || raise('Model has no id. But need one to save the file.')
  "#{Rails.root}/uploads/#{Rails.env}_env/#{model.class.to_s.underscore}s/#{model.id}"
end

#url(version = nil) ⇒ Object

Override the filename of the uploaded files: Avoid using model.id or version_name here, see uploader/store.rb for details. def filename

"original.#{model.attachment.file.extension}" if original_filename

end



126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/uploaders/attachment_uploader.rb', line 126

def url(version = nil)
  model.id || raise('Model has no id.')
  if version
    filename = self.send(version).current_path
  else
    filename = self.current_path
  end
  filename || raise('No filename.')
  extension = File.extname(filename).gsub(/^./, '')
  basename = File.basename(filename).gsub(/.#{extension}$/, '')
  Rails.application.routes.url_helpers.attachment_download_path(id: model.id, basename: basename, extension: extension, version: version )
end