Skip to content

Commit

Permalink
fix(server) - upload media handling http error
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkbln committed Sep 4, 2024
1 parent 925830e commit 9857d06
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
30 changes: 22 additions & 8 deletions server/lib/publisher/wordpress/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,35 @@ defmodule Publisher.WordPress.API do
## Example
req = API.new(headers)
req = API.new(headers, http1)
{:ok, response} = Req.get(req, url: "podlove/v2/episodes")
"""
def new(headers) do
def new(headers, http1) do
user = get_header_value(headers, "wordpress-user")
password = get_header_value(headers, "wordpress-password")
site = get_header_value(headers, "wordpress-site")

Req.new(
base_url: site <> "/wp-json/",
headers: [{"Content-Type", "application/json"}],
auth: {:basic, user <> ":" <> password},
connect_options: [transport_opts: [verify: :verify_none]]
)
if (http1) do
Req.new(
base_url: site <> "/wp-json/",
headers: [{"Content-Type", "application/json"}],
auth: {:basic, user <> ":" <> password},
connect_options: [
transport_opts: [verify: :verify_none]
]
)
else
Req.new(
base_url: site <> "/wp-json/",
headers: [{"Content-Type", "application/json"}],
auth: {:basic, user <> ":" <> password},
connect_options: [
protocols: [:http1],
transport_opts: [verify: :verify_none]
]
)
end
end

defp get_header_value(headers, header_item) do
Expand Down
2 changes: 1 addition & 1 deletion server/lib/publisher/wordpress/episode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Publisher.WordPress.Episode do
alias Publisher.WordPress.Media

def save(conn, params) do
req = API.new(conn.req_headers)
req = API.new(conn.req_headers, true)

with episode_id <- find_or_create_episode(req, params["guid"]),
post_id <- fetch_post_id(req, episode_id),
Expand Down
8 changes: 4 additions & 4 deletions server/lib/publisher/wordpress/podcast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Publisher.WordPress.Podcast do
|> reject_empty_values()
|> Enum.into(%{})

req = API.new(headers)
req = API.new(headers, false)

with {:ok, response} <- Req.post(req, url: "podlove/v2/onboarding/setup", json: payload),
{:ok, _} <- extract_status(response) do
Expand All @@ -58,7 +58,7 @@ defmodule Publisher.WordPress.Podcast do
explicit: body.explicit
}

req = API.new(headers)
req = API.new(headers, false)

with {:ok, response} <- Req.post(req, url: "podlove/v2/podcast", json: payload),
{:ok, _} <- extract_status(response) do
Expand All @@ -76,7 +76,7 @@ defmodule Publisher.WordPress.Podcast do
# Logger.log(:info, "user: #{user}, endpoint: #{site}/wp-json/wp/v2/media")
Logger.log(:info, "body { name: #{image_name}, type: #{image_type} }")

req = API.new(headers)
req = API.new(headers, true)

with {:ok, source_url} <- Media.upload_image(req, base64_image, image_name, image_type),
{:ok, info} <- save_podcast_image_url(req, source_url) do
Expand All @@ -93,7 +93,7 @@ defmodule Publisher.WordPress.Podcast do
# Logger.log(:info, "user: #{user}, endpoint: #{site}/wp-json/wp/v2/media")
Logger.log(:info, "body { name: #{image_name}, url: #{image_url} }")

req = API.new(headers)
req = API.new(headers, true)

with {:ok, source_url} <- Media.upload_media_from_url(req, image_url, image_name),
{:ok, info} <- save_podcast_image_url(req, source_url) do
Expand Down

0 comments on commit 9857d06

Please sign in to comment.