Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/ex_webrtc_recorder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,14 @@ defmodule ExWebRTC.Recorder do
end)

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

case state.upload_handler do
nil ->
{manifest_diff, nil, state}

handler ->
{ref, handler} = S3.UploadHandler.spawn_task(handler, manifest_diff)
{ref, handler} = S3.UploadHandler.spawn_task(handler, files_to_upload)

{manifest_diff, ref, %{state | upload_handler: handler}}
end
Expand Down
35 changes: 23 additions & 12 deletions lib/ex_webrtc_recorder/s3/upload_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,21 @@ if Code.ensure_loaded?(ExAws.S3) do
def spawn_task(
%__MODULE__{bucket_name: bucket_name, s3_config_overrides: s3_config_overrides} =
handler,
manifest
files_to_upload
) do
s3_paths =
Map.new(manifest, fn {id, %{location: path}} ->
Map.new(files_to_upload, fn {id, %{location: path}} ->
s3_path = path |> Path.basename() |> then(&Path.join(handler.base_path, &1))

{id, s3_path}
end)

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

{id, %{object_data | location: location}}
end)

download_manifest = prepare_download_manifest(files_to_upload, bucket_name, s3_paths)
# FIXME: this links, ideally we should use `async_nolink` instead
# but this may require a slight change of the current UploadHandler logic
task =
Task.Supervisor.async(ExWebRTC.Recorder.TaskSupervisor, fn ->
upload(manifest, bucket_name, s3_paths, s3_config_overrides)
upload(files_to_upload, bucket_name, s3_paths, s3_config_overrides)
end)

{task.ref,
Expand Down Expand Up @@ -95,8 +89,8 @@ if Code.ensure_loaded?(ExAws.S3) do
{result, manifest, %__MODULE__{handler | tasks: tasks}}
end

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

Expand All @@ -118,6 +112,23 @@ if Code.ensure_loaded?(ExAws.S3) do
{id, result}
end)
end

defp prepare_download_manifest(files_to_upload, bucket_name, s3_paths) do
{manifest_file, track_files} = Map.pop(files_to_upload, "manifest_file")

download_manifest =
Map.new(track_files, fn {id, object_data} ->
{:ok, location} = Recorder.S3.Utils.to_url(bucket_name, s3_paths[id])
{id, %{object_data | location: location}}
end)

# Update the local manifest file to contain S3 URLs instead of local paths
if manifest_file do
:ok = File.write!(manifest_file.location, Jason.encode!(download_manifest))
end

download_manifest
end
end
else
defmodule ExWebRTC.Recorder.S3.UploadHandler do
Expand Down
Loading