@@ -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 ) }
0 commit comments