mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-26 17:51:13 +08:00
Implement report template loader [SCI-10999]
This commit is contained in:
parent
36821c691f
commit
08bb1c9dc6
1 changed files with 37 additions and 0 deletions
37
config/initializers/report_template_loader.rb
Normal file
37
config/initializers/report_template_loader.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'httparty'
|
||||
require 'active_storage/service/s3_service'
|
||||
|
||||
template_zip_url_string = ENV.fetch('REPORT_TEMPLATES_ZIP_URL', nil)
|
||||
|
||||
return unless template_zip_url_string
|
||||
|
||||
template_zip_url = URI.parse(template_zip_url_string)
|
||||
contents = case template_zip_url.scheme
|
||||
when 'https'
|
||||
HTTParty.get(template_zip_url).body
|
||||
when 's3'
|
||||
ActiveStorage::Service::S3Service.new(
|
||||
bucket: template_zip_url.host
|
||||
).download(template_zip_url.path[1..])
|
||||
end
|
||||
|
||||
root_folder = nil
|
||||
Zip::File.open_buffer(StringIO.new(contents)) do |zip|
|
||||
zip.each do |entry|
|
||||
# set root zip folder
|
||||
root_folder = entry.name and next if entry.name.count('/') == 1
|
||||
|
||||
# create path while omitting root zip folder
|
||||
path = Rails.root.join('app', 'views', 'reports', entry.name.sub(root_folder, ''))
|
||||
path.dirname.mkpath
|
||||
|
||||
# don't try and write file if entry is a folder
|
||||
next if entry.name.ends_with?('/')
|
||||
|
||||
path.open('wb') do |f|
|
||||
f.write(entry.get_input_stream.read)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue