Add support for remote OVH cloud storage

This commit is contained in:
Guillaume Lazzara 2016-05-13 16:08:51 +02:00
parent b15c2bbb2b
commit 833d7790c2
53 changed files with 243518 additions and 126 deletions

View file

@ -1,40 +1,28 @@
# encoding: utf-8
class CerfaUploader < CarrierWave::Uploader::Base
before :cache, :save_original_filename
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Choose what kind of storage to use for this uploader:
if Features.remote_storage
storage :fog
else
storage :file
end
# 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
"../uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
if Features.remote_storage
nil
else
"../uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
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:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].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
# Create different versions of your uploaded files:
# version :thumb do
# process :resize_to_fit => [50, 50]
# end
def cache_dir
'/tmp/tps-cache'
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
@ -42,10 +30,30 @@ class CerfaUploader < CarrierWave::Uploader::Base
%w(pdf doc docx xls xlsx ppt pptx odt ods odp)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
def filename
if original_filename || model.content_secure_token
if Features.remote_storage
@filename = "#{model.class.to_s.underscore}-#{secure_token}.pdf"
else original_filename
@filename = "#{model.class.to_s.underscore}.pdf"
end
else
@filename = nil
end
@filename
end
private
def secure_token
model.content_secure_token ||= generate_secure_token
end
def generate_secure_token
SecureRandom.uuid
end
def save_original_filename(file)
model.original_filename ||= file.original_filename if file.respond_to?(:original_filename)
end
end