scinote-web/app/models/zip_export.rb

40 lines
803 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-03-23 22:45:02 +08:00
require 'zip'
require 'fileutils'
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
has_one_attached :zip_file
2017-03-23 22:45:02 +08:00
after_create :self_destruct
def self.delete_expired_export(id)
find_by(id: id)&.destroy
2017-03-23 22:45:02 +08:00
end
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
zip_file.blob&.filename&.to_s
end
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
private
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