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
How to Create a Symbol from a String That Has Whitespaces
How to Write a Rails Mixin That Spans Across Model, Controller, and View
How to Get the File Extension from a Url
How to Store Enum as String to Database in Rails
Set Default Stage with Capistrano 3
Bundle Command Not Found Windows X64
How to Run and Debug Ruby on Rails from Visual Studio Code
Ruby Sandboxing VS. Integrating a Scripting Language
Mechanize How to Get Current Url
Limiting Characters/Words in View - Ruby on Rails
Testing Hash Contents Using Rspec
Rails Activerecord Query Date Range
Import SASS Partial Over Http Instead of Filesystem
When and Where Do I Require Files in a Rails Application
Difference Between As_JSON and To_JSON Method in Ruby