TempFile module to handle random file names

This commit is contained in:
mmacia committed 2026-08-26 21:44:56 +00:00
1 parent 937b57b667
commit fd3a74f6d6
3 files changed
+13 -7

No files matched your search

+1 -2
View File
@@ -123,8 +123,7 @@ defmodule FeedReader.Collector do
do: {:ok, %ParseResult{feed: feed, items: Enum.reverse(items)}}
defp temporary_path do
path =
Path.join(System.tmp_dir!(), "feed_reader-parse-#{System.unique_integer([:positive])}.xml")
path = FeedReader.TempFile.random_file("feed_reader-parse")
case File.open(path, [:write, :binary, :exclusive]) do
{:ok, file} ->
+1 -5
View File
@@ -233,11 +233,7 @@ defmodule FeedReader.Downloader do
defp open_temp_file(0), do: {:error, :eexist}
defp open_temp_file(attempts) do
path =
Path.join(
System.tmp_dir!(),
"feed_reader-download-#{System.unique_integer([:positive])}.xml"
)
path = FeedReader.TempFile.random_file("feed_reader-download")
case File.open(path, [:write, :binary, :exclusive]) do
{:ok, file} -> {:ok, path, file}
+11
View File
@@ -0,0 +1,11 @@
defmodule FeedReader.TempFile do
@moduledoc false
@spec random_file(String.t()) :: String.t()
def random_file(base_name \\ "feed_reader") do
filename =
"#{base_name}-#{:crypto.strong_rand_bytes(16) |> Base.encode16(case: :lower)}.xml"
Path.join(System.tmp_dir!(), filename)
end
end