Carrierwave How to Get the File Extension

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

Get file name of Carrierwave upload in rails controller

The solution was to get the filename from the carrierwave file like this:

  def create
@photo = Photo.new(params[:photo])
@photo.user_id = current_user.id

@photo.name = @photo.image.file.filename if @photo.name == ""

respond_to do |format|
if @photo.save
format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
format.json { render action: 'show', status: :created, location: @photo }
else
format.html { render action: 'new' }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

CarrierWave / MiniMagick not updating file extension after convert

I have solved this issue using version name customization. Note that I have chosen not to convert the original file, which is a bit different from my initial approach.

Here's a snippet of the code I've implemented:

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

version :large do
process resize_to_fit: [2000, 2000]
process convert: 'jpg'

def full_filename(for_file = model.file_name.file)
"large_#{for_file.sub('png', 'jpg')}"
end
end

version :thumb do
process resize_to_fit: [500, nil]
process convert: 'jpg'

def full_filename(for_file = model.file_name.file)
"thumb_#{for_file.sub('png', 'jpg')}"
end
end
end

How to detect a file type in a Rails app?

I guess (is not a good thing to let other people guess what you should already provided in your question) you have a model Document and your uploader is media, something like this:

class Document < ActiveRecord::Base
mount_uploader :media, MediaUploader
end

If this is the case, for each document you get the extension (document.media.file.extension.downcase) and compare it with 'jpg', 'jpeg', 'png'

<% document.each do |doc| %>
<% if ['jpg', 'jpeg', 'png'].include?(document.media.file.extension.downcase) %>
<%= link_to image_tag(doc.media_url(:thumb)) %>
<% else %>
<%= link_to doc.media.path %> # This link downloading the file
<% end %>
<% end %>

Carrierwave can give you the content type if you want it by using:

document.media.content_type # this returns image/png for a png file ...

Edit:

I think a better way is to check it like this (it's cleaner):

<% document.each do |doc| %>
<% if document.media.content_type =~ /image/ %>
<%= link_to image_tag(doc.media_url(:thumb)) %>
<% else %>
<%= link_to doc.media.path %> # This link downloading the file
<% end %>
<% end %>

Carrierwave Versions depending on the file extension

Try with something like:

  version :thumb, :if => :is_gif?

protected
def is_gif?(picture)
picture.extension.to_s.downcase == 'gif'
end


Related Topics



Leave a reply



Submit