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

stop being clever about Upload storage location #795

Merged
merged 1 commit into from
Apr 1, 2024
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
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
Loading