2018-11-07 12:04:29 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-23 22:45:02 +08:00
|
|
|
require 'zip'
|
|
|
|
require 'fileutils'
|
2018-08-29 05:22:55 +08:00
|
|
|
require 'csv'
|
2017-03-23 22:45:02 +08:00
|
|
|
|
2017-04-13 19:01:05 +08:00
|
|
|
# To use ZipExport you have to define the generate_( type )_zip method!
|
|
|
|
# Example:
|
|
|
|
# def generate_(type)_zip(tmp_dir, data, options = {})
|
|
|
|
# attributes = options.fetch(:attributes) { :attributes_missing }
|
|
|
|
# file = FileUtils.touch("#{tmp_dir}/export.csv").first
|
|
|
|
# records = data
|
|
|
|
# CSV.open(file, 'wb') do |csv|
|
|
|
|
# csv << attributes
|
|
|
|
# records.find_each do |entity|
|
|
|
|
# csv << entity.values_at(*attributes.map(&:to_sym))
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class ZipExport < ApplicationRecord
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :user, optional: true
|
2019-05-20 22:35:24 +08:00
|
|
|
|
2019-07-05 22:56:05 +08:00
|
|
|
has_one_attached :zip_file
|
2017-03-23 22:45:02 +08:00
|
|
|
|
2019-04-30 06:43:34 +08:00
|
|
|
after_create :self_destruct
|
|
|
|
|
|
|
|
def self.delete_expired_export(id)
|
|
|
|
export = find_by_id(id)
|
2019-07-05 22:56:05 +08:00
|
|
|
export&.destroy
|
2017-03-23 22:45:02 +08:00
|
|
|
end
|
|
|
|
|
2019-07-09 16:28:15 +08:00
|
|
|
def zip_file_name
|
2019-07-26 20:16:44 +08:00
|
|
|
return '' unless zip_file.attached?
|
2017-03-23 22:45:02 +08:00
|
|
|
|
2019-07-09 16:28:15 +08:00
|
|
|
zip_file.blob&.filename&.to_s
|
2019-04-30 06:43:34 +08:00
|
|
|
end
|
|
|
|
|
2023-06-12 16:29:17 +08:00
|
|
|
def zip!(input_dir, output_file)
|
|
|
|
entries = Dir.glob('**/*', base: input_dir)
|
|
|
|
Zip::File.open(output_file, create: true) do |zipfile|
|
|
|
|
entries.each do |entry|
|
|
|
|
zipfile.add(entry, "#{input_dir}/#{entry}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-23 22:45:02 +08:00
|
|
|
def generate_exportable_zip(user, data, type, options = {})
|
2019-07-05 22:56:05 +08:00
|
|
|
I18n.backend.date_format = user.settings[:date_format] || Constants::DEFAULT_DATE_FORMAT
|
|
|
|
zip_input_dir = FileUtils.mkdir_p(File.join(Rails.root, "tmp/temp_zip_#{Time.now.to_i}")).first
|
|
|
|
tmp_zip_dir = FileUtils.mkdir_p(File.join(Rails.root, 'tmp/zip-ready')).first
|
2023-06-12 16:29:17 +08:00
|
|
|
tmp_full_zip_name = File.join(tmp_zip_dir, "export_#{Time.now.strftime('%F %H-%M-%S_UTC')}.zip")
|
2019-07-05 22:56:05 +08:00
|
|
|
|
2018-11-09 04:39:09 +08:00
|
|
|
fill_content(zip_input_dir, data, type, options)
|
2019-07-05 22:56:05 +08:00
|
|
|
zip!(zip_input_dir, tmp_zip_file)
|
2023-06-12 16:29:17 +08:00
|
|
|
zip_file.attach(io: File.open(tmp_full_zip_name), filename: tmp_zip_name)
|
2017-03-23 22:45:02 +08:00
|
|
|
generate_notification(user) if save
|
2018-11-09 04:19:17 +08:00
|
|
|
ensure
|
2023-06-12 16:29:17 +08:00
|
|
|
FileUtils.rm_rf([zip_input_dir, tmp_full_zip_name], secure: true)
|
2017-03-23 22:45:02 +08:00
|
|
|
end
|
|
|
|
|
2018-08-29 06:21:17 +08:00
|
|
|
handle_asynchronously :generate_exportable_zip
|
2017-04-13 19:01:05 +08:00
|
|
|
|
2018-08-22 03:07:45 +08:00
|
|
|
private
|
|
|
|
|
2019-04-30 06:43:34 +08:00
|
|
|
def self_destruct
|
|
|
|
ZipExport.delay(run_at: Constants::EXPORTABLE_ZIP_EXPIRATION_DAYS.days.from_now)
|
|
|
|
.delete_expired_export(id)
|
|
|
|
end
|
|
|
|
|
2023-06-12 16:29:17 +08:00
|
|
|
def method_missing(method_name, *args, &block)
|
|
|
|
return super unless method_name.to_s.start_with?('generate_')
|
|
|
|
|
|
|
|
raise StandardError, 'Method is missing! To use this zip_export you have to define a method: generate_( type )_zip.'
|
2018-08-22 03:07:45 +08:00
|
|
|
end
|
|
|
|
|
2017-04-13 19:27:43 +08:00
|
|
|
def respond_to_missing?(method_name, include_private = false)
|
2023-06-12 16:29:17 +08:00
|
|
|
method_name.to_s.start_with?('generate_') || super
|
2017-04-13 19:27:43 +08:00
|
|
|
end
|
2017-03-23 22:45:02 +08:00
|
|
|
|
|
|
|
def fill_content(dir, data, type, options = {})
|
2017-04-13 19:01:05 +08:00
|
|
|
eval("generate_#{type}_zip(dir, data, options)")
|
2017-03-23 22:45:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_notification(user)
|
|
|
|
notification = Notification.create(
|
|
|
|
type_of: :deliver,
|
|
|
|
title: I18n.t('zip_export.notification_title'),
|
2017-03-24 19:10:07 +08:00
|
|
|
message: "<a data-id='#{id}' " \
|
2019-03-07 23:46:05 +08:00
|
|
|
"data-turbolinks='false' " \
|
2017-03-24 19:10:07 +08:00
|
|
|
"href='#{Rails.application
|
|
|
|
.routes
|
|
|
|
.url_helpers
|
|
|
|
.zip_exports_download_path(self)}'>" \
|
2019-07-09 16:28:15 +08:00
|
|
|
"#{zip_file_name}</a>"
|
2017-03-23 22:45:02 +08:00
|
|
|
)
|
|
|
|
UserNotification.create(notification: notification, user: user)
|
|
|
|
end
|
|
|
|
|
2017-06-12 23:49:42 +08:00
|
|
|
def generate_repositories_zip(tmp_dir, data, _options = {})
|
2017-05-29 20:42:55 +08:00
|
|
|
file = FileUtils.touch("#{tmp_dir}/export.csv").first
|
|
|
|
File.open(file, 'wb') { |f| f.write(data) }
|
|
|
|
end
|
2017-03-21 23:15:11 +08:00
|
|
|
end
|