Cache transformations
This commit is contained in:
@@ -5,8 +5,12 @@ defmodule MusicLibrary.Application do
|
|||||||
|
|
||||||
use Application
|
use Application
|
||||||
|
|
||||||
|
alias MusicLibrary.Assets
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def start(_type, _args) do
|
def start(_type, _args) do
|
||||||
|
_ = Assets.Cache.new()
|
||||||
|
|
||||||
children = [
|
children = [
|
||||||
MusicLibraryWeb.Telemetry,
|
MusicLibraryWeb.Telemetry,
|
||||||
MusicLibrary.Vault,
|
MusicLibrary.Vault,
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
defmodule MusicLibrary.Assets.Cache do
|
||||||
|
def new do
|
||||||
|
:ets.new(__MODULE__, [:named_table, :public, read_concurrency: true])
|
||||||
|
end
|
||||||
|
|
||||||
|
def set(payload, format, content) do
|
||||||
|
:ets.insert(__MODULE__, {{payload, format}, content})
|
||||||
|
end
|
||||||
|
|
||||||
|
def get(payload, format) do
|
||||||
|
case :ets.lookup(__MODULE__, {payload, format}) do
|
||||||
|
[{{^payload, ^format}, content}] -> {:found, content}
|
||||||
|
[] -> :not_found
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -6,18 +6,25 @@ defmodule MusicLibrary.Records.Cover do
|
|||||||
|
|
||||||
@external_resource fallback_path
|
@external_resource fallback_path
|
||||||
|
|
||||||
@size 2000
|
@default_size 2000
|
||||||
|
@default_format "image/jpeg"
|
||||||
|
|
||||||
def fallback_data, do: unquote(fallback_data)
|
def fallback_data, do: unquote(fallback_data)
|
||||||
|
|
||||||
def resize(cover_data, size \\ @size) do
|
def resize(cover_data, size \\ @default_size, format \\ @default_format) do
|
||||||
{:ok, thumb} = Operation.thumbnail_buffer(cover_data, size)
|
{:ok, thumb} = Operation.thumbnail_buffer(cover_data, size)
|
||||||
Image.write_to_buffer(thumb, ".jpg")
|
Image.write_to_buffer(thumb, extension(format))
|
||||||
end
|
end
|
||||||
|
|
||||||
def correct_size?(cover_data) do
|
def convert(cover_data, data_format, target_format) do
|
||||||
{:ok, image} = Image.new_from_buffer(cover_data)
|
if data_format == target_format do
|
||||||
|
{:ok, cover_data}
|
||||||
Image.width(image) == @size
|
else
|
||||||
|
{:ok, image} = Image.new_from_buffer(cover_data)
|
||||||
|
Image.write_to_buffer(image, extension(target_format))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp extension("image/jpeg"), do: ".jpg"
|
||||||
|
defp extension("image/webp"), do: ".webp"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,36 +2,58 @@ defmodule MusicLibraryWeb.CoverController do
|
|||||||
use MusicLibraryWeb, :controller
|
use MusicLibraryWeb, :controller
|
||||||
|
|
||||||
alias MusicLibrary.Assets
|
alias MusicLibrary.Assets
|
||||||
alias MusicLibrary.Assets.Transform
|
alias MusicLibrary.Assets.{Cache, Transform}
|
||||||
alias MusicLibrary.Records.Cover
|
alias MusicLibrary.Records.Cover
|
||||||
|
|
||||||
# 1 year in seconds
|
# 1 year in seconds
|
||||||
@cache_duration 60 * 60 * 24 * 365
|
@cache_duration 60 * 60 * 24 * 365
|
||||||
|
|
||||||
def show(conn, %{"transform_payload" => payload}) do
|
def show(conn, %{"transform_payload" => payload}) do
|
||||||
|
format = pick_format(conn)
|
||||||
transform = Transform.decode!(payload)
|
transform = Transform.decode!(payload)
|
||||||
|
|
||||||
case Assets.get(transform.hash) do
|
case cached_get(payload, transform, format) do
|
||||||
nil ->
|
nil ->
|
||||||
not_found(conn)
|
not_found(conn)
|
||||||
|
|
||||||
%{content: content} ->
|
content when is_binary(content) ->
|
||||||
image_data =
|
|
||||||
if transform.width do
|
|
||||||
# TODO: find a way to cache computation, or pre-compute thumb and store it
|
|
||||||
{:ok, thumb_data} = Cover.resize(content, transform.width)
|
|
||||||
thumb_data
|
|
||||||
else
|
|
||||||
content
|
|
||||||
end
|
|
||||||
|
|
||||||
case get_req_header(conn, "if-none-match") do
|
case get_req_header(conn, "if-none-match") do
|
||||||
[^payload] -> extend_cache(conn)
|
[^payload] -> extend_cache(conn)
|
||||||
_ -> respond_with_cache(conn, image_data, payload)
|
_ -> respond_with_cache(conn, content, format, payload)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp cached_get(payload, transform, format) do
|
||||||
|
case Cache.get(payload, format) do
|
||||||
|
:not_found ->
|
||||||
|
if asset = Assets.get(transform.hash) do
|
||||||
|
image_data =
|
||||||
|
if transform.width do
|
||||||
|
# TODO: find a way to cache computation, or pre-compute thumb and store it
|
||||||
|
{:ok, data} = Cover.resize(asset.content, transform.width, format)
|
||||||
|
data
|
||||||
|
else
|
||||||
|
{:ok, data} = Cover.convert(asset.content, asset.format, format)
|
||||||
|
data
|
||||||
|
end
|
||||||
|
|
||||||
|
Cache.set(payload, format, image_data)
|
||||||
|
image_data
|
||||||
|
end
|
||||||
|
|
||||||
|
{:found, content} ->
|
||||||
|
content
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp pick_format(conn) do
|
||||||
|
case get_req_header(conn, "accept") do
|
||||||
|
["image/webp" <> _] -> "image/webp"
|
||||||
|
_ -> "image/jpeg"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp not_found(conn) do
|
defp not_found(conn) do
|
||||||
conn
|
conn
|
||||||
|> put_status(:not_found)
|
|> put_status(:not_found)
|
||||||
@@ -44,9 +66,9 @@ defmodule MusicLibraryWeb.CoverController do
|
|||||||
|> send_resp(304, "")
|
|> send_resp(304, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
defp respond_with_cache(conn, data, etag) do
|
defp respond_with_cache(conn, data, format, etag) do
|
||||||
conn
|
conn
|
||||||
|> put_resp_content_type("image/jpeg", "utf-8")
|
|> put_resp_content_type(format, "utf-8")
|
||||||
|> put_resp_header("cache-control", "public, max-age=#{@cache_duration}")
|
|> put_resp_header("cache-control", "public, max-age=#{@cache_duration}")
|
||||||
|> put_resp_header("etag", etag)
|
|> put_resp_header("etag", etag)
|
||||||
|> send_resp(200, data)
|
|> send_resp(200, data)
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ defmodule MusicLibrary.Records.CoverTest do
|
|||||||
cover_data = marbles_cover_data()
|
cover_data = marbles_cover_data()
|
||||||
{:ok, resized_cover} = Cover.resize(cover_data)
|
{:ok, resized_cover} = Cover.resize(cover_data)
|
||||||
assert cover_data !== resized_cover
|
assert cover_data !== resized_cover
|
||||||
assert Cover.correct_size?(resized_cover)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ defmodule MusicLibraryWeb.CoverControllerTest do
|
|||||||
%{asset: asset}
|
%{asset: asset}
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "GET /covers/:hash" do
|
describe "GET /covers/:payload" do
|
||||||
setup [:create_asset]
|
setup [:create_asset]
|
||||||
|
|
||||||
test "404s when asset doesn't exist", %{conn: conn} do
|
test "404s when asset doesn't exist", %{conn: conn} do
|
||||||
|
|||||||
Reference in New Issue
Block a user