TempFile module to handle random file names
This commit is contained in:
1 parent
937b57b667
commit
fd3a74f6d6
3 files changed
+13
-7
No files matched your search
@@ -123,8 +123,7 @@ defmodule FeedReader.Collector do
|
|||||||
do: {:ok, %ParseResult{feed: feed, items: Enum.reverse(items)}}
|
do: {:ok, %ParseResult{feed: feed, items: Enum.reverse(items)}}
|
||||||
|
|
||||||
defp temporary_path do
|
defp temporary_path do
|
||||||
path =
|
path = FeedReader.TempFile.random_file("feed_reader-parse")
|
||||||
Path.join(System.tmp_dir!(), "feed_reader-parse-#{System.unique_integer([:positive])}.xml")
|
|
||||||
|
|
||||||
case File.open(path, [:write, :binary, :exclusive]) do
|
case File.open(path, [:write, :binary, :exclusive]) do
|
||||||
{:ok, file} ->
|
{:ok, file} ->
|
||||||
|
|||||||
@@ -233,11 +233,7 @@ defmodule FeedReader.Downloader do
|
|||||||
defp open_temp_file(0), do: {:error, :eexist}
|
defp open_temp_file(0), do: {:error, :eexist}
|
||||||
|
|
||||||
defp open_temp_file(attempts) do
|
defp open_temp_file(attempts) do
|
||||||
path =
|
path = FeedReader.TempFile.random_file("feed_reader-download")
|
||||||
Path.join(
|
|
||||||
System.tmp_dir!(),
|
|
||||||
"feed_reader-download-#{System.unique_integer([:positive])}.xml"
|
|
||||||
)
|
|
||||||
|
|
||||||
case File.open(path, [:write, :binary, :exclusive]) do
|
case File.open(path, [:write, :binary, :exclusive]) do
|
||||||
{:ok, file} -> {:ok, path, file}
|
{:ok, file} -> {:ok, path, 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
|
||||||
Reference in new issue
Block a user