Current section
Files
Jump to
Current section
Files
src/multipartkit@infer.erl
-module(multipartkit@infer).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/multipartkit/infer.gleam").
-export([default_inferer/0, content_type_from_filename/1, content_type_from_bytes/1]).
-export_type([inferer/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type inferer() :: {inferer,
fun((binary()) -> gleam@option:option(binary())),
fun((bitstring()) -> gleam@option:option(binary()))}.
-file("src/multipartkit/infer.gleam", 58).
-spec noop_filename(binary()) -> gleam@option:option(binary()).
noop_filename(_) ->
none.
-file("src/multipartkit/infer.gleam", 62).
-spec noop_bytes(bitstring()) -> gleam@option:option(binary()).
noop_bytes(_) ->
none.
-file("src/multipartkit/infer.gleam", 54).
?DOC(
" Inferer that always returns `None`.\n"
"\n"
" `add_file_auto` uses this and therefore always emits\n"
" `application/octet-stream` unless the host swaps in a real inferer via\n"
" `add_file_auto_with`.\n"
).
-spec default_inferer() -> inferer().
default_inferer() ->
{inferer, fun noop_filename/1, fun noop_bytes/1}.
-file("src/multipartkit/infer.gleam", 107).
?DOC(
" Optional content-type inference from a filename.\n"
"\n"
" This is the default no-op of multipartkit's pluggable inference\n"
" interface: it **always returns `None`** for every input, including\n"
" well-known extensions like `\"photo.png\"` or `\"doc.pdf\"`. multipartkit\n"
" deliberately ships no built-in extension table so the library has zero\n"
" inference dependencies; callers who want real inference wire one in via\n"
" `form.add_file_auto_with` (the function this helper exists to compose\n"
" with). Calling this function directly is rarely what you want — it\n"
" exists mainly so the `Inferer` shape stays uniform whether or not a\n"
" real inferer is wired in.\n"
"\n"
" To get actual inference, build an `Inferer` from `nao1215/mimetype`\n"
" (or any other inference library) and pass it to\n"
" `form.add_file_auto_with`:\n"
"\n"
" ```gleam\n"
" import gleam/option.{type Option, None, Some}\n"
" import mimetype\n"
" import multipartkit/form\n"
" import multipartkit/infer.{type Inferer, Inferer}\n"
"\n"
" let inferer =\n"
" Inferer(\n"
" from_filename: fn(name) {\n"
" case mimetype.filename_to_mime_type_strict(name) {\n"
" Ok(value) -> Some(value)\n"
" Error(_) -> None\n"
" }\n"
" },\n"
" from_bytes: fn(body) {\n"
" case mimetype.detect_strict(body) {\n"
" Ok(value) -> Some(value)\n"
" Error(_) -> None\n"
" }\n"
" },\n"
" )\n"
" form.add_file_auto_with(my_form, \"upload\", \"photo.png\", bytes, inferer)\n"
" ```\n"
"\n"
" See also `examples/mimetype_inference` for a runnable wiring.\n"
).
-spec content_type_from_filename(binary()) -> gleam@option:option(binary()).
content_type_from_filename(Filename) ->
(erlang:element(2, default_inferer()))(Filename).
-file("src/multipartkit/infer.gleam", 119).
?DOC(
" Optional content-type inference from a body byte sequence.\n"
"\n"
" Same documented default-no-op policy as `content_type_from_filename`:\n"
" this **always returns `None`** for every input, including well-known\n"
" magic-byte signatures like the 8-byte PNG header. Wire `nao1215/mimetype`\n"
" (or another inferer) in via `form.add_file_auto_with` for actual\n"
" inference — see the docstring on `content_type_from_filename` for a\n"
" worked example.\n"
).
-spec content_type_from_bytes(bitstring()) -> gleam@option:option(binary()).
content_type_from_bytes(Body) ->
(erlang:element(3, default_inferer()))(Body).