Skip to content

Commit 04d1cf4

Browse files
committed
dry
1 parent 91bf20e commit 04d1cf4

File tree

3 files changed

+4
-55
lines changed

3 files changed

+4
-55
lines changed

lib/diff_web/live/diff_live_view.ex

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ defmodule DiffWeb.DiffLiveView do
227227

228228
diff_data =
229229
Jason.encode!(%{
230-
"diff" => sanitize_utf8(raw_diff),
230+
"diff" => DiffWeb.LiveView.sanitize_utf8(raw_diff),
231231
"path_from" => path_from,
232232
"path_to" => path_to
233233
})
@@ -328,50 +328,6 @@ defmodule DiffWeb.DiffLiveView do
328328
defp parse_version(""), do: {:ok, :latest}
329329
defp parse_version(input), do: Version.parse(input)
330330

331-
defp sanitize_utf8(content) when is_binary(content) do
332-
case String.valid?(content) do
333-
true ->
334-
content
335-
336-
false ->
337-
# Multiple fallback strategies for invalid UTF-8
338-
content
339-
|> sanitize_invalid_bytes()
340-
end
341-
end
342-
343-
defp sanitize_utf8(content), do: content
344-
345-
defp sanitize_invalid_bytes(content) do
346-
# Try different encoding conversions and fallbacks
347-
cond do
348-
# Try converting from common encodings
349-
latin1_result = safe_unicode_convert(content, :latin1, :utf8) ->
350-
latin1_result
351-
352-
# Try converting from ISO-8859-1
353-
iso_result = safe_unicode_convert(content, {:encoding, :latin1}, :utf8) ->
354-
iso_result
355-
356-
# Last resort: replace invalid bytes with replacement character
357-
true ->
358-
content
359-
|> :binary.bin_to_list()
360-
# Replace high bytes with '?'
361-
|> Enum.map(fn byte -> if byte > 127, do: 63, else: byte end)
362-
|> :binary.list_to_bin()
363-
end
364-
end
365-
366-
defp safe_unicode_convert(content, from, to) do
367-
case :unicode.characters_to_binary(content, from, to) do
368-
result when is_binary(result) -> result
369-
_ -> nil
370-
end
371-
rescue
372-
_ -> nil
373-
end
374-
375331
defp parse_diff(diff) do
376332
case String.split(diff, ":", trim: true) do
377333
[app, from, to] -> {app, from, to, build_url(app, from, to)}

lib/diff_web/views/live_view.ex

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defmodule DiffWeb.LiveView do
4444
end
4545
end
4646

47-
defp sanitize_utf8(content) when is_binary(content) do
47+
def sanitize_utf8(content) when is_binary(content) do
4848
case String.valid?(content) do
4949
true ->
5050
content
@@ -55,19 +55,15 @@ defmodule DiffWeb.LiveView do
5555
end
5656
end
5757

58-
defp sanitize_utf8(content), do: content
58+
def sanitize_utf8(content), do: content
5959

6060
defp sanitize_invalid_bytes(content) do
6161
# Try different encoding conversions and fallbacks
6262
cond do
63-
# Try converting from common encodings
63+
# Try converting from Latin-1/ISO-8859-1 encoding
6464
latin1_result = safe_unicode_convert(content, :latin1, :utf8) ->
6565
latin1_result
6666

67-
# Try converting from ISO-8859-1
68-
iso_result = safe_unicode_convert(content, {:encoding, :latin1}, :utf8) ->
69-
iso_result
70-
7167
# Last resort: replace invalid bytes with replacement character
7268
true ->
7369
content

lib/diff_web/views/page_view.ex

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)