-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stream live upload chunks directly to the target file system (#2111)
- Loading branch information
1 parent
f361eaa
commit 1980ddc
Showing
8 changed files
with
152 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
defmodule LivebookWeb.FileSystemWriter do | ||
@moduledoc false | ||
|
||
# Custom writer for live uploads, uploading directly using the | ||
# `Livebook.FileSystem` abstraction. | ||
# | ||
# ## Options | ||
# | ||
# * `:file` (required) - `%Livebook.FileSystem.File{}` to upload | ||
# the contents to | ||
# | ||
|
||
@behaviour Phoenix.LiveView.UploadWriter | ||
|
||
@impl true | ||
def init(opts) do | ||
file = Keyword.fetch!(opts, :file) | ||
|
||
%{file_system: file_system, path: path} = file | ||
|
||
with {:ok, write_state} <- Livebook.FileSystem.write_stream_init(file_system, path, []) do | ||
{:ok, %{file: file, write_state: write_state}} | ||
end | ||
end | ||
|
||
@impl true | ||
def meta(state) do | ||
%{file: state.file} | ||
end | ||
|
||
@impl true | ||
def write_chunk(chunk, state) do | ||
case Livebook.FileSystem.write_stream_chunk(state.file.file_system, state.write_state, chunk) do | ||
{:ok, write_state} -> {:ok, %{state | write_state: write_state}} | ||
{:error, message} -> {:error, message, state} | ||
end | ||
end | ||
|
||
@impl true | ||
def close(state, :done) do | ||
case Livebook.FileSystem.write_stream_finish(state.file.file_system, state.write_state) do | ||
:ok -> {:ok, state} | ||
{:error, message} -> {:error, message} | ||
end | ||
end | ||
|
||
def close(state, _reason) do | ||
case Livebook.FileSystem.write_stream_halt(state.file.file_system, state.write_state) do | ||
:ok -> {:ok, state} | ||
{:error, message} -> {:error, message} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters