Skip to content

Commit

Permalink
Support File upload in Elixir 1.16 and earlier
Browse files Browse the repository at this point in the history
`File.stream!/3` changed in Elixir 1.16 which breaks File uploads.
This changes to use `File.stream!/2` which shoule be backwards
compatible between versions as we don't need all the options available
in `File.stream!/3`
  • Loading branch information
jjcarstens committed Feb 5, 2024
1 parent b63cc0e commit b61a8ce
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/nerves_hub_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ defmodule NervesHubLink do
@doc """
Send a file to the connected console
"""
@spec send_file(Path.t()) :: :ok
@spec send_file(Path.t()) :: :ok | {:error, :too_large | File.posix()}
defdelegate send_file(file_path), to: Socket
end
1 change: 1 addition & 0 deletions lib/nerves_hub_link/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ defmodule NervesHubLink.Socket do
GenServer.call(__MODULE__, {:check_connection, type})
end

@spec send_file(Path.t()) :: :ok | {:error, :too_large | File.posix()}
def send_file(file_path) do
GenServer.call(__MODULE__, {:send_file, file_path})
end
Expand Down
3 changes: 2 additions & 1 deletion lib/nerves_hub_link/upload_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule NervesHubLink.UploadFile do
defstruct [:file_path, :socket_pid]
end

@spec start_link(Path.t(), pid()) :: GenServer.on_start()
def start_link(file_path, socket_pid) do
GenServer.start_link(__MODULE__, file_path: file_path, socket_pid: socket_pid)
end
Expand All @@ -38,7 +39,7 @@ defmodule NervesHubLink.UploadFile do

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

File.stream!(state.file_path, [], 1024)
File.stream!(state.file_path, 1024)
|> Stream.with_index()
|> Stream.each(fn {chunk, index} ->
:ok = Socket.upload_data(state.socket_pid, filename, index, chunk)
Expand Down

0 comments on commit b61a8ce

Please sign in to comment.