diff --git a/lib/waffle/definition/storage.ex b/lib/waffle/definition/storage.ex index eac884a..f0da496 100644 --- a/lib/waffle/definition/storage.ex +++ b/lib/waffle/definition/storage.ex @@ -107,6 +107,19 @@ defmodule Waffle.Definition.Storage do This code would authenticate request only for specific domain. Otherwise, it would send empty request headers. + ## Temporary Directory + + When processing files, Waffle creates temporary files for operations like downloading + remote files or applying transformations. By default, these files are stored in the + system's temporary directory (as returned by `System.tmp_dir/0`). + + You can customize this location by setting the `:tmp_dir` configuration option: + + config :waffle, + tmp_dir: "/path/to/custom/tmp" + + Waffle may fail to remove temporary files if the process using them crashes. + """ defmacro __using__(_) do quote do diff --git a/lib/waffle/file.ex b/lib/waffle/file.ex index fb429ef..eccb0e9 100644 --- a/lib/waffle/file.ex +++ b/lib/waffle/file.ex @@ -134,7 +134,14 @@ defmodule Waffle.File do |> Base.encode32() |> Kernel.<>(string_extension) - Path.join(System.tmp_dir(), file_name) + Path.join(tmp_dir(), file_name) + end + + defp tmp_dir do + case Application.get_env(:waffle, :tmp_dir) do + nil -> System.tmp_dir() + value -> value + end end defp write_binary(file) do diff --git a/test/file_test.exs b/test/file_test.exs new file mode 100644 index 0000000..b0194a2 --- /dev/null +++ b/test/file_test.exs @@ -0,0 +1,22 @@ +defmodule WaffleTest.File do + use ExUnit.Case, async: false + + @custom_tmp_dir System.tmp_dir() <> "/waffle_test_custom" + + describe "generate_temporary_path/1" do + test "uses configured tmp_dir" do + File.mkdir_p!(@custom_tmp_dir) + Application.put_env(:waffle, :tmp_dir, @custom_tmp_dir) + + assert Waffle.File.generate_temporary_path() |> String.starts_with?(@custom_tmp_dir) + on_exit fn -> + Application.delete_env(:waffle, :tmp_dir) + File.rm_rf!(@custom_tmp_dir) + end + end + + test "uses system tmp_dir" do + assert Waffle.File.generate_temporary_path() |> String.starts_with?(System.tmp_dir()) + end + end +end