diff --git a/lib/waffle/file.ex b/lib/waffle/file.ex index c630628..c06a324 100644 --- a/lib/waffle/file.ex +++ b/lib/waffle/file.ex @@ -132,7 +132,7 @@ defmodule Waffle.File do defp write_binary(file) do path = generate_temporary_path(file) - File.write!(path, file.binary) + write_to_file(path, file.binary) %__MODULE__{ file_name: file.file_name, @@ -158,19 +158,31 @@ defmodule Waffle.File do case remote_file do {:ok, body, filename} -> - case File.write(local_path, body) do + case write_to_file(local_path, body) do :ok -> {:ok, filename} _ -> :error end {:ok, body} -> - File.write(local_path, body) + :ok = write_to_file(local_path, body) + + {:error, _reason} = err -> err end end + #stream binary in chuck into the file. Fixes the write for >2GB files. + defp write_to_file(local_path, body) do + body + |> StringIO.open() + |> elem(1) + |> IO.binstream(1024*1024*10) + |> Stream.into(File.stream!(local_path)) + |> Stream.run + end + # hackney :connect_timeout - timeout used when establishing a connection, in milliseconds # hackney :recv_timeout - timeout used when receiving from a connection, in milliseconds # :backoff_max - maximum backoff time, in milliseconds