From d0630ea6eee88b0931ca0458940ddcd653af8159 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Sun, 5 Jan 2020 22:08:13 +1100 Subject: [PATCH] FIX: MaxMind DB file not downloading correctly Previously we had the ability to download a simple .gz file new changes mean we have a a tar.gz file that needs some levels of fiddling to get extracted correctly --- lib/discourse_ip_info.rb | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/discourse_ip_info.rb b/lib/discourse_ip_info.rb index 50c5d3854f..6aa0f16505 100644 --- a/lib/discourse_ip_info.rb +++ b/lib/discourse_ip_info.rb @@ -44,11 +44,33 @@ class DiscourseIpInfo follow_redirect: false ) - Discourse::Utils.execute_command("gunzip", gz_file.path) + filename = File.basename(gz_file.path) + + dir = "#{Dir.tmpdir}/#{SecureRandom.hex}" + + Discourse::Utils.execute_command( + "mkdir", "-p", dir + ) + + Discourse::Utils.execute_command( + "cp", + gz_file.path, + "#{dir}/#{filename}" + ) + + Discourse::Utils.execute_command( + "tar", + "-xzvf", + "#{dir}/#{filename}", + chdir: dir + ) + + Dir["#{dir}/**/*.mmdb"].each do |f| + FileUtils.mv(f, mmdb_path(name)) + end - path = gz_file.path.sub(/\.gz\z/, "") - FileUtils.mv(path, mmdb_path(name)) ensure + FileUtils.rm_r(dir, force: true) if dir gz_file&.close! end