From 4afead9b6da02627c4e0dc79dae82fa2189c6405 Mon Sep 17 00:00:00 2001 From: sulphur Date: Tue, 14 Sep 2021 11:15:12 +0200 Subject: [PATCH] use stream for writting to files --- lib/waffle/file.ex | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/waffle/file.ex b/lib/waffle/file.ex index edecca8..9af43d5 100644 --- a/lib/waffle/file.ex +++ b/lib/waffle/file.ex @@ -125,7 +125,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, @@ -151,19 +151,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, error} -> {:error, error} 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