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

@ -10,8 +10,12 @@ class Cerfa < ActiveRecord::Base
end
def content_url
unless content.url.nil?
(Downloader.new content, 'CERFA').url
if Features.remote_storage and !content.url.nil?
(RemoteDownloader.new content.filename).url
else
unless content.url.nil?
(LocalDownloader.new content, 'CERFA').url
end
end
end
end

View file

@ -18,9 +18,13 @@ class PieceJustificative < ActiveRecord::Base
end
def content_url
unless content.url.nil?
(Downloader.new content,
(type_de_piece_justificative.nil? ? content.file.original_filename : type_de_piece_justificative.libelle)).url
if Features.remote_storage and !content.url.nil?
(RemoteDownloader.new content.filename).url
else
unless content.url.nil?
(LocalDownloader.new content,
(type_de_piece_justificative.nil? ? content.original_filename : type_de_piece_justificative.libelle)).url
end
end
end

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

View file

@ -1,6 +1,6 @@
require 'securerandom'
class Downloader
class LocalDownloader
BASE_PATH_DISK = File.join(Rails.root, "public/downloads/")
def initialize(filename, filename_suffix = '')

View file

@ -1,40 +1,28 @@
# encoding: utf-8
class PieceJustificativeUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
before :cache, :save_original_filename
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
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,34 @@ class PieceJustificativeUploader < 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
def original_filename
model.original_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

View file

@ -2,39 +2,26 @@
class ProcedureLogoUploader < CarrierWave::Uploader::Base
# 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
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 +29,27 @@ class ProcedureLogoUploader < CarrierWave::Uploader::Base
%w(jpg jpeg 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
# "something.jpg" if original_filename
# end
def filename
if original_filename || model.logo_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.logo_secure_token ||= generate_secure_token
end
def generate_secure_token
SecureRandom.uuid
end
end

View file

@ -0,0 +1,11 @@
class RemoteDownloader
DEST_URL = "https://storage.apientreprise.fr/" + CarrierWave::Uploader::Base.fog_directory + '/'
def initialize(filename)
@filename = filename
end
def url
@url ||= File.join(DEST_URL, @filename)
end
end

View file

@ -16,7 +16,7 @@
\-
%span#piece_justificative
%b
= link_to com.piece_justificative.content.file.original_filename, com.piece_justificative.content_url, style:'color: green', target: '_blank'
= link_to com.piece_justificative.original_filename, com.piece_justificative.content_url, style:'color: green', target: '_blank'
%br
.description#body