Credo: upload attrs read once into atom keys

bc25690 · Gabe Morell · 2026-11-04 21:30

3 files +10 -3

Files changed

modified lib/oh_my_ex_oh_ircd/images.ex
+10 −3
@@ -238,10 +238,10 @@ defmodule OhMyExOhIrcd.Images do
238 238 :bad_type | :too_large | :not_registered | changeset | reason}`.
239 239 """
240 240 def store_upload(channel_name, attrs, source) when is_binary(channel_name) do
241 − content_type = attrs[:content_type] || attrs["content_type"]
242 − byte_size = attrs[:byte_size] || attrs["byte_size"]
241 + attrs = upload_attrs(attrs)
242 + content_type = attrs.content_type
243 243
244 − with :ok <- validate_upload(content_type, byte_size) do
244 + with :ok <- validate_upload(content_type, attrs.byte_size) do
245 245 case Channels.get(channel_name) do
246 246 nil -> {:error, :not_registered}
247 247 %ChannelRecord{id: id} -> do_store(id, attrs, content_type, source)
@@ -249,6 +249,13 @@ defmodule OhMyExOhIrcd.Images do
249 249 end
250 250 end
251 251
252 + @upload_attrs [:who, :user_id, :title, :content_type, :byte_size]
253 +
254 + # attrs may come atom- or string-keyed; read once into atom keys
255 + defp upload_attrs(attrs), do: Map.new(@upload_attrs, &{&1, upload_attr(attrs, &1)})
256 +
257 + defp upload_attr(attrs, key), do: Map.get(attrs, key) || Map.get(attrs, Atom.to_string(key))
258 +
252 259 defp validate_upload(content_type, byte_size) do
253 260 cond do
254 261 content_type not in allowed_types() -> {:error, :bad_type}

Parents: 87dde30