Skip to content

Commit

Permalink
Fixed content filename on S3
Browse files Browse the repository at this point in the history
  • Loading branch information
mellelieuwes committed Dec 7, 2023
1 parent ab03622 commit a0e9db5
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions core/systems/content/s3.ex
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
defmodule Systems.Content.S3 do
alias ExAws.S3

def store(file, _original_filename) do
def store(file, original_filename) do
bucket = Access.fetch!(s3_settings(), :bucket)
uuid = Ecto.UUID.generate()
extname = Path.extname(file)
id = "#{uuid}#{extname}"
extname = Path.extname(original_filename)
filename = "#{uuid}#{extname}"

upload_file(file, id, bucket)
id
upload_file(file, filename, bucket)
filename
end

def remove(id) do
def remove(filename) do
bucket = Access.fetch!(s3_settings(), :bucket)
object_key = "#{object_key(id)}"
object_key = "#{object_key(filename)}"

S3.delete_object(bucket, object_key)
|> backend().request!()
end

def get_public_url(id) do
def get_public_url(filename) do
settings = s3_settings()
public_url = Access.get(settings, :public_url)
"#{public_url}/#{object_key(id)}"
"#{public_url}/#{object_key(filename)}"
end

defp upload_file(file, id, bucket) do
defp upload_file(file, filename, bucket) do
{:ok, data} = File.read(file)
object_key = "#{object_key(id)}"
object_key = "#{object_key(filename)}"

S3.put_object(
bucket,
object_key,
data,
content_type: content_type(file)
content_type: content_type(filename)
)
|> backend().request!()
end

defp content_type(name), do: MIME.from_path(name)

defp object_key(id) do
defp object_key(filename) do
prefix = Access.get(s3_settings(), :prefix, nil)

[prefix, id]
[prefix, filename]
|> Enum.filter(&(&1 != nil))
|> Enum.join("/")
end
Expand Down

0 comments on commit a0e9db5

Please sign in to comment.