Skip to content

Commit

Permalink
Trim BOM from bulk create CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Dec 8, 2023
1 parent 31e4a37 commit 35efaa0
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions app/lib/meadow_web/controllers/authority_records_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule MeadowWeb.AuthorityRecordsController do

defp csv_to_maps(file) do
[headers | rows] =
File.stream!(file, [], :line)
File.stream!(file, [:trim_bom], :line)
|> CSV.parse_stream(skip_headers: false)
|> Enum.to_list()

Expand All @@ -60,12 +60,17 @@ defmodule MeadowWeb.AuthorityRecordsController do
headers = Enum.map(headers, &String.to_atom/1)
{:ok, Enum.map(rows, fn row -> Enum.zip(headers, row) |> Enum.into(%{}) end)}

_ ->
other ->
Logger.error("Bad headers: " <> inspect(other))
{:error, :bad_format}
end
rescue
NimbleCSV.ParseError -> {:error, :bad_format}
exception -> reraise exception, __STACKTRACE__
exception in NimbleCSV.ParseError ->
Logger.error("Parsing error: " <> inspect(exception))
{:error, :bad_format}

exception ->
reraise exception, __STACKTRACE__
end

def export(conn, %{"file" => file} = params) do
Expand Down

0 comments on commit 35efaa0

Please sign in to comment.