Merge branch 'main' into feature/338_import_custom_fields
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
commit
9e27de84cb
7 changed files with 199 additions and 22 deletions
|
|
@ -21,4 +21,99 @@ defmodule Mv.Config do
|
|||
def sql_sandbox? do
|
||||
Application.get_env(:mv, :sql_sandbox, false)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the maximum file size for CSV imports in bytes.
|
||||
|
||||
Reads the `max_file_size_mb` value from the CSV import configuration
|
||||
and converts it to bytes.
|
||||
|
||||
## Returns
|
||||
|
||||
- Maximum file size in bytes (default: 10_485_760 bytes = 10 MB)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Mv.Config.csv_import_max_file_size_bytes()
|
||||
10_485_760
|
||||
"""
|
||||
@spec csv_import_max_file_size_bytes() :: non_neg_integer()
|
||||
def csv_import_max_file_size_bytes do
|
||||
max_file_size_mb = get_csv_import_config(:max_file_size_mb, 10)
|
||||
max_file_size_mb * 1024 * 1024
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the maximum number of rows allowed in CSV imports.
|
||||
|
||||
Reads the `max_rows` value from the CSV import configuration.
|
||||
|
||||
## Returns
|
||||
|
||||
- Maximum number of rows (default: 1000)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Mv.Config.csv_import_max_rows()
|
||||
1000
|
||||
"""
|
||||
@spec csv_import_max_rows() :: pos_integer()
|
||||
def csv_import_max_rows do
|
||||
get_csv_import_config(:max_rows, 1000)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the maximum file size for CSV imports in megabytes.
|
||||
|
||||
Reads the `max_file_size_mb` value from the CSV import configuration.
|
||||
|
||||
## Returns
|
||||
|
||||
- Maximum file size in megabytes (default: 10)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Mv.Config.csv_import_max_file_size_mb()
|
||||
10
|
||||
"""
|
||||
@spec csv_import_max_file_size_mb() :: pos_integer()
|
||||
def csv_import_max_file_size_mb do
|
||||
get_csv_import_config(:max_file_size_mb, 10)
|
||||
end
|
||||
|
||||
# Helper function to get CSV import config values
|
||||
defp get_csv_import_config(key, default) do
|
||||
Application.get_env(:mv, :csv_import, [])
|
||||
|> Keyword.get(key, default)
|
||||
|> parse_and_validate_integer(default)
|
||||
end
|
||||
|
||||
# Parses and validates integer configuration values.
|
||||
#
|
||||
# Accepts:
|
||||
# - Integer values (passed through)
|
||||
# - String integers (e.g., "1000") - parsed to integer
|
||||
# - Invalid values (e.g., "abc", nil) - falls back to default
|
||||
#
|
||||
# Always clamps the result to a minimum of 1 to ensure positive values.
|
||||
#
|
||||
# Note: We don't log warnings for unparseable values because:
|
||||
# - These functions may be called frequently (e.g., on every request)
|
||||
# - Logging would create excessive log spam
|
||||
# - The fallback to default provides a safe behavior
|
||||
# - Configuration errors should be caught during deployment/testing
|
||||
defp parse_and_validate_integer(value, _default) when is_integer(value) do
|
||||
max(1, value)
|
||||
end
|
||||
|
||||
defp parse_and_validate_integer(value, default) when is_binary(value) do
|
||||
case Integer.parse(value) do
|
||||
{int, _remainder} -> max(1, int)
|
||||
:error -> default
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_and_validate_integer(_value, default) do
|
||||
default
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -34,8 +34,8 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
|
||||
### Limits
|
||||
|
||||
- Maximum file size: 10 MB
|
||||
- Maximum rows: 1,000 rows (excluding header)
|
||||
- Maximum file size: configurable via `config :mv, csv_import: [max_file_size_mb: ...]`
|
||||
- Maximum rows: configurable via `config :mv, csv_import: [max_rows: ...]` (excluding header)
|
||||
- Processing: chunks of 200 rows
|
||||
- Errors: capped at 50 per import
|
||||
|
||||
|
|
@ -54,8 +54,6 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
on_mount {MvWeb.LiveHelpers, :ensure_user_role_loaded}
|
||||
|
||||
# CSV Import configuration constants
|
||||
# 10 MB
|
||||
@max_file_size_bytes 10_485_760
|
||||
@max_errors 50
|
||||
|
||||
@impl true
|
||||
|
|
@ -76,13 +74,15 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
|> assign(:import_status, :idle)
|
||||
|> assign(:locale, locale)
|
||||
|> assign(:max_errors, @max_errors)
|
||||
|> assign(:csv_import_max_rows, Config.csv_import_max_rows())
|
||||
|> assign(:csv_import_max_file_size_mb, Config.csv_import_max_file_size_mb())
|
||||
|> assign_form()
|
||||
# Configure file upload with auto-upload enabled
|
||||
# Files are uploaded automatically when selected, no need for manual trigger
|
||||
|> allow_upload(:csv_file,
|
||||
accept: ~w(.csv),
|
||||
max_entries: 1,
|
||||
max_file_size: @max_file_size_bytes,
|
||||
max_file_size: Config.csv_import_max_file_size_bytes(),
|
||||
auto_upload: true
|
||||
)
|
||||
|
||||
|
|
@ -206,7 +206,7 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
/>
|
||||
<label class="label" id="csv_file_help">
|
||||
<span class="label-text-alt">
|
||||
{gettext("CSV files only, maximum 10 MB")}
|
||||
{gettext("CSV files only, maximum %{size} MB", size: @csv_import_max_file_size_mb)}
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
|
|
@ -417,7 +417,8 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
actor = MvWeb.LiveHelpers.current_actor(socket)
|
||||
|
||||
with {:ok, content} <- consume_and_read_csv(socket),
|
||||
{:ok, import_state} <- MemberCSV.prepare(content, actor: actor) do
|
||||
{:ok, import_state} <-
|
||||
MemberCSV.prepare(content, max_rows: Config.csv_import_max_rows(), actor: actor) do
|
||||
start_import(socket, import_state)
|
||||
else
|
||||
{:error, reason} when is_binary(reason) ->
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue