Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made sure to skip directories while upload feldspar zip file to S3 #483

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Changes from all 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
34 changes: 21 additions & 13 deletions core/systems/feldspar/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,38 @@ defmodule Systems.Feldspar.S3 do
end

defp upload_zip_content(zip_file, target) do
settings = s3_settings()

{:ok, zip_handle} = :zip.zip_open(to_charlist(zip_file), [:memory])

upload_file = fn file ->
{:ok, {_, data}} = :zip.zip_get(file, zip_handle)
# Read the zip file names and skip the comment (first item)
{:ok, [_ | contents]} = :zip.zip_list_dir(zip_handle)

contents
|> Enum.map(fn {:zip_file, file, info, _, _, _} -> {file, info} end)
|> Task.async_stream(&upload_file(&1, zip_handle, target, s3_settings()), max_concurrency: 10)
|> Stream.run()
end

defp upload_file({name, info}, zip_handle, target, settings) do
if is_regular_file(info) do
{:ok, {_, data}} = :zip.zip_get(name, zip_handle)

S3.put_object(
Access.fetch!(settings, :bucket),
"#{object_key(target)}/#{file}",
"#{object_key(target)}/#{name}",
data
)
|> backend().request!()
end

# Read the zip file names and skip the comment (first item)
{:ok, [_ | contents]} = :zip.zip_list_dir(zip_handle)

contents
|> Enum.map(fn {:zip_file, file, _, _, _, _} -> file end)
|> Task.async_stream(upload_file, max_concurrency: 10)
|> Stream.run()
end

@doc """
See: https://www.erlang.org/doc/man/file#type-file_info
Files types: device | directory | other | regular | symlink | undefined
Only real files (aka :regular) can/should be uploaded.
"""
def is_regular_file({:file_info, _, :regular, _, _, _, _, _, _, _, _, _, _, _}), do: true
def is_regular_file(_), do: false

defp object_key(id) do
prefix = Access.get(s3_settings(), :prefix, "")
"#{prefix}#{id}"
Expand Down
Loading