From 1ee183557ce50719bb4e9ed53b5284b41bbfcbc9 Mon Sep 17 00:00:00 2001 From: Gavin McDonald Date: Mon, 5 May 2025 17:22:08 -0400 Subject: [PATCH] enforce constraints on the settings, store settings in the image --- lib/labelmaker_web/constants.ex | 9 ++++++--- .../controllers/label_controller.ex | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/labelmaker_web/constants.ex b/lib/labelmaker_web/constants.ex index 22ff0b4..6a2b955 100644 --- a/lib/labelmaker_web/constants.ex +++ b/lib/labelmaker_web/constants.ex @@ -1,6 +1,6 @@ defmodule LabelmakerWeb.Constants do @defaults %{ - "label" => "Labelmaker", + "label" => "64 character maximum", "font" => "Helvetica", "color" => "black", "size" => "24" @@ -22,16 +22,19 @@ defmodule LabelmakerWeb.Constants do |> Enum.reject(&String.starts_with?(&1, " Font: .")) |> Enum.map(&String.trim_leading(&1, " Font: ")) + @max_label_length 64 + @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 defaults, do: @defaults def fonts, do: @fonts def font_count, do: @fonts |> length() + def max_label_length, do: @max_label_length + def permitted_keys, do: @permitted_keys def sizes, do: @sizes end diff --git a/lib/labelmaker_web/controllers/label_controller.ex b/lib/labelmaker_web/controllers/label_controller.ex index 93eebf1..f112a4e 100644 --- a/lib/labelmaker_web/controllers/label_controller.ex +++ b/lib/labelmaker_web/controllers/label_controller.ex @@ -6,10 +6,23 @@ defmodule LabelmakerWeb.LabelController do File.mkdir_p!(@label_dir) def show(conn, params) do + params = + params + |> Map.take(Constants.permitted_keys()) + |> Enum.filter(fn {k, v} -> + case k do + "color" -> v in Constants.colors() + "font" -> v in Constants.fonts() + "label" -> String.length(v) <= Constants.max_label_length() + "size" -> v in Constants.sizes() + _ -> true + end + end) + |> Map.new() + options = Constants.defaults() |> Map.merge(params) - |> Map.take(Constants.permitted_keys()) filename = options @@ -40,6 +53,9 @@ defmodule LabelmakerWeb.LabelController do "-font", options["font"], "label:#{options["label"]}", + "-set", + "comment", + inspect(options), filepath ]