Current section

Files

Jump to
multipartkit src multipartkit@validate.erl
Raw

src/multipartkit@validate.erl

-module(multipartkit@validate).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/multipartkit/validate.gleam").
-export([has_field/2, max_file_size/2, allowed_content_types/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/multipartkit/validate.gleam", 13).
?DOC(" Return `True` if any part is a text field with the given `name`.\n").
-spec has_field(list(multipartkit@part:part()), binary()) -> boolean().
has_field(Parts, Name) ->
gleam@list:any(
Parts,
fun(P) -> case {erlang:element(3, P), erlang:element(4, P)} of
{{some, Value}, none} ->
Value =:= Name;
{_, _} ->
false
end end
).
-file("src/multipartkit/validate.gleam", 23).
?DOC(" Validate that the part body does not exceed `max` bytes.\n").
-spec max_file_size(multipartkit@part:part(), integer()) -> {ok,
multipartkit@part:part()} |
{error, multipartkit@error:multipart_error()}.
max_file_size(The_part, Max) ->
gleam@bool:guard(
erlang:byte_size(erlang:element(6, The_part)) > Max,
{error, {part_too_large, Max}},
fun() -> {ok, The_part} end
).
-file("src/multipartkit/validate.gleam", 52).
-spec strip_parameters(binary()) -> binary().
strip_parameters(Value) ->
case gleam@string:split_once(Value, <<";"/utf8>>) of
{ok, {Media, _}} ->
gleam@string:trim(Media);
{error, nil} ->
gleam@string:trim(Value)
end.
-file("src/multipartkit/validate.gleam", 33).
?DOC(
" Validate that the part's `Content-Type` media type (case-insensitive)\n"
" belongs to `allowed`. Parameters on the part's Content-Type are ignored.\n"
).
-spec allowed_content_types(multipartkit@part:part(), list(binary())) -> {ok,
multipartkit@part:part()} |
{error, multipartkit@error:multipart_error()}.
allowed_content_types(The_part, Allowed) ->
Actual = case erlang:element(5, The_part) of
{some, Value} ->
Value;
none ->
<<""/utf8>>
end,
Actual_media = strip_parameters(Actual),
case gleam@list:any(
Allowed,
fun(Candidate) ->
multipartkit@internal@text:equals_ci(
strip_parameters(Candidate),
Actual_media
)
end
) of
true ->
{ok, The_part};
false ->
{error, {disallowed_content_type, Actual}}
end.