diff --git a/lib/philomena/autocomplete/autocomplete.ex b/lib/philomena/autocomplete/autocomplete.ex index ed7dc5fa..6be4dcf2 100644 --- a/lib/philomena/autocomplete/autocomplete.ex +++ b/lib/philomena/autocomplete/autocomplete.ex @@ -2,6 +2,8 @@ defmodule Philomena.Autocomplete.Autocomplete do use Ecto.Schema import Ecto.Changeset + @type t :: %__MODULE__{} + @primary_key false schema "autocomplete" do field :content, :binary diff --git a/lib/philomena/sha512.ex b/lib/philomena/sha512.ex index 1b500440..03c9645e 100644 --- a/lib/philomena/sha512.ex +++ b/lib/philomena/sha512.ex @@ -1,11 +1,27 @@ defmodule Philomena.Sha512 do - @spec file(String.t()) :: String.t() - def file(file) do + @chunk_size 10_485_760 + + @spec file(Path.t()) :: String.t() + def file(path) do hash_ref = :crypto.hash_init(:sha512) - File.stream!(file, [], 10_485_760) + path + |> stream_file() |> Enum.reduce(hash_ref, &:crypto.hash_update(&2, &1)) |> :crypto.hash_final() |> Base.encode16(case: :lower) end + + if Version.match?(System.version(), ">= 1.16.0") do + # `stream!/2` was added in Elixir 1.16 to accept a shortened form, + # where we only need to specify the size of each stream chunk + defp stream_file(file) do + File.stream!(file, @chunk_size) + end + else + # Use legacy stream/3 for older Elixir versions + defp stream_file(file) do + File.stream!(file, [], @chunk_size) + end + end end diff --git a/lib/philomena_web/config.ex b/lib/philomena_web/config.ex index c661550e..7061ef26 100644 --- a/lib/philomena_web/config.ex +++ b/lib/philomena_web/config.ex @@ -1,4 +1,8 @@ defmodule PhilomenaWeb.Config do + # Dialyzer only analyzes beam files directly and cannot see the compile-time variance in + # the associated values, so it flags a false positive here. + @dialyzer [:no_match] + @reload_enabled Application.compile_env(:philomena, :vite_reload, false) @csp_relaxed Application.compile_env(:philomena, :csp_relaxed, false)