Skip to content

Commit 7b6777a

Browse files
committed
Support videos
1 parent 438b62b commit 7b6777a

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

lib/cloudinex/uploader.ex

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/cloudinex/uploader_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,17 @@ defmodule Cloudinex.UploaderTest do
9696

9797
Cloudinex.Uploader.upload_file("./test/fixtures/logo.png")
9898
end
99+
100+
test "uploads video", %{bypass: bypass} do
101+
response = load_fixture("folders")
102+
Bypass.expect bypass, fn conn ->
103+
assert "/demo/video/upload" == conn.request_path
104+
assert "POST" == conn.method
105+
{:ok, _, conn} = Plug.Conn.read_body(conn, length: 1_000_000)
106+
107+
conn
108+
|> Plug.Conn.resp(200, response)
109+
end
110+
Cloudinex.Uploader.upload_file("./test/fixtures/logo.png", %{"type": "video"})
111+
end
99112
end

0 commit comments

Comments
 (0)