Skip to content

Commit

Permalink
Support File.stream!/3 in multiple Elixir versions
Browse files Browse the repository at this point in the history
For UploadFile, this adds backwards compatible support for streaming
file contents to NervesHub via the `File.stream!/3` function which
reordered arguments in Elixir >- 1.16
  • Loading branch information
jjcarstens committed Feb 5, 2024
1 parent b61a8ce commit 13355bc
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/nerves_hub_link/upload_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule NervesHubLink.UploadFile do

:ok = Socket.start_uploading(state.socket_pid, filename)

File.stream!(state.file_path, 1024)
file_stream!(state)
|> Stream.with_index()
|> Stream.each(fn {chunk, index} ->
:ok = Socket.upload_data(state.socket_pid, filename, index, chunk)
Expand All @@ -50,4 +50,10 @@ defmodule NervesHubLink.UploadFile do

{:noreply, state}
end

if Version.match?(System.version(), ">= 1.16.0") do
def file_stream!(state), do: File.stream!(state.file_path, 1024, [])
else
def file_stream!(state), do: File.stream!(state.file_path, [], 1024)
end
end

0 comments on commit 13355bc

Please sign in to comment.