2014-12-29 00:13:49 +08:00
|
|
|
class UserExport < ActiveRecord::Base
|
2014-12-24 17:11:41 +08:00
|
|
|
|
|
|
|
def self.get_download_path(filename)
|
2014-12-29 00:13:49 +08:00
|
|
|
path = File.join(UserExport.base_directory, filename)
|
2014-12-24 17:11:41 +08:00
|
|
|
if File.exists?(path)
|
|
|
|
return path
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.remove_old_exports
|
2014-12-29 00:13:49 +08:00
|
|
|
expired_exports = UserExport.where('created_at < ?', 2.days.ago).to_a
|
2014-12-24 17:11:41 +08:00
|
|
|
expired_exports.map do |expired_export|
|
|
|
|
file_name = "export_#{expired_export.id}.csv"
|
2014-12-29 00:13:49 +08:00
|
|
|
file_path = "#{UserExport.base_directory}/#{file_name}"
|
|
|
|
|
2014-12-24 17:11:41 +08:00
|
|
|
if File.exist?(file_path)
|
|
|
|
File.delete(file_path)
|
|
|
|
end
|
2014-12-29 00:13:49 +08:00
|
|
|
UserExport.find(expired_export.id).destroy
|
2014-12-24 17:11:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_directory
|
|
|
|
File.join(Rails.root, "public", "uploads", "csv_exports", RailsMultisite::ConnectionManagement.current_db)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
2014-12-29 00:13:49 +08:00
|
|
|
# Table name: user_exports
|
2014-12-24 17:11:41 +08:00
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# export_type :string(255) not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
#
|