2010-04-13 00:40:47 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
class FileUploader < CarrierWave::Uploader::Base
|
|
|
|
|
2012-07-24 23:26:16 +02:00
|
|
|
@@generate_file_name = ''
|
|
|
|
@@original_filename = ''
|
|
|
|
|
2012-07-27 01:08:25 +02:00
|
|
|
|
2010-04-13 00:40:47 +02:00
|
|
|
# Include RMagick or ImageScience support
|
2011-04-11 06:38:53 +02:00
|
|
|
# include CarrierWave::RMagick
|
2010-04-13 00:40:47 +02:00
|
|
|
# include CarrierWave::ImageScience
|
2012-07-20 22:50:30 +02:00
|
|
|
include CarrierWave::MiniMagick
|
2010-04-13 00:40:47 +02:00
|
|
|
# Choose what kind of storage to use for this uploader
|
2010-04-14 05:56:13 +02:00
|
|
|
if ENV['S3_KEY']
|
2011-05-08 14:59:52 +02:00
|
|
|
storage :fog
|
2012-07-20 22:50:30 +02:00
|
|
|
|
2010-04-14 07:00:48 +02:00
|
|
|
def cache_dir
|
2011-04-25 11:38:10 +02:00
|
|
|
"#{Rails.root.to_s}/tmp/uploads" if ENV['HEROKU'] == 'true'
|
2010-04-14 07:00:48 +02:00
|
|
|
end
|
2010-04-14 05:56:13 +02:00
|
|
|
else
|
|
|
|
storage :file
|
|
|
|
end
|
2010-04-13 00:40:47 +02:00
|
|
|
|
|
|
|
# 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
|
2010-04-13 21:10:40 +02:00
|
|
|
#{}"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
|
2012-07-24 23:26:16 +02:00
|
|
|
ENV['STORAGE_PATH'] + "/files/#{model.album.path}/#{model.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def filename
|
|
|
|
unless @@original_filename == original_filename
|
|
|
|
@@original_filename = original_filename
|
|
|
|
@@generate_file_name = "#{::SecureRandom.hex(8)}#{File.extname(original_filename).downcase}" if original_filename
|
|
|
|
end
|
|
|
|
@@generate_file_name
|
2010-04-13 00:40:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Provide a default URL as a default if there hasn't been a file uploaded
|
|
|
|
# def default_url
|
|
|
|
# "/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
|
|
|
|
|
|
|
|
# Create different versions of your uploaded files
|
2012-07-20 22:50:30 +02:00
|
|
|
version :collection do
|
2012-07-24 23:26:16 +02:00
|
|
|
process :resize_to_fill => [260, 180]
|
2012-07-20 22:50:30 +02:00
|
|
|
|
|
|
|
def store_dir
|
2012-07-24 23:26:16 +02:00
|
|
|
ENV['STORAGE_PATH'] + "/thumbs/#{model.album.path}/#{model.id}"
|
2012-07-20 22:50:30 +02:00
|
|
|
end
|
|
|
|
end
|
2012-07-24 23:26:16 +02:00
|
|
|
|
2012-07-20 22:50:30 +02:00
|
|
|
version :album do
|
|
|
|
process :resize_to_fill => [100, 100]
|
|
|
|
|
|
|
|
def store_dir
|
2012-07-24 23:26:16 +02:00
|
|
|
ENV['STORAGE_PATH'] + "/thumbs/#{model.album.path}/#{model.id}"
|
2012-07-20 22:50:30 +02:00
|
|
|
end
|
|
|
|
end
|
2012-07-24 23:26:16 +02:00
|
|
|
|
2012-07-26 22:26:23 +02:00
|
|
|
######################################################################################################################
|
|
|
|
# Note
|
|
|
|
# The default grid system provided in Bootstrap utilizes 12 columns that
|
|
|
|
# render out at widths of 724px, 940px (default without responsive CSS included), and 1170px.
|
|
|
|
# Below 767px viewports, the columns become fluid and stack vertically.
|
|
|
|
|
|
|
|
version :middle do
|
2012-07-27 01:08:25 +02:00
|
|
|
process :resize_to_fill => [742, 500]
|
2012-07-26 22:26:23 +02:00
|
|
|
|
|
|
|
def store_dir
|
|
|
|
ENV['STORAGE_PATH'] + "/thumbs/#{model.album.path}/#{model.id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
version :large do
|
2012-07-27 01:08:25 +02:00
|
|
|
process :resize_to_fill => [940, 600]
|
2012-07-20 22:50:30 +02:00
|
|
|
|
|
|
|
def store_dir
|
2012-07-24 23:26:16 +02:00
|
|
|
ENV['STORAGE_PATH'] + "/thumbs/#{model.album.path}/#{model.id}"
|
2012-07-20 22:50:30 +02:00
|
|
|
end
|
|
|
|
end
|
2012-07-24 23:26:16 +02:00
|
|
|
|
2012-07-26 22:26:23 +02:00
|
|
|
version :largest do
|
2012-07-27 01:08:25 +02:00
|
|
|
process :resize_to_fill => [1170, 600]
|
2012-07-20 22:50:30 +02:00
|
|
|
|
|
|
|
def store_dir
|
2012-07-24 23:26:16 +02:00
|
|
|
ENV['STORAGE_PATH'] + "/thumbs/#{model.album.path}/#{model.id}"
|
2012-07-20 22:50:30 +02:00
|
|
|
end
|
|
|
|
end
|
2012-07-26 22:26:23 +02:00
|
|
|
######################################################################################################################
|
|
|
|
|
2010-04-13 00:40:47 +02:00
|
|
|
|
|
|
|
# Add a white list of extensions which are allowed to be uploaded,
|
|
|
|
# for images you might use something like this:
|
2012-07-20 22:50:30 +02:00
|
|
|
def extension_white_list
|
|
|
|
%w(jpg jpeg gif png bmp tiff)
|
|
|
|
end
|
2010-04-13 00:40:47 +02:00
|
|
|
end
|