mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
oembed api
This commit is contained in:
parent
a9c276ae2f
commit
50880ba888
3 changed files with 74 additions and 1 deletions
72
lib/philomena_web/controllers/api/json/oembed_controller.ex
Normal file
72
lib/philomena_web/controllers/api/json/oembed_controller.ex
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
defmodule PhilomenaWeb.Api.Json.OembedController do
|
||||||
|
use PhilomenaWeb, :controller
|
||||||
|
|
||||||
|
alias Philomena.Images.Image
|
||||||
|
alias Philomena.Repo
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
@cdn_regex ~r/\/img\/.*\/(\d+)(\.|[\/_][_\w])/
|
||||||
|
@img_regex ~r/\/(\d+)/
|
||||||
|
|
||||||
|
def index(conn, %{"url" => url}) do
|
||||||
|
parsed = URI.parse(url)
|
||||||
|
|
||||||
|
try_oembed(conn, parsed)
|
||||||
|
end
|
||||||
|
def index(conn, _params), do: oembed_error(conn)
|
||||||
|
|
||||||
|
defp try_oembed(conn, %{path: path}) do
|
||||||
|
cdn = Regex.run(@cdn_regex, path, capture: :all_but_first)
|
||||||
|
img = Regex.run(@img_regex, path, capture: :all_but_first)
|
||||||
|
|
||||||
|
image_id =
|
||||||
|
cond do
|
||||||
|
cdn -> hd(cdn)
|
||||||
|
img -> hd(img)
|
||||||
|
true -> nil
|
||||||
|
end
|
||||||
|
|
||||||
|
load_image(image_id)
|
||||||
|
|> oembed_image(conn)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp load_image(nil), do: nil
|
||||||
|
defp load_image(id) do
|
||||||
|
Image
|
||||||
|
|> where(id: ^id, hidden_from_users: false)
|
||||||
|
|> preload([:tags, :user])
|
||||||
|
|> Repo.one()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp oembed_image(nil, conn), do: oembed_error(conn)
|
||||||
|
defp oembed_image(image, conn), do: json(conn, oembed_json(image, conn))
|
||||||
|
|
||||||
|
defp oembed_error(conn) do
|
||||||
|
conn
|
||||||
|
|> Plug.Conn.put_status(:not_found)
|
||||||
|
|> json(%{error: "couldn't find an image"})
|
||||||
|
end
|
||||||
|
|
||||||
|
defp oembed_json(image, conn) do
|
||||||
|
%{
|
||||||
|
version: "1.0",
|
||||||
|
type: "photo",
|
||||||
|
title: "##{image.id} - #{image.tag_list_cache} - Derpibooru",
|
||||||
|
author_url: image.source_url,
|
||||||
|
author_name: artist_tags(image.tags),
|
||||||
|
provider_name: "Derpibooru",
|
||||||
|
provider_url: Routes.image_url(conn, :show, image),
|
||||||
|
cache_age: 7200,
|
||||||
|
derpibooru_id: image.id,
|
||||||
|
derpibooru_score: image.score,
|
||||||
|
derpibooru_comments: image.comments_count,
|
||||||
|
derpibooru_tags: Enum.map(image.tags, & &1.name)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp artist_tags(tags) do
|
||||||
|
tags
|
||||||
|
|> Enum.filter(& &1.namespace == "artist")
|
||||||
|
|> Enum.map_join(", ", & &1.name)
|
||||||
|
end
|
||||||
|
end
|
|
@ -9,7 +9,7 @@ defmodule PhilomenaWeb.FilterIdPlug do
|
||||||
filter = load_filter(conn.params)
|
filter = load_filter(conn.params)
|
||||||
user = conn.assigns.current_user
|
user = conn.assigns.current_user
|
||||||
|
|
||||||
case Canada.Can.can?(user, :show, filter) do
|
case not is_nil(filter) and Canada.Can.can?(user, :show, filter) do
|
||||||
true -> Plug.Conn.assign(conn, :current_filter, filter)
|
true -> Plug.Conn.assign(conn, :current_filter, filter)
|
||||||
false -> conn
|
false -> conn
|
||||||
end
|
end
|
||||||
|
|
|
@ -80,6 +80,7 @@ defmodule PhilomenaWeb.Router do
|
||||||
resources "/reverse", ReverseController, only: [:create]
|
resources "/reverse", ReverseController, only: [:create]
|
||||||
end
|
end
|
||||||
resources "/search", SearchController, only: [:index]
|
resources "/search", SearchController, only: [:index]
|
||||||
|
resources "/oembed", OembedController, only: [:index]
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/", PhilomenaWeb do
|
scope "/", PhilomenaWeb do
|
||||||
|
|
Loading…
Reference in a new issue