Carrierwave and Correct File Extension Depending on Its Contents

Mixed file types with CarrierWave

According to the Carrierwave Docs you can do the conditional processing:

version :medium, :if => :image? do
process :resize_to_fit => [300, 300]
end

protected

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

Actually more full answer I found here

Carrierwave how to get the file extension

Determine file extension (I suppose a name for mounted uploader is 'file'):

file = my_model.file.url
extension = my_model.file.file.extension.downcase

Then prepare mime and disposition vars:

disposition = 'attachment'
mime = MIME::Types.type_for(file).first.content_type

if %w{jpg png jpg gif bmp}.include?(extension) or extension == "pdf"
disposition = 'inline'
end

(add other extensions if you want).

And then send the file:

send_file file, :type => mime, :disposition => disposition

How to correct file extension for thumbnail generation of movie with CarrierWave

I recently solved this by overriding the full_filename method for the :thumb version

version :thumb do
# do your processing
process :whatever

# redefine the name for this version
def full_filename(for_file=file)
super.chomp('mp4') + 'png'
end
end

I called super to get the default :thumb filename, then changed the extension from mp4 to png, but you could do anything.

For more info, the carrierwave wiki has a good article on How to: Customize your version file names. Check out the other wiki pages for lots of ideas.

Carrierwave (with FOG) filename extension mismatch in view

this has been working for me:

def full_filename(for_file=file)
super.chomp('wav') + '.png'
end

or for all filetypes:

def full_filename(for_file=file)
super.chomp(File.extname(super)) + '.png'
end

Carrierwave file upload with different file types

You should try using like this

class FileUploader < CarrierWave::Uploader::Base  
include CarrierWave::MiniMagick

process :process_image, if: :image?
process :process_pdf, if: :pdf?

protected

def image?(new_file)
new_file.content_type.start_with? 'image'
end

def pdf?(new_file)
new_file.content_type.start_with? 'application'
end

def process_image
# I process image here
end

def process_pdf
# I process pdf here
end
end

CarrierWave: create 1 uploader for multiple types of files

I came across this, and it looks like an example of how to solve this problem: https://gist.github.com/995663.

The uploader first gets loaded when you call mount_uploader, at which point things like if image? or elsif video? won't work, because there is no file to upload defined yet. You'll need the methods to be called when the class is instantiated instead.

What the link I gave above does, is rewrite the process method, so that it takes a list of file extensions, and processes only if your file matches one of those extensions

# create a new "process_extensions" method.  It is like "process", except
# it takes an array of extensions as the first parameter, and registers
# a trampoline method which checks the extension before invocation
def self.process_extensions(*args)
extensions = args.shift
args.each do |arg|
if arg.is_a?(Hash)
arg.each do |method, args|
processors.push([:process_trampoline, [extensions, method, args]])
end
else
processors.push([:process_trampoline, [extensions, arg, []]])
end
end
end

# our trampoline method which only performs processing if the extension matches
def process_trampoline(extensions, method, args)
extension = File.extname(original_filename).downcase
extension = extension[1..-1] if extension[0,1] == '.'
self.send(method, *args) if extensions.include?(extension)
end

You can then use this to call what used to be process

IMAGE_EXTENSIONS = %w(jpg jpeg gif png)
DOCUMENT_EXTENSIONS = %(exe pdf doc docm xls)
def extension_white_list
IMAGE_EXTENSIONS + DOCUMENT_EXTENSIONS
end

process_extensions IMAGE_EXTENSIONS, :resize_to_fit => [1024, 768]

For versions, there's a page on the carrierwave wiki that allows you to conditionally process versions, if you're on >0.5.4. https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing. You'll have to change the version code to look like this:

version :big, :if => :image? do
process :resize_to_limit => [160, 100]
end

protected
def image?(new_file)
new_file.content_type.include? 'image'
end


Related Topics



Leave a reply



Submit