Server records on this day email assets from a public endpoint
This commit is contained in:
@@ -288,7 +288,7 @@ All authenticated routes live inside a single `live_session` with three `on_moun
|
|||||||
| `HealthController` | `/health` | Health check |
|
| `HealthController` | `/health` | Health check |
|
||||||
| `LastFmController` | `/auth/last_fm/callback` | Last.fm OAuth |
|
| `LastFmController` | `/auth/last_fm/callback` | Last.fm OAuth |
|
||||||
| `ArchiveController` | `/backup`, `/api/backup` | Database backup download (API route requires token) |
|
| `ArchiveController` | `/backup`, `/api/backup` | Database backup download (API route requires token) |
|
||||||
| `AssetController` | `/assets/:transform_payload`, `/api/assets/:transform_payload` | Serve images with transforms (API route requires token) |
|
| `AssetController` | `/assets/:transform_payload`, `/public/assets/:transform_payload`, `/api/assets/:transform_payload` | Serve images with transforms (public route for emails, API route requires token) |
|
||||||
| `CollectionController` | `/api/collection/*` | JSON API for collection queries |
|
| `CollectionController` | `/api/collection/*` | JSON API for collection queries |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -44,11 +44,10 @@ defmodule MusicLibrary.RecordsOnThisDayEmail do
|
|||||||
|
|
||||||
defp build_html(records, date, conf) do
|
defp build_html(records, date, conf) do
|
||||||
base_url = Keyword.fetch!(conf, :base_url) |> String.trim_trailing("/")
|
base_url = Keyword.fetch!(conf, :base_url) |> String.trim_trailing("/")
|
||||||
api_token = Application.get_env(:music_library, MusicLibraryWeb)[:api_token]
|
|
||||||
heading = date |> Calendar.strftime("Records on %-d %B") |> html_escape()
|
heading = date |> Calendar.strftime("Records on %-d %B") |> html_escape()
|
||||||
|
|
||||||
records_html =
|
records_html =
|
||||||
Enum.map_join(records, "\n", fn record -> record_html(record, date, base_url, api_token) end)
|
Enum.map_join(records, "\n", fn record -> record_html(record, date, base_url) end)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
<div style="max-width: 600px; margin: 0 auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif; background-color: #f4f4f5;">
|
<div style="max-width: 600px; margin: 0 auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif; background-color: #f4f4f5;">
|
||||||
@@ -62,9 +61,9 @@ defmodule MusicLibrary.RecordsOnThisDayEmail do
|
|||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
defp record_html(record, date, base_url, api_token) do
|
defp record_html(record, date, base_url) do
|
||||||
years = Record.released_how_long_ago?(record, date)
|
years = Record.released_how_long_ago?(record, date)
|
||||||
cover_url = cover_image_url(record, base_url, api_token)
|
cover_url = cover_image_url(record, base_url)
|
||||||
record_url = record_detail_url(record, base_url)
|
record_url = record_detail_url(record, base_url)
|
||||||
artist_names = record |> Record.artist_names() |> html_escape()
|
artist_names = record |> Record.artist_names() |> html_escape()
|
||||||
title = record.title |> html_escape()
|
title = record.title |> html_escape()
|
||||||
@@ -106,11 +105,11 @@ defmodule MusicLibrary.RecordsOnThisDayEmail do
|
|||||||
"#{base_url}/collection/#{record.id}"
|
"#{base_url}/collection/#{record.id}"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp cover_image_url(%{cover_hash: nil}, _base_url, _api_token), do: nil
|
defp cover_image_url(%{cover_hash: nil}, _base_url), do: nil
|
||||||
|
|
||||||
defp cover_image_url(record, base_url, api_token) do
|
defp cover_image_url(record, base_url) do
|
||||||
payload = Transform.new(hash: record.cover_hash, width: 96) |> Transform.encode!()
|
payload = Transform.new(hash: record.cover_hash, width: 96) |> Transform.encode!()
|
||||||
"#{base_url}/api/assets/#{payload}?token=#{api_token}"
|
"#{base_url}/public/assets/#{payload}"
|
||||||
end
|
end
|
||||||
|
|
||||||
defp years_ago_label(nil), do: ""
|
defp years_ago_label(nil), do: ""
|
||||||
|
|||||||
@@ -32,6 +32,8 @@ defmodule MusicLibraryWeb.Router do
|
|||||||
|
|
||||||
get "/auth/last_fm/callback", LastFmController, :callback
|
get "/auth/last_fm/callback", LastFmController, :callback
|
||||||
|
|
||||||
|
get "/public/assets/:transform_payload", AssetController, :show
|
||||||
|
|
||||||
scope "/" do
|
scope "/" do
|
||||||
pipe_through :logged_in
|
pipe_through :logged_in
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user