Last active
October 12, 2015 00:41
-
-
Save ZakharDay/9f55569df32afe44f386 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 ApplicationUploader < CarrierWave::Uploader::Base | |
# include CarrierWave::RMagick | |
include CarrierWave::MiniMagick | |
include CarrierWave::MimeTypes | |
include Sprockets::Helpers::RailsHelper | |
include Sprockets::Helpers::IsolatedHelper | |
CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/ | |
storage :file | |
process :set_content_type | |
process quality: 70 | |
before :store, :remember_cache_id | |
after :store, :delete_tmp_dir | |
def extension_white_list | |
%w(jpg jpeg gif png) | |
end | |
def store_dir | |
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" | |
end | |
def filename | |
if original_filename | |
if model && model.read_attribute(mounted_as).present? | |
model.read_attribute(mounted_as) | |
else | |
@name ||= "#{secure_token(10)}.#{file.extension}" | |
end | |
end | |
end | |
# store! nil's the cache_id after it finishes so we need to remember it for deletion | |
def remember_cache_id(new_file) | |
@cache_id_was = cache_id | |
end | |
def delete_tmp_dir(new_file) | |
# make sure we don't delete other things accidentally by checking the name pattern | |
if @cache_id_was.present? && @cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/ | |
FileUtils.rm_rf(File.join(root, cache_dir, @cache_id_was)) | |
end | |
end | |
protected | |
def secure_token(length=16) | |
var = :"@#{mounted_as}_secure_token" | |
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(length/2)) | |
end | |
end |
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 AvatarUploader < ApplicationUploader | |
version :large do | |
process :crop | |
end | |
version :medium, from_version: :large do | |
resize_to_fit(56, 56) | |
end | |
version :small, from_version: :large do | |
resize_to_fit(40, 40) | |
end | |
def default_url | |
asset_path("fallback/avatar.svg") | |
end | |
def crop | |
manipulate! do |img| | |
x, y, w, h = 0, 0, 0, 0 | |
if !model.avatar_crop_y.nil? | |
y = model.avatar_crop_y.to_i | |
h = 256 | |
img.resize "256" | |
elsif !model.avatar_crop_x.nil? | |
x = model.avatar_crop_x.to_i | |
w = 256 | |
img.resize "x256" | |
end | |
img.crop("#{w}x#{h}+#{x}+#{y}") | |
img | |
end | |
end | |
end |
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 ImageUploader < ApplicationUploader | |
version :compressed do | |
resize_to_fit(928, nil) | |
end | |
version :large, :from_version => :compressed do | |
process :crop_large | |
end | |
version :poster, :from_version => :compressed do | |
process :crop_poster | |
end | |
version :teaser, :from_version => :large do | |
resize_to_fit(640, 356) | |
end | |
version :thumb, :from_version => :large do | |
resize_to_fit(288, 160) | |
end | |
version :link, :from_version => :large do | |
resize_to_fit(63, 35) | |
end | |
def store_dir | |
"uploads/publication_image/#{mounted_as}/#{model.id}" | |
end | |
def default_url | |
if version_name == 'poster' | |
image_name = 'poster' | |
else | |
image_name = 'image' | |
end | |
asset_path("fallback/#{image_name}.svg") | |
end | |
def crop_large | |
manipulate! do |img| | |
x, y, w, h = 0, 0, 0, 0 | |
if !model.crop_y.nil? | |
y = model.crop_y.to_i | |
h = 515 | |
img.resize "928" | |
elsif !model.crop_x.nil? | |
x = model.crop_x.to_i | |
w = 928 | |
img.resize "x515" | |
end | |
img.crop("#{w}x#{h}+#{x}+#{y}!") | |
img | |
end | |
end | |
def crop_poster | |
manipulate! do |img| | |
x, y, w, h = 0, 0, 0, 0 | |
if !model.crop_y.nil? | |
y = model.crop_y.to_i/(928.to_f/284) | |
h = 428 | |
img.resize "284" | |
elsif !model.crop_x.nil? | |
x = model.crop_x.to_i/(515.to_f/428) | |
w = 284 | |
img.resize "x428" | |
end | |
img.crop("#{w}x#{h}+#{x}+#{y}!") | |
img | |
end | |
end | |
end |
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 TinymceUploader < ApplicationUploader | |
version :compressed do | |
process :resize_to_width => [800, nil] | |
end | |
def resize_to_width(width, height) | |
manipulate! do |img| | |
if img[:width] >= width | |
img.resize "#{width}x#{img[:height]}" | |
end | |
img = yield(img) if block_given? | |
img | |
end | |
end | |
def default_url | |
if version_name == 'poster' | |
image_name = 'poster' | |
else | |
image_name = 'image' | |
end | |
asset_path("fallback/#{image_name}.svg") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment