Store item content MIME type

This commit is contained in:
Shadowfacts 2020-07-18 22:18:41 -04:00
parent 198ca0345b
commit 26bfb2e58f
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 37 additions and 9 deletions

View File

@ -141,6 +141,10 @@ label.sidebar-toggle > .oi {
display: inline-block; display: inline-block;
} }
.post-content img { .item-content img {
max-width: 100%; max-width: 100%;
} }
.item-content > .raw-content {
white-space: pre-wrap;
}

View File

@ -45,6 +45,7 @@ defmodule Frenzy.Item do
schema "items" do schema "items" do
field :content, :string field :content, :string
field :content_type, :string
field :date, :utc_datetime field :date, :utc_datetime
field :creator, :string field :creator, :string
field :guid, :string field :guid, :string
@ -63,6 +64,7 @@ defmodule Frenzy.Item do
__meta__: Ecto.Schema.Metadata.t(), __meta__: Ecto.Schema.Metadata.t(),
id: integer() | nil, id: integer() | nil,
content: String.t(), content: String.t(),
content_type: String.t() | nil,
date: DateTime.t(), date: DateTime.t(),
creator: String.t(), creator: String.t(),
guid: String.t(), guid: String.t(),

View File

@ -11,8 +11,8 @@ defmodule Frenzy.Pipeline.GeminiScrapeStage do
Logger.warn("Unable to get Gemini content for #{url}: #{reason}") Logger.warn("Unable to get Gemini content for #{url}: #{reason}")
{:ok, item_params} {:ok, item_params}
content -> {content, content_type} ->
{:ok, %{item_params | content: content}} {:ok, %{item_params | content: content, content_type: content_type}}
end end
end end
@ -24,14 +24,21 @@ defmodule Frenzy.Pipeline.GeminiScrapeStage do
@impl Stage @impl Stage
def default_opts(), do: %{} def default_opts(), do: %{}
@spec get_content(String.t(), map()) :: {:ok, String.t()} | {:error, term()} @spec get_content(String.t(), map()) :: {String.t(), String.t()} | {:error, term()}
def get_content(url, _opts) do def get_content(url, _opts) do
case Network.gemini_request(url) do case Network.gemini_request(url) do
{:error, reason} -> {:error, reason} ->
{:error, reason} {:error, reason}
{:ok, %Gemini.Response{body: body}} -> {:ok, %Gemini.Response{body: body, meta: meta}} ->
body {body, parse_content_type(meta)}
end end
end end
defp parse_content_type(meta) do
meta
|> String.split(";")
|> hd()
|> String.trim()
end
end end

View File

@ -32,7 +32,9 @@ defmodule Frenzy.Task.CreateItem do
url: url, url: url,
date: date, date: date,
creator: "", creator: "",
content: entry.content content: entry.content,
# we assume text/html in the feed itself, other stages may alter this
content_type: "text/html"
} }
feed = Repo.preload(feed, :pipeline) feed = Repo.preload(feed, :pipeline)

View File

@ -16,6 +16,10 @@
<% end %> <% end %>
</div> </div>
<article class="mt-4"> <article class="item-content mt-4">
<%= raw(@item.content) %> <%= if @item.content_type in [nil, "text/html"] do %>
<%= raw(@item.content) %>
<% else %>
<pre class="raw-content"><%= raw(@item.content) %></pre>
<% end %>
</article> </article>

View File

@ -0,0 +1,9 @@
defmodule Frenzy.Repo.Migrations.ItemAddContentType do
use Ecto.Migration
def change do
alter table(:items) do
add :content_type, :string, default: nil
end
end
end