Skip to content

A platform for community discussion. Free, open, simple.

FIX: use tar -xzvf to extract maxmind vs Ruby wrappers

Sam Saffron0f76e3090c9c
diff --git a/lib/discourse_ip_info.rb b/lib/discourse_ip_info.rb
index b67f5cdafe0..0f0ff8e4f5f 100644
--- a/lib/discourse_ip_info.rb
+++ b/lib/discourse_ip_info.rb
@@ -7,7 +7,7 @@ class DiscourseIpInfo
include Singleton
def initialize
- open_db(File.join(Rails.root, 'vendor', 'data'))
+ open_db(DiscourseIpInfo.path)
end
def open_db(path)
@@ -16,38 +16,43 @@ class DiscourseIpInfo
@cache = LruRedux::ThreadSafeCache.new(2000)
end
+ def self.path
+ @path ||= File.join(Rails.root, 'vendor', 'data')
+ end
+
def self.mmdb_path(name)
- File.join(Rails.root, 'vendor', 'data', "#{name}.mmdb")
+ File.join(path, "#{name}.mmdb")
end
def self.mmdb_download(name)
require 'rubygems/package'
require 'zlib'
+ FileUtils.mkdir_p(path)
+
uri = URI("https://geolite.maxmind.com/download/geoip/database/#{name}.tar.gz")
- begin
- tar_gz_file = Tempfile.new
- tar_gz_file.binmode
- tar_gz_file.write(Net::HTTP.get(uri))
- tar_gz_file.close
+ tar_gz_file = Tempfile.new
+ tar_gz_file.binmode
+ tar_gz_file.write(Net::HTTP.get(uri))
+ tar_gz_file.close
- begin
- extractor = Gem::Package::TarReader.new(Zlib::GzipReader.open(tar_gz_file.path))
- extractor.rewind
+ dest = File.join(Dir.tmpdir, "maxmind_#{SecureRandom.hex}")
+ FileUtils.mkdir_p(dest)
- 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
- extractor.close
- end
+ Discourse::Utils.execute_command('tar', '-xzvf', tar_gz_file.path, "-C", dest)
- ensure
- tar_gz_file.close
- tar_gz_file.unlink
+ Dir.glob("#{dest}/**/*.mmdb").each do |path|
+ if path.include?(name)
+ FileUtils.mv(path, mmdb_path(name))
+ else
+ Rails.logger.warn("Skipping unknown mmdb file during ip database update #{path}")
+ end
end
+
+ ensure
+ FileUtils.rm_rf(dest) if dest
+ FileUtils.rm(tar_gz_file) if tar_gz_file
end
def mmdb_load(filepath)