-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathog_image.ex
More file actions
32 lines (24 loc) · 869 Bytes
/
Copy pathog_image.ex
File metadata and controls
32 lines (24 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
defmodule JolaDevWeb.Plugs.OGImage do
@moduledoc """
Serves Open Graph preview images from `JolaDev.OGImage`. Intercepts any
`/images/og/<slug>.png` request, looks up the baked PNG bytes, and sends
them with appropriate cache headers.
"""
@behaviour Plug
import Plug.Conn
@dev_mode Application.compile_env!(:jola_dev, :og_image_dev_mode)
@cache_control if @dev_mode, do: "public, max-age=0", else: "public, max-age=31536000"
@impl Plug
def init(opts), do: opts
@impl Plug
def call(%Plug.Conn{request_path: "/images/og/" <> rest} = conn, _) do
slug = String.replace_suffix(rest, ".png", "")
{:ok, bytes} = JolaDev.OGImage.image_for(slug)
conn
|> put_resp_content_type("image/png")
|> put_resp_header("cache-control", @cache_control)
|> send_resp(200, bytes)
|> halt()
end
def call(conn, _), do: conn
end