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-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)
|
2023-09-08 17:35:16 +08:00
|
|
|
find_by(id: id)&.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
|
|
|
|
|
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
|
2017-03-21 23:15:11 +08:00
|
|
|
end
|