Skip to content

Commit

Permalink
Added StorageBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Яков Сергеев committed Nov 27, 2024
1 parent 735c731 commit 281a0ed
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/waffle/behaviors/storage_behaviour.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Waffle.StorageBehavior do
@callback put(definition :: atom, version :: atom, file_and_scope :: {Waffle.File.t(), term}) :: {:ok, file_name :: String.t()} | {:error, reason :: term}

@callback url(definition :: atom, version :: atom, file_and_scope :: {Waffle.File.t(), term}) :: String.t()

@callback delete(definition :: atom, version :: atom, file_and_scope :: {Waffle.File.t(), term}) :: atom
end
5 changes: 5 additions & 0 deletions lib/waffle/storage/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ defmodule Waffle.Storage.Local do
end
"""

@behaviour Waffle.StorageBehavior

alias Waffle.Definition.Versioning

@impl true
def put(definition, version, {file, scope}) do
destination_path = Path.join([
definition.storage_dir_prefix(),
Expand All @@ -41,6 +44,7 @@ defmodule Waffle.Storage.Local do
{:ok, file.file_name}
end

@impl true
def url(definition, version, file_and_scope, _options \\ []) do
local_path = Path.join([
definition.storage_dir(version, file_and_scope),
Expand All @@ -56,6 +60,7 @@ defmodule Waffle.Storage.Local do
|> URI.encode()
end

@impl true
def delete(definition, version, file_and_scope) do
Path.join([
definition.storage_dir_prefix(),
Expand Down
5 changes: 5 additions & 0 deletions lib/waffle/storage/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ defmodule Waffle.Storage.S3 do
"""
require Logger

@behaviour Waffle.StorageBehavior

alias ExAws.Config
alias ExAws.Request.Url
alias ExAws.S3
Expand All @@ -138,6 +140,7 @@ defmodule Waffle.Storage.S3 do

@default_expiry_time 60 * 5

@impl true
def put(definition, version, {file, scope}) do
destination_dir = definition.storage_dir(version, {file, scope})
s3_bucket = s3_bucket(definition, {file, scope})
Expand All @@ -152,13 +155,15 @@ defmodule Waffle.Storage.S3 do
do_put(file, {s3_bucket, s3_key, s3_options})
end

@impl true
def url(definition, version, file_and_scope, options \\ []) do
case Keyword.get(options, :signed, false) do
false -> build_url(definition, version, file_and_scope, options)
true -> build_signed_url(definition, version, file_and_scope, options)
end
end

@impl true
def delete(definition, version, {file, scope}) do
s3_bucket(definition, {file, scope})
|> S3.delete_object(s3_key(definition, version, {file, scope}))
Expand Down

0 comments on commit 281a0ed

Please sign in to comment.