scinote-web/app/models/zip_export.rb

105 lines
3.3 KiB
Ruby
Raw Normal View History

2017-03-23 22:45:02 +08:00
require 'zip'
require 'fileutils'
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-03-21 23:15:11 +08:00
class ZipExport < ActiveRecord::Base
belongs_to :user
has_attached_file :zip_file
validates_attachment :zip_file,
content_type: { content_type: 'application/zip' }
2017-03-23 22:45:02 +08:00
# When using S3 file upload, we can limit file accessibility with url signing
def presigned_url(style = :original,
download: false,
timeout: Constants::URL_SHORT_EXPIRE_TIME)
if stored_on_s3?
if download
download_arg = 'attachment; filename=' + URI.escape(zip_file_file_name)
else
download_arg = nil
end
signer = Aws::S3::Presigner.new(client: S3_BUCKET.client)
signer.presigned_url(:get_object,
bucket: S3_BUCKET.name,
key: zip_file.path(style)[1..-1],
expires_in: timeout,
response_content_disposition: download_arg)
end
end
def stored_on_s3?
zip_file.options[:storage].to_sym == :s3
end
def generate_exportable_zip(user, data, type, options = {})
FileUtils.mkdir_p(File.join(Rails.root, 'tmp/zip-ready'))
dir_to_zip = FileUtils.mkdir_p(
File.join(Rails.root, "tmp/temp-zip-#{Time.now.to_i}")
).first
output_file = File.new(
File.join(Rails.root, "tmp/zip-ready/export-#{Time.now.to_i}.zip"),
'w+'
)
fill_content(dir_to_zip, data, type, options)
zip!(dir_to_zip, output_file.path)
self.zip_file = File.open(output_file)
generate_notification(user) if save
end
handle_asynchronously :generate_exportable_zip
2017-04-13 19:27:43 +08:00
private
2017-04-13 19:01:05 +08:00
def method_missing(m, *args, &block)
puts 'Method is missing! To use this zip_export you have to ' \
2017-04-13 19:27:43 +08:00
'define a method: generate_( type )_zip.'
2017-04-13 20:34:58 +08:00
object.public_send(m, *args, &block)
2017-04-13 19:01:05 +08:00
end
2017-04-13 19:27:43 +08:00
def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?(' generate_') || super
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}' " \
"href='#{Rails.application
.routes
.url_helpers
.zip_exports_download_path(self)}'>" \
2017-03-23 22:45:02 +08:00
"#{zip_file_file_name}</a>"
)
UserNotification.create(notification: notification, user: user)
end
def zip!(input_dir, output_file)
files = Dir.entries(input_dir)
files.delete_if { |el| el == '..' || el == '.' }
Zip::File.open(output_file, Zip::File::CREATE) do |zipfile|
files.each do |filename|
zipfile.add(filename, input_dir + '/' + filename)
end
end
end
2017-03-21 23:15:11 +08:00
end