@@ -102,7 +102,7 @@ defmodule Cloudinex.Uploader do
102
102
Uploads file
103
103
104
104
```elixir
105
- iex> Cloudinex.Uploader.upload_url ("./example.jpg")
105
+ iex> Cloudinex.Uploader.upload_file ("./example.jpg", %{"type": "image"} )
106
106
{:ok,
107
107
%{"bytes" => 228821,
108
108
"created_at" => "2017-09-03T20:43:45Z",
@@ -124,27 +124,33 @@ defmodule Cloudinex.Uploader do
124
124
[API Docs](http://cloudinary.com/documentation/upload_images#uploading_with_a_direct_call_to_the_api)
125
125
"""
126
126
@ spec upload_file ( file_path :: String . t ( ) , options :: Map . t ( ) ) :: { atom , Map . t ( ) }
127
- def upload_file ( file_path , options \\ % { } ) do
127
+ def upload_file ( file_path , options \\ % { type: "image" } ) do
128
+ { type , options } = Map . pop ( options , :type )
129
+
128
130
options
129
131
|> generate_upload_keys
130
- |> file_upload ( file_path )
132
+ |> file_upload ( file_path , type )
131
133
end
132
134
133
- defp file_upload (
134
- % { "api_key" => api_key , "signature" => signature , "timestamp" => timestamp } = options ,
135
- file_path
136
- ) do
137
- mp =
138
- Multipart . new ( )
139
- |> Multipart . add_content_type_param ( "application/x-www-form-urlencoded" )
140
- |> Multipart . add_file ( file_path )
135
+ defp file_upload ( % { "api_key" => api_key , "signature" => signature , "timestamp" => timestamp } = options , file_path , type ) do
136
+ mp = Multipart . new
137
+ |> Multipart . add_content_type_param ( "application/x-www-form-urlencoded" )
138
+ |> Multipart . add_file ( file_path )
141
139
142
140
mp =
143
141
Enum . reduce ( options , mp , fn { key , value } , acc -> Multipart . add_field ( acc , key , value ) end )
144
142
143
+ url = case type do
144
+ "image" -> "/image/upload"
145
+ "video" -> "/video/upload"
146
+ _ -> Logger . debug fn ->
147
+ ~s( upload_file called with an unsupported media type. "image" and "video" are the only supported types.)
148
+ end
149
+ end
150
+
145
151
client ( )
146
- |> post ( "/image/upload" , mp )
147
- |> Helpers . handle_json_response ( )
152
+ |> post ( url , mp )
153
+ |> Helpers . handle_json_response
148
154
end
149
155
150
156
defp generate_upload_keys ( options ) do
0 commit comments