-
-
Save elskwid/4405252 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
class DocumentUploader < CarrierWave::Uploader::Base | |
include ::CarrierWave::Backgrounder::Delay | |
include CarrierWave::MimeTypes | |
# Include RMagick or MiniMagick support: | |
include CarrierWave::RMagick | |
# include CarrierWave::MiniMagick | |
include CarrierWave::UNOConv | |
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility: | |
include Sprockets::Helpers::RailsHelper | |
include Sprockets::Helpers::IsolatedHelper | |
configure do |config| | |
config.remove_previously_stored_files_after_update = false | |
end | |
# Choose what kind of storage to use for this uploader: | |
# storage :file | |
storage :fog | |
# Override the directory where uploaded files will be stored. | |
# This is a sensible default for uploaders that are meant to be mounted: | |
def store_dir | |
"" | |
end | |
def cache_dir | |
# when we go distributed, this needs to be shared between servers. | |
'../tmp/uploads/' | |
end | |
# Provide a default URL as a default if there hasn't been a file uploaded: | |
def default_url | |
# # For Rails 3.1+ asset pipeline compatibility: | |
asset_path("icons/" + [version_name, "processing.gif"].compact.join('_')) | |
# | |
# "/images/fallback/" + [version_name, "default.png"].compact.join('_') | |
end | |
# Process files as they are uploaded: | |
# process scale: [200, 300] | |
# | |
# def scale(width, height) | |
# # do something | |
# end | |
process :set_content_type | |
process :exif_rotate, if: :image? | |
# Create different versions of your uploaded files: | |
version :thumb512, if: :image? do | |
process resize_to_fit: [512, 512] | |
end | |
version :pdf, if: :pdf_ready? do | |
process uno_convert: 'pdf' | |
# you can define methods right on the versions and they are available in the uploader instance for that version. | |
def fog_attributes | |
{ "Content-Disposition" => "attachment; filename=\"#{model.original_filename}\"" }.merge super | |
end | |
end | |
version :png512, from_version: :thumb512, if: :image? do | |
process convert: 'png' | |
end | |
version :thumb64, from_version: :thumb512, if: :image? do | |
process resize_to_fit: [64, 64] | |
end | |
# Add a white list of extensions which are allowed to be uploaded. | |
# For images you might use something like this: | |
# def extension_white_list | |
# %w(jpg jpeg gif png) | |
# end | |
# Override the filename of the uploaded files: | |
# Avoid using model.id or version_name here, see uploader/store.rb for details. | |
def filename | |
if original_filename | |
if (name = model.try(:read_attribute, :file)).present? | |
name | |
else | |
@name ||= "#{secure_token}.#{file.extension}" if original_filename.present? | |
end | |
end | |
end | |
def fog_attributes | |
{ "Content-Disposition" => "inline; filename=\"#{model.original_filename}\"" }.merge super | |
end | |
def secure_token | |
model.secure_token ||= SecureRandom.uuid | |
end | |
protected | |
def image? new_file | |
model.image? | |
end | |
def pdf_ready? new_file | |
model.pdf_ready? | |
end | |
# Rotates the image based on the EXIF Orientation | |
def exif_rotate | |
manipulate! { |img| img.auto_orient } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment