Skip to content

Commit

Permalink
stop being clever about Upload storage location (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat authored Apr 1, 2024
1 parent b97a450 commit ac75b77
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions lib/banchan/uploads/uploads.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,17 @@ defmodule Banchan.Uploads do
"""
def get_data!(upload) do
local = Path.join([local_upload_dir(), upload.bucket, upload.key])

if upload.pending do
raise "Tried to get data for a pending upload"
end

if File.exists?(local) do
File.read!(local)
else
if use_s3?() do
ExAws.S3.get_object(upload.bucket, upload.key)
|> ExAws.request!()
|> Map.get(:body)
else
local = Path.join([local_upload_dir(), upload.bucket, upload.key])
File.read!(local)
end
end

Expand All @@ -123,17 +122,16 @@ defmodule Banchan.Uploads do
"""
def stream_data!(upload) do
local = Path.join([local_upload_dir(), upload.bucket, upload.key])

if upload.pending do
raise "Tried to get data for a pending upload"
end

if File.exists?(local) do
File.stream!(local, [], 32_768)
else
if use_s3?() do
ExAws.S3.download_file(upload.bucket, upload.key, :memory)
|> ExAws.stream!()
else
local = Path.join([local_upload_dir(), upload.bucket, upload.key])
File.stream!(local, [], 32_768)
end
end

Expand All @@ -152,11 +150,11 @@ defmodule Banchan.Uploads do
raise "Tried to get data for a pending upload"
end

if File.exists?(local) do
File.cp!(local, dest)
else
if use_s3?() do
ExAws.S3.download_file(upload.bucket, upload.key, dest)
|> ExAws.request!()
else
File.cp!(local, dest)
end
end

Expand Down Expand Up @@ -237,8 +235,7 @@ defmodule Banchan.Uploads do
Uploads data from `src` to Upload's storage. Does not otherwise modify the Upload.
"""
def upload_file!(%Upload{} = upload, src) do
if Application.fetch_env!(:banchan, :env) == :prod ||
!is_nil(Application.get_env(:ex_aws, :region)) do
if use_s3?() do
src
|> ExAws.S3.Upload.stream_file()
|> ExAws.S3.upload(upload.bucket, upload.key)
Expand Down Expand Up @@ -296,8 +293,7 @@ defmodule Banchan.Uploads do
end

defp copy_file!(%Upload{} = upload, dest_bucket, dest_key) do
if Application.fetch_env!(:banchan, :env) == :prod ||
!is_nil(Application.get_env(:ex_aws, :region)) do
if use_s3?() do
ExAws.S3.put_object_copy(dest_bucket, dest_key, upload.bucket, upload.key)
|> ExAws.request!()
else
Expand All @@ -310,6 +306,11 @@ defmodule Banchan.Uploads do
:ok
end

defp use_s3? do
Application.fetch_env!(:banchan, :env) == :prod ||
!is_nil(Application.get_env(:ex_aws, :region))
end

## Editing

@doc """
Expand All @@ -334,8 +335,7 @@ defmodule Banchan.Uploads do
Deletes an upload's stored data.
"""
def delete_data!(%Upload{} = upload) do
if Application.fetch_env!(:banchan, :env) == :prod ||
!is_nil(Application.get_env(:ex_aws, :region)) do
if use_s3?() do
ExAws.S3.delete_object(upload.bucket, upload.key) |> ExAws.request!()
else
local = Path.join([local_upload_dir(), upload.bucket, upload.key])
Expand Down

0 comments on commit ac75b77

Please sign in to comment.