Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uploads for files larger than 2GB #105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this number exactly?

Copy link
Author

@sulphur sulphur Oct 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is arbitrary. I'm using 10MB chunks to write to files. When to small it can be long when to high it will use to much memory i figured 10Mb is good compromise today. I don't know what would be best here :)

|> 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