Skip to content

Commit e3c6e4e

Browse files
committed
Apply comment suggestion
1 parent b9f761f commit e3c6e4e

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

lib/ex_webrtc_recorder.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,13 @@ defmodule ExWebRTC.Recorder do
320320
end)
321321

322322
manifest_diff = to_manifest(state.track_data, track_ids)
323-
files_to_upload = Map.put(manifest_diff, "manifest_file", %{location: state.manifest_path})
324323

325324
case state.upload_handler do
326325
nil ->
327326
{manifest_diff, nil, state}
328327

329328
handler ->
330-
{ref, handler} = S3.UploadHandler.spawn_task(handler, files_to_upload)
329+
{ref, handler} = S3.UploadHandler.spawn_task(handler, manifest_diff)
331330

332331
{manifest_diff, ref, %{state | upload_handler: handler}}
333332
end

lib/ex_webrtc_recorder/s3/upload_handler.ex

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,37 @@ if Code.ensure_loaded?(ExAws.S3) do
4040
def spawn_task(
4141
%__MODULE__{bucket_name: bucket_name, s3_config_overrides: s3_config_overrides} =
4242
handler,
43-
files_to_upload
43+
manifest
4444
) do
4545
s3_paths =
46-
Map.new(files_to_upload, fn {id, %{location: path}} ->
46+
Map.new(manifest, fn {id, %{location: path}} ->
4747
s3_path = path |> Path.basename() |> then(&Path.join(handler.base_path, &1))
4848

4949
{id, s3_path}
5050
end)
5151

5252
download_manifest =
53-
Map.new(files_to_upload, fn {id, object_data} ->
53+
Map.new(manifest, fn {id, object_data} ->
5454
{:ok, location} = Recorder.S3.Utils.to_url(bucket_name, s3_paths[id])
5555

5656
{id, %{object_data | location: location}}
5757
end)
5858

59+
manifest_s3_path =
60+
Path.join(handler.base_path, "manifest.json")
61+
62+
Recorder.S3.Utils.upload_manifest(
63+
download_manifest,
64+
bucket_name,
65+
manifest_s3_path,
66+
s3_config_overrides
67+
)
68+
5969
# FIXME: this links, ideally we should use `async_nolink` instead
6070
# but this may require a slight change of the current UploadHandler logic
6171
task =
6272
Task.Supervisor.async(ExWebRTC.Recorder.TaskSupervisor, fn ->
63-
upload(files_to_upload, bucket_name, s3_paths, s3_config_overrides)
73+
upload(manifest, bucket_name, s3_paths, s3_config_overrides)
6474
end)
6575

6676
{task.ref,
@@ -95,8 +105,8 @@ if Code.ensure_loaded?(ExAws.S3) do
95105
{result, manifest, %__MODULE__{handler | tasks: tasks}}
96106
end
97107

98-
defp upload(files_to_upload, bucket_name, s3_paths, s3_config_overrides) do
99-
Map.new(files_to_upload, fn {id, %{location: path}} ->
108+
defp upload(manifest, bucket_name, s3_paths, s3_config_overrides) do
109+
Map.new(manifest, fn {id, %{location: path}} ->
100110
%{^id => s3_path} = s3_paths
101111
Logger.debug("Uploading `#{path}` to bucket `#{bucket_name}`, path `#{s3_path}`")
102112

lib/ex_webrtc_recorder/s3/utils.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ if Code.ensure_loaded?(ExAws.S3) do
1010
|> ExAws.request(s3_config)
1111
end
1212

13+
@chunk_size 5 * 1024 * 1024
14+
15+
@spec upload_manifest(Manifest.t(), String.t(), String.t(), keyword()) ::
16+
{:ok | :error, term()}
17+
def upload_manifest(manifest, s3_bucket_name, s3_path, s3_config \\ []) do
18+
manifest
19+
|> Jason.encode!()
20+
|> ExWebRTC.Utils.chunk(@chunk_size)
21+
|> ExAws.S3.upload(s3_bucket_name, s3_path)
22+
|> ExAws.request(s3_config)
23+
end
24+
1325
@spec fetch_file(String.t(), String.t(), Path.t(), keyword()) :: {:ok | :error, term()}
1426
def fetch_file(s3_bucket_name, s3_path, output_path, s3_config \\ []) do
1527
ExAws.S3.download_file(s3_bucket_name, s3_path, output_path)
@@ -67,6 +79,7 @@ else
6779
@moduledoc false
6880

6981
def upload_file(_, _, _, _ \\ nil), do: error()
82+
def upload_manifest(_, _, _, _ \\ nil), do: error()
7083
def fetch_file(_, _, _, _ \\ nil), do: error()
7184
def to_url(_, _), do: error()
7285
def parse_url(_), do: error()

0 commit comments

Comments
 (0)