Add downloader for upload files outwards of public folder

This commit is contained in:
Xavier J 2016-01-05 15:53:01 +01:00
parent 4961f39a71
commit e7570564f8
2 changed files with 44 additions and 0 deletions

View file

@ -0,0 +1,36 @@
require 'securerandom'
class Downloader
BASE_PATH_DISK = File.join(Rails.root, "public/")
def initialize(filename, filename_suffix = '')
@filename = filename.to_s
@filename_suffix = filename_suffix.empty? ? '' : "_#{filename_suffix}"
@extension = @filename.split(/[.]/).last
generate_random_base_path!
FileUtils.cp @filename, "#{@base_path}/#{@filename_suffix}.#{@extension}"
end
def url
@url ||= File.join(TPS::Application::URL, random_folder_name, "#{@filename_suffix}.#{@extension}")
end
protected
attr_accessor :random_folder_name
def generate_random_base_path!
@base_path ||= begin
loop do
self.random_folder_name = SecureRandom.hex
base_path = File.join(BASE_PATH_DISK, self.random_folder_name)
unless File.directory?(base_path)
Dir.mkdir(base_path)
break base_path
end
end
end
end
end

View file

@ -26,5 +26,13 @@ module TPS
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
if Rails.env.production?
URL = "https://tps.apientreprise.fr/"
elsif Rails.env.staging?
URL = "https://tps-dev.apientreprise.fr/"
else
URL = "http://localhost:3000/"
end
end
end