From 0f4bebe2bd287aa02101c033036da43152afb534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20St=C4=99pie=C5=84?= Date: Tue, 25 Feb 2020 09:16:22 +0100 Subject: [PATCH] reader: unzip and untar data without checking the filename MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since filename can be also an URL, it's more wise to make decision based on data and not the filename. This commit will try to unzip and untar the data, and if it fails it will pass the data down the pipe. Signed-off-by: Sławomir Stępień --- lib/mmdb2/reader.ex | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/mmdb2/reader.ex b/lib/mmdb2/reader.ex index b5b6d7d..d4d797a 100644 --- a/lib/mmdb2/reader.ex +++ b/lib/mmdb2/reader.ex @@ -12,8 +12,8 @@ defmodule Geolix.Adapter.MMDB2.Reader do {:ok, {{_, 200, _}, _, body}} -> body |> IO.iodata_to_binary() - |> maybe_gunzip(filename) - |> maybe_untar(filename) + |> maybe_gunzip() + |> maybe_untar() |> MMDB2Decoder.parse_database() {:ok, {{_, status, _}, _, _}} -> @@ -29,8 +29,8 @@ defmodule Geolix.Adapter.MMDB2.Reader do {:ok, _} -> filename |> File.read!() - |> maybe_gunzip(filename) - |> maybe_untar(filename) + |> maybe_gunzip() + |> maybe_untar() |> MMDB2Decoder.parse_database() error -> @@ -48,21 +48,18 @@ defmodule Geolix.Adapter.MMDB2.Reader do end end - defp maybe_untar(data, filename) do - if String.ends_with?(filename, [".tar", ".tar.gz"]) do - {:ok, files} = :erl_tar.extract({:binary, data}, [:memory]) - - find_mmdb_contents(files) - else - data + defp maybe_untar(data) do + case :erl_tar.extract({:binary, data}, [:memory]) do + {:ok, files} -> find_mmdb_contents(files) + {:error, _} -> data end end - defp maybe_gunzip(data, filename) do - if String.ends_with?(filename, ".gz") do + defp maybe_gunzip(data) do + try do :zlib.gunzip(data) - else - data + rescue + _ -> data end end end