Skip to content

Commit cbc7c97

Browse files
committed
Base64 for binary fields
1 parent 314fedb commit cbc7c97

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

lib/kaffy/resource_form.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ defmodule Kaffy.ResourceForm do
152152
:decimal ->
153153
text_input(form, field, opts)
154154

155+
:binary ->
156+
value =
157+
data
158+
|> Map.get(field, "")
159+
|> Base.encode64()
160+
161+
text_input(form, field, [value: value] ++ opts)
162+
155163
t when t in [:boolean, :boolean_checkbox] ->
156164
checkbox_opts = add_class(opts, "custom-control-input")
157165
label_opts = add_class(opts, "custom-control-label")

lib/kaffy/resource_params.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Kaffy.ResourceParams do
33

44
def decode_map_fields(resource, schema, params) do
55
map_fields = ResourceSchema.get_map_fields(schema) |> Enum.map(fn {f, _} -> to_string(f) end)
6+
binary_fields = ResourceSchema.get_binary_fields(schema) |> Enum.map(fn {f, _} -> to_string(f) end)
67

78
attrs =
89
Map.get(params, resource, %{})
@@ -12,9 +13,13 @@ defmodule Kaffy.ResourceParams do
1213
{k, v}
1314

1415
false ->
15-
case k in map_fields && String.length(v) > 0 do
16-
true -> {k, Kaffy.Utils.json().decode!(v)}
17-
false -> {k, v}
16+
if k in binary_fields && String.length(v) > 0 do
17+
{k, Base.decode64!(v)}
18+
else
19+
case k in map_fields && String.length(v) > 0 do
20+
true -> {k, Kaffy.Utils.json().decode!(v)}
21+
false -> {k, v}
22+
end
1823
end
1924
end
2025
end)

lib/kaffy/resource_schema.ex

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ defmodule Kaffy.ResourceSchema do
191191
Kaffy.Utils.json().encode!(value, escape: :html_safe, pretty: true)
192192

193193
is_binary(value) ->
194-
value
194+
if String.valid?(value) do
195+
value
196+
else
197+
Base.encode64(value)
198+
end
195199

196200
true ->
197201
kaffy_field_value(schema, field)
@@ -218,7 +222,11 @@ defmodule Kaffy.ResourceSchema do
218222
Kaffy.Utils.json().encode!(value, escape: :html_safe, pretty: true)
219223

220224
is_binary(value) ->
221-
String.slice(value, 0, 140)
225+
if String.valid?(value) do
226+
String.slice(value, 0, 140)
227+
else
228+
Base.encode64(String.slice(value, 0, 140))
229+
end
222230

223231
is_list(value) ->
224232
pretty_list(value)
@@ -328,6 +336,17 @@ defmodule Kaffy.ResourceSchema do
328336
end)
329337
end
330338

339+
def get_binary_fields(schema) do
340+
get_all_fields(schema)
341+
|> Enum.filter(fn
342+
{_f, %{type: :binary}} ->
343+
true
344+
345+
_ ->
346+
false
347+
end)
348+
end
349+
331350
def widgets(_resource) do
332351
[]
333352
end

0 commit comments

Comments
 (0)