FEATURE: Refresh MaxmindDb during assets:precompile. (#7340)

This commit is contained in:
Bianca Nenciu
2019-04-10 12:37:29 +03:00
committed by Régis Hanol
parent 82e051077d
commit 4555d0c598
4 changed files with 53 additions and 25 deletions

View File

@ -16,6 +16,36 @@ class DiscourseIpInfo
@cache = LruRedux::ThreadSafeCache.new(2000)
end
def self.mmdb_path(name)
File.join(Rails.root, 'vendor', 'data', "#{name}.mmdb")
end
def self.mmdb_download(name)
require 'rubygems/package'
require 'zlib'
uri = URI("https://geolite.maxmind.com/download/geoip/database/#{name}.tar.gz")
tar_gz_file = Tempfile.new
begin
tar_gz_file.binmode
tar_gz_file.write(Net::HTTP.get(uri))
tar_gz_file.close
extractor = Gem::Package::TarReader.new(Zlib::GzipReader.open(tar_gz_file.path))
extractor.rewind
extractor.each do |entry|
next unless entry.full_name.ends_with?(".mmdb")
File.open(mmdb_path(name), "wb") { |f| f.write(entry.read) }
end
ensure
tar_gz_file.close
tar_gz_file.unlink
extractor.close
end
end
def mmdb_load(filepath)
begin
MaxMindDB.new(filepath, MaxMindDB::LOW_MEMORY_FILE_READER)