-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to stream hls files to a http server
- Loading branch information
Showing
6 changed files
with
76 additions
and
3 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
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,51 @@ | ||
defmodule HTTPServer.Router do | ||
use Plug.Router | ||
|
||
require Logger | ||
|
||
# plug(:opts) | ||
plug(:match) | ||
plug(:dispatch) | ||
|
||
@impl true | ||
def call(conn, opts) do | ||
assign(conn, :directory, opts.directory) | ||
|> super(opts) | ||
end | ||
|
||
post "/hls_output/:filename" do | ||
hls_dir = conn.assigns.directory | ||
|
||
file_path = Path.join(hls_dir, filename) | ||
conn = write_body_to_file(conn, file_path) | ||
send_resp(conn, 200, "File successfully written") | ||
end | ||
|
||
delete "/hls_output/:filename" do | ||
hls_dir = conn.assigns.directory | ||
|
||
case Path.join(hls_dir, filename) |> File.rm() do | ||
:ok -> | ||
send_resp(conn, 200, "File deleted successfully") | ||
|
||
{:error, error} -> | ||
send_resp(conn, 409, "Error deleting file: #{inspect(error)}") | ||
end | ||
end | ||
|
||
match _ do | ||
send_resp(conn, 404, "Not found") | ||
end | ||
|
||
defp write_body_to_file(conn, file_path) do | ||
case read_body(conn) do | ||
{:ok, body, conn} -> | ||
File.write(file_path, body) | ||
conn | ||
|
||
{:more, partial_body, conn} -> | ||
File.write(file_path, partial_body) | ||
write_body_to_file(conn, file_path) | ||
end | ||
end | ||
end |
File renamed without changes.
File renamed without changes.