Skip to content

Commit

Permalink
Temporarily use atoms as text-id
Browse files Browse the repository at this point in the history
  • Loading branch information
rtshkmr committed Dec 31, 2023
1 parent 3239319 commit 6dbc7fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
28 changes: 20 additions & 8 deletions lib/vyasa/adapters/og_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ defmodule Vyasa.Adapters.OgAdapter do
Retrieves the actual content from the gita that is alluded to by an opengraph file name.
"""
def get_content(:gita, filename) do
[chapter_num, verse_num] = Regex.scan(~r/\d+/, filename) |> List.flatten()
%{text: text} = Gita.verse(chapter_num, verse_num)

%{"src"=> :gita , "text"=> text}
case Regex.scan(~r/\d+/, filename) |> List.flatten() do
[chapter_num, verse_num] -> # TODO: this could use better guards. e.g. gita-1-000.png will break it
%{text: text} = Gita.verse(chapter_num, verse_num)
%{"src"=> :gita , "text"=> text}
_ -> {:error, "Malformed / unsupported filename #{filename}"}
end
end


Expand All @@ -28,11 +30,10 @@ defmodule Vyasa.Adapters.OgAdapter do
@doc """
Returns a string representing the filename for a particular verse from a particular chapter of
the gita. Currently just supports gita.
TODO: use text-id of some sort to generify the text part.
"""
def encode_filename(src, [chapter_num, verse_num]) when src == :gita do
Integer.to_string(chapter_num) <> "-" <> Integer.to_string(verse_num) <> ".png"
gita_uuid = "gita" # TODO: consider better unique ids based on what we want to support for our corpus.
gita_uuid <> "-" <> Integer.to_string(chapter_num) <> "-" <> Integer.to_string(verse_num) <> ".png"
end


Expand All @@ -44,5 +45,16 @@ defmodule Vyasa.Adapters.OgAdapter do
%{chapter_num => chapter_num, verse_num => verse_num}
end

@supported_src_ids ["gita"]
@doc """
Resolves src id from filename. Currently merely converts it to an existing atom.
"""
def resolve_src_id(filename) do
src_id = List.first(String.split(filename, "-"))
case Enum.member?(@supported_src_ids, src_id) do
true -> {:ok, String.to_existing_atom(src_id)}
_ -> {:error, "Unsupported source"}
end
end

end
end
11 changes: 6 additions & 5 deletions lib/vyasa_web/controllers/og_image_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule VyasaWeb.OgImageController do
if File.exists?(target_url) do
{:ok, target_url}
else
case get_content_for_file(filename) do
case get_og_content(filename) do
%{"src" => :gita, "text" => text} ->
ImageGenerator.generate_opengraph_image!(filename, text)
{:ok, target_url}
Expand All @@ -32,15 +32,16 @@ defmodule VyasaWeb.OgImageController do
end
end

# TODO file name needs to have an identifier for the title of the text for a text based lookup currently defaults just to gita
# TODO also needs unique title id -- perhaps we can just use url params for this for now.
@doc """
Fetches the actual content, by parsing the filename.
For now, this shall only fetch gita-related content. Subsequently, this function may be
updated to support other texts and media formats.
"""
def get_content_for_file(filename) do
OgAdapter.get_content(:gita, filename)
def get_og_content(filename) do
case OgAdapter.resolve_src_id(filename) do
{:ok, :gita} -> OgAdapter.get_content(:gita, filename)
_ -> {:error}
end
end
end

0 comments on commit 6dbc7fb

Please sign in to comment.