From 13355bcd2649ef461a5f3294d152d6fb5ecbcfd8 Mon Sep 17 00:00:00 2001 From: Jon Carstens Date: Mon, 5 Feb 2024 08:46:35 -0700 Subject: [PATCH] Support `File.stream!/3` in multiple Elixir versions 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 --- lib/nerves_hub_link/upload_file.ex | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/nerves_hub_link/upload_file.ex b/lib/nerves_hub_link/upload_file.ex index 4d33788..2617a55 100644 --- a/lib/nerves_hub_link/upload_file.ex +++ b/lib/nerves_hub_link/upload_file.ex @@ -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) @@ -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