serve images, not HTML
This commit is contained in:
37
lib/labelmaker_web/constants.ex
Normal file
37
lib/labelmaker_web/constants.ex
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
defmodule LabelmakerWeb.Constants do
|
||||||
|
@defaults %{
|
||||||
|
"label" => "Labelmaker",
|
||||||
|
"font" => "Helvetica",
|
||||||
|
"color" => "black",
|
||||||
|
"size" => "24"
|
||||||
|
}
|
||||||
|
|
||||||
|
@permitted_keys Map.keys(@defaults)
|
||||||
|
|
||||||
|
@colors System.cmd("magick", ["-list", "color"])
|
||||||
|
|> elem(0)
|
||||||
|
|> String.split("\n")
|
||||||
|
# drop header stuff
|
||||||
|
|> Enum.drop(5)
|
||||||
|
|> Enum.map(fn color_info -> color_info |> String.split() |> List.first() end)
|
||||||
|
|
||||||
|
@fonts System.cmd("magick", ["-list", "font"])
|
||||||
|
|> elem(0)
|
||||||
|
|> String.split("\n")
|
||||||
|
|> Enum.filter(&String.starts_with?(&1, " Font:"))
|
||||||
|
|> Enum.reject(&String.starts_with?(&1, " Font: ."))
|
||||||
|
|> Enum.map(&String.trim_leading(&1, " Font: "))
|
||||||
|
|
||||||
|
@sizes 8..72
|
||||||
|
|> Enum.to_list()
|
||||||
|
|> Enum.take_every(4)
|
||||||
|
|> Enum.map(&Integer.to_string/1)
|
||||||
|
|
||||||
|
def defaults, do: @defaults
|
||||||
|
def permitted_keys, do: @permitted_keys
|
||||||
|
def colors, do: @colors
|
||||||
|
def color_count, do: @colors |> length()
|
||||||
|
def fonts, do: @fonts
|
||||||
|
def font_count, do: @fonts |> length()
|
||||||
|
def sizes, do: @sizes
|
||||||
|
end
|
||||||
48
lib/labelmaker_web/controllers/label_controller.ex
Normal file
48
lib/labelmaker_web/controllers/label_controller.ex
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
defmodule LabelmakerWeb.LabelController do
|
||||||
|
use LabelmakerWeb, :controller
|
||||||
|
alias LabelmakerWeb.Constants
|
||||||
|
|
||||||
|
@label_dir Path.join(:code.priv_dir(:labelmaker), "static/labels")
|
||||||
|
File.mkdir_p!(@label_dir)
|
||||||
|
|
||||||
|
def show(conn, params) do
|
||||||
|
options =
|
||||||
|
Constants.defaults()
|
||||||
|
|> Map.merge(params)
|
||||||
|
|> Map.take(Constants.permitted_keys())
|
||||||
|
|
||||||
|
filename =
|
||||||
|
options
|
||||||
|
|> inspect()
|
||||||
|
|> (fn str -> :crypto.hash(:sha256, str) end).()
|
||||||
|
|> Base.encode16(case: :lower)
|
||||||
|
|
||||||
|
filename = filename <> ".png"
|
||||||
|
filepath = Path.join(@label_dir, filename)
|
||||||
|
|
||||||
|
unless File.exists?(filepath) do
|
||||||
|
generate_image(options, filepath)
|
||||||
|
end
|
||||||
|
|
||||||
|
conn
|
||||||
|
|> put_resp_content_type("image/png")
|
||||||
|
|> send_file(200, filepath)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp generate_image(options, filepath) do
|
||||||
|
args = [
|
||||||
|
"-background",
|
||||||
|
"none",
|
||||||
|
"-fill",
|
||||||
|
options["color"],
|
||||||
|
"-pointsize",
|
||||||
|
options["size"],
|
||||||
|
"-font",
|
||||||
|
options["font"],
|
||||||
|
"label:#{options["label"]}",
|
||||||
|
filepath
|
||||||
|
]
|
||||||
|
|
||||||
|
{_, 0} = System.cmd("magick", args)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -24,7 +24,8 @@ defmodule LabelmakerWeb.Router do
|
|||||||
pipe_through :browser
|
pipe_through :browser
|
||||||
|
|
||||||
live "/", Home
|
live "/", Home
|
||||||
live "/:label", Label
|
get "/:label", LabelController, :show
|
||||||
|
# live "/:label", Label
|
||||||
end
|
end
|
||||||
|
|
||||||
# Other scopes may use custom stacks.
|
# Other scopes may use custom stacks.
|
||||||
|
|||||||
Reference in New Issue
Block a user