@@ -3,6 +3,8 @@ defmodule Upload do
3
3
An opinionated file uploader.
4
4
"""
5
5
6
+ @ type variant_id :: String . t ( ) | atom ( )
7
+
6
8
alias Upload.Blob
7
9
alias Upload.Stat
8
10
@@ -37,7 +39,16 @@ defmodule Upload do
37
39
end
38
40
end
39
41
40
- @ spec variant_exists? ( Blob . t ( ) , String . t ( ) | atom ( ) ) :: boolean ( )
42
+ @ doc """
43
+ Checks if a variant exists for a given `Upload.Blob` and the variant identifier.
44
+
45
+ ## Example
46
+
47
+ iex> Upload.variant_exists?(person.avatar, :small)
48
+ iex> true
49
+
50
+ """
51
+ @ spec variant_exists? ( Blob . t ( ) , variant_id ( ) ) :: boolean ( )
41
52
def variant_exists? ( % Blob { id: blob_id } , variant ) do
42
53
repo = Upload.Config . repo ( )
43
54
@@ -46,7 +57,17 @@ defmodule Upload do
46
57
|> repo . exists? ( )
47
58
end
48
59
49
- @ spec get_variant ( Blob . t ( ) , String . t ( ) | atom ( ) ) :: nil | Blob . t ( )
60
+ @ doc """
61
+ Returns the variant for a given `Upload.Blob` and the variant identifier or `nil` if it
62
+ does not exist.
63
+
64
+ ## Example
65
+
66
+ iex> Upload.get_variant(person.avatar, :small)
67
+ %Blob{}
68
+
69
+ """
70
+ @ spec get_variant ( Blob . t ( ) , variant_id ( ) ) :: nil | Blob . t ( )
50
71
def get_variant ( % Blob { id: blob_id } , variant ) do
51
72
repo = Upload.Config . repo ( )
52
73
@@ -64,13 +85,13 @@ defmodule Upload do
64
85
65
86
## Example
66
87
67
- ```elixir
68
- create_variant(original_blob, "small", &transform_fn/3)
69
- ```
88
+ iex> create_variant(original_blob, :small, &transform_fn/3)
89
+ {:ok, %Blob{}}
70
90
"""
71
91
@ spec create_variant ( Blob . t ( ) , String . t ( ) , any ( ) ) :: { :ok , Blob . t ( ) } | { :error , any ( ) }
72
92
def create_variant ( original_blob , variant , transform_fn ) when is_function ( transform_fn , 3 ) do
73
93
repo = Upload.Config . repo ( )
94
+ variant = to_string ( variant )
74
95
75
96
{ :ok , multi_result } =
76
97
Ecto.Multi . new ( )
@@ -87,13 +108,14 @@ defmodule Upload do
87
108
88
109
## Example
89
110
90
- ```elixir
91
- create_multiple_variants(blob, ["small", "large"], &transform_fn/3)
92
- ```
111
+ iex> create_multiple_variants(blob, [:small, :large], &transform_fn/3)
112
+ {:ok, [%Blob{...}, %Blob{...}]}
93
113
"""
94
- @ spec create_multiple_variants ( Blob . t ( ) , [ String . t ( ) ] , any ( ) ) :: any ( )
114
+ @ spec create_multiple_variants ( Blob . t ( ) , [ variant_id ( ) ] , any ( ) ) ::
115
+ { :ok , [ Blob . t ( ) ] } | { :error , any ( ) }
95
116
def create_multiple_variants ( original_blob , variants , transform_fn )
96
117
when is_function ( transform_fn , 3 ) do
118
+ variants = Enum . map ( variants , & to_string / 1 )
97
119
repo = Upload.Config . repo ( )
98
120
multi = Ecto.Multi . new ( )
99
121
@@ -116,26 +138,32 @@ defmodule Upload do
116
138
@ doc """
117
139
Set the visiblity of a `Blob` using the struct or it's key.
118
140
119
- This only applies when Upload is configured to use S3.
141
+ > #### Note {: .warning}
142
+ >
143
+ > This only applies when Upload is configured to use S3.
120
144
121
- Supported access control lists for Amazon S3 are:
145
+ Supported canned access control lists for Amazon S3 are:
122
146
123
147
| ACL | Permissions Added to ACL |
124
148
|------------------------------|---------------------------------------------------------------------------------|
125
- | "private" | Owner gets `FULL_CONTROL`. No one else has access rights (default). |
126
- | "public_read" | Owner gets `FULL_CONTROL`. The `AllUsers` group gets READ access. |
127
- | "public_read_write" | Owner gets `FULL_CONTROL`. The `AllUsers` group gets `READ` and `WRITE` access. |
128
- | | Granting this on a bucket is generally not recommended. |
129
- | "authenticated_read" | Owner gets `FULL_CONTROL`. The `AuthenticatedUsers` group gets `READ` access. |
130
- | "bucket_owner_read" | Object owner gets `FULL_CONTROL`. Bucket owner gets `READ` access. |
131
- | "bucket_owner_full_control" | Both the object owner and the bucket owner get `FULL_CONTROL` over the object. |
149
+ | private | Owner gets `FULL_CONTROL`. No one else has access rights (default). |
150
+ | public_read | Owner gets `FULL_CONTROL`. The `AllUsers` group gets READ access. |
151
+ | public_read_write | Owner gets `FULL_CONTROL`. The `AllUsers` group gets `READ` and `WRITE` access. Granting this on a bucket is generally not recommended. |
152
+ | authenticated_read | Owner gets `FULL_CONTROL`. The `AuthenticatedUsers` group gets `READ` access. |
153
+ | bucket_owner_read | Object owner gets `FULL_CONTROL`. Bucket owner gets `READ` access. |
154
+ | bucket_owner_full_control | Both the object owner and the bucket owner get `FULL_CONTROL` over the object. |
155
+
156
+ ## Example
157
+
158
+ iex> Upload.put_access_control_list(person.avatar, :public_read)
159
+ :ok
132
160
"""
133
161
@ spec put_access_control_list ( Blob . t ( ) | Blob . key ( ) , String . t ( ) ) :: :ok | { :error , term ( ) }
134
- def put_access_control_list ( % Blob { key: key } , acl ) do
135
- put_access_control_list ( key , acl )
162
+ def put_access_control_list ( % Blob { key: key } = _blob , canned_acl ) do
163
+ put_access_control_list ( key , canned_acl )
136
164
end
137
165
138
- def put_access_control_list ( key , acl ) do
139
- Upload.Storage . put_access_control_list ( key , acl )
166
+ def put_access_control_list ( key , canned_acl ) do
167
+ Upload.Storage . put_access_control_list ( key , [ { : acl, canned_acl } ] )
140
168
end
141
169
end
0 commit comments