Skip to content

Instantly share code, notes, and snippets.

@crazymykl
Created December 26, 2012 16:44
Show Gist options
  • Save crazymykl/4381362 to your computer and use it in GitHub Desktop.
Save crazymykl/4381362 to your computer and use it in GitHub Desktop.
The uploader showing carrierwaveuploader/carrierwave#937. I would like the version :pdf (only) to have different fog attributes.
# 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'
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
disposition = model.image? ? "inline" : "attachment"
{ "Content-Disposition" => "#{disposition}; 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
@elskwid
Copy link

elskwid commented Dec 29, 2012

Okay, I see what you're trying to do. For all the images you want the disposition to be "inline" and attachment for the pdf. Is that correct?

I'm going to fork with a possible suggestion.

@elskwid
Copy link

elskwid commented Dec 29, 2012

I defined the helper method on the specific version. I suspect something like this might work. Without having an environment set up like yours it's hard to tell. Give it a look and let me know if it helps.

https://gist.github.com/4405252

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment