From a9782230faf758b7e9711098ea53c67d881aaf06 Mon Sep 17 00:00:00 2001 From: Daniel Khaapamyaki Date: Thu, 23 Nov 2023 10:33:59 +0100 Subject: [PATCH] Improve get_from_path/1,2 (#13) --- lib/infer.ex | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/infer.ex b/lib/infer.ex index 62a53be..40786d5 100644 --- a/lib/infer.ex +++ b/lib/infer.ex @@ -42,13 +42,22 @@ defmodule Infer do iex> Infer.get_from_path("test/docs/sample.pptx") %Infer.Type{extension: "pptx", matcher: &Infer.Doc.pptx?/1, matcher_type: :doc, mime_type: "application/vnd.openxmlformats-officedocument.presentationml.presentation"} + iex> Infer.get_from_path("test/docs/unknown.path") + nil + """ @spec get_from_path(binary()) :: Infer.Type.t() | nil def get_from_path(path, byte_size \\ 2048) do - with {:ok, io_device} <- :file.open(path, [:read, :binary]), - {:ok, binary} <- :file.read(io_device, byte_size) do - :file.close(io_device) - Enum.find(@matchers, & &1.matcher.(binary)) + result = File.open(path, [:binary, :read], fn io_device -> + case IO.binread(io_device, byte_size) do + binary when is_binary(binary) -> Enum.find(@matchers, & &1.matcher.(binary)) + _other -> nil + end + end) + + case result do + {:ok, %Infer.Type{} = type} -> type + _other -> nil end end