Skip to content

Commit

Permalink
fix: file resolving and multi part sending
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC committed Jul 1, 2018
1 parent 39c2149 commit 7b7a1ab
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/rest.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ defmodule Crux.Rest do
do: create_message(channel_id, Map.new(not_map))

def create_message(channel_id, %{files: files} = args) when is_number(channel_id) do
Enum.reduce_while(files, [], fn file ->
Enum.reduce_while(files, [], fn file, acc ->
with {:error, error} <- Util.map_file(file) do
{:halt, error}
else
tuple ->
{:cont, tuple}
{:cont, [tuple | acc]}
end
end)
|> case do
Expand Down
9 changes: 4 additions & 5 deletions lib/rest/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Crux.Rest.Util do
{:ok, response.body}
end

String.valid?(file) ->
File.exists?(file) && File.stat!(file).type == :regular ->
File.read(file)

is_binary(file) ->
Expand Down Expand Up @@ -63,7 +63,7 @@ defmodule Crux.Rest.Util do
cond do
Regex.match?(~r{^https?://}, bin_or_path) ->
with {:ok, %{body: file}} <- HTTPoison.get(bin_or_path) do
map_file({Path.basename(name, file), Path.basename(name)})
map_file({file, Path.basename(name)})
else
{:error, inner} ->
{:error, inner}
Expand All @@ -72,8 +72,7 @@ defmodule Crux.Rest.Util do
{:error, other}
end

# Not sure whether this is actually a good idea
String.valid?(bin_or_path) ->
File.exists?(bin_or_path) && File.stat!(bin_or_path).type == :regular ->
map_file({:file, bin_or_path, Path.basename(name)})

true ->
Expand All @@ -82,7 +81,7 @@ defmodule Crux.Rest.Util do
end

def map_file({name_or_atom, bin_or_path, name}) do
disposition = {"form-data", [{"filename", "\"#{name}\""}]}
disposition = {"form-data", [{"filename", "\"#{name}\""}, {"name", "\"#{name}\""}]}
headers = [{"content-type", :mimerl.filename(name)}]

{name_or_atom, bin_or_path, disposition, headers}
Expand Down

0 comments on commit 7b7a1ab

Please sign in to comment.