Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/waffle/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down