Fix Dialyzer (#265)

* Fix post-Elixir 1.16 divergence in File.stream!

* Add exclusion for PhilomenaWeb.Config compile-time variance

* Add missing Autocomplete.t
This commit is contained in:
liamwhite 2024-06-01 23:35:19 -04:00 committed by GitHub
parent 9d20b9c465
commit 80f9fa9352
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -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)