Skip to content

Commit 71ce749

Browse files
authored
Merge pull request #133 from OkinawaNet/storage_behavior
Added StorageBehavior
2 parents 735c731 + 4f1b992 commit 71ce749

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule Waffle.StorageBehavior do
2+
@moduledoc """
3+
Defines the behavior for file storage.
4+
5+
## Callbacks
6+
7+
- `put/3`: Saves a file and returns the file name or an error.
8+
- `url/3`: Generates a URL for accessing a file.
9+
- `delete/3`: Deletes a file and returns the result of the operation.
10+
"""
11+
12+
@callback put(definition :: atom, version :: atom, file_and_scope :: {Waffle.File.t(), any}) ::
13+
{:ok, file_name :: String.t()} | {:error, reason :: any}
14+
15+
@callback url(definition :: atom, version :: atom, file_and_scope :: {Waffle.File.t(), any}) ::
16+
String.t()
17+
18+
@callback delete(definition :: atom, version :: atom, file_and_scope :: {Waffle.File.t(), any}) ::
19+
atom
20+
end

lib/waffle/storage/local.ex

+5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ defmodule Waffle.Storage.Local do
2222
end
2323
"""
2424

25+
@behaviour Waffle.StorageBehavior
26+
2527
alias Waffle.Definition.Versioning
2628

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

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

63+
@impl true
5964
def delete(definition, version, file_and_scope) do
6065
Path.join([
6166
definition.storage_dir_prefix(),

lib/waffle/storage/s3.ex

+5
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ defmodule Waffle.Storage.S3 do
130130
"""
131131
require Logger
132132

133+
@behaviour Waffle.StorageBehavior
134+
133135
alias ExAws.Config
134136
alias ExAws.Request.Url
135137
alias ExAws.S3
@@ -138,6 +140,7 @@ defmodule Waffle.Storage.S3 do
138140

139141
@default_expiry_time 60 * 5
140142

143+
@impl true
141144
def put(definition, version, {file, scope}) do
142145
destination_dir = definition.storage_dir(version, {file, scope})
143146
s3_bucket = s3_bucket(definition, {file, scope})
@@ -152,13 +155,15 @@ defmodule Waffle.Storage.S3 do
152155
do_put(file, {s3_bucket, s3_key, s3_options})
153156
end
154157

158+
@impl true
155159
def url(definition, version, file_and_scope, options \\ []) do
156160
case Keyword.get(options, :signed, false) do
157161
false -> build_url(definition, version, file_and_scope, options)
158162
true -> build_signed_url(definition, version, file_and_scope, options)
159163
end
160164
end
161165

166+
@impl true
162167
def delete(definition, version, {file, scope}) do
163168
s3_bucket(definition, {file, scope})
164169
|> S3.delete_object(s3_key(definition, version, {file, scope}))

0 commit comments

Comments
 (0)