@@ -102,7 +102,7 @@ defmodule Cloudinex.Uploader do
102102 Uploads file
103103
104104 ```elixir
105- iex> Cloudinex.Uploader.upload_url ("./example.jpg")
105+ iex> Cloudinex.Uploader.upload_file ("./example.jpg", %{"type": "image"} )
106106 {:ok,
107107 %{"bytes" => 228821,
108108 "created_at" => "2017-09-03T20:43:45Z",
@@ -124,27 +124,33 @@ defmodule Cloudinex.Uploader do
124124 [API Docs](http://cloudinary.com/documentation/upload_images#uploading_with_a_direct_call_to_the_api)
125125 """
126126 @ 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+
128130 options
129131 |> generate_upload_keys
130- |> file_upload ( file_path )
132+ |> file_upload ( file_path , type )
131133 end
132134
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 )
141139
142140 mp =
143141 Enum . reduce ( options , mp , fn { key , value } , acc -> Multipart . add_field ( acc , key , value ) end )
144142
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+
145151 client ( )
146- |> post ( "/image/upload" , mp )
147- |> Helpers . handle_json_response ( )
152+ |> post ( url , mp )
153+ |> Helpers . handle_json_response
148154 end
149155
150156 defp generate_upload_keys ( options ) do
0 commit comments