Bastion.Upload
lib/upload.march — Bastion.Upload: multipart form-data upload parsing.
Parses multipart/form-data request bodies into a list of UploadedFile records (for file fields) and a list of (name, value) pairs (for text fields).
The body is read fully into memory before parsing. This works well for uploads up to a few hundred MB on typical hardware; for true streaming to external storage see the Streaming section below.
── Usage ──────────────────────────────────────────────────────────────────────
fn handle_upload(conn) do
let opts = { Bastion.Upload.default_opts() with
max_file_size = 5_000_000,
allowed_types = ["image/jpeg", "image/png", "image/webp"]
}
match Bastion.Upload.parse_conn(conn, opts) do
Err(e) ->
let msg = Bastion.Upload.error_message(e)
Controller.json(conn, 422, "{\"error\":\"" ++ msg ++ "\"}")
Ok(result) ->
let files = result.files
let fields = result.fields
let photo = List.head(files)
-- process files...
Controller.json(conn, 200, "{\"uploaded\":" ++ String.from_int(List.length(files)) ++ "}")
end
endAfter the request handler returns, temporary files are automatically deleted via the after-send hook system.
Types
Functions
Return default upload options (10MB max file, 10 files, 64KB fields, all types allowed).
Return a human-readable message for an UploadError.
Parse a raw multipart body with an explicit content-type header string.
Useful in tests where you build the body manually:
let body = build_multipart_body(boundary, fields, files)
let ct = "multipart/form-data; boundary=" ++ boundary
Bastion.Upload.parse(body, ct, Bastion.Upload.default_opts())Parse a multipart/form-data request from a Conn.
Reads the full request body and content-type header, then delegates to parse/3.
Returns Ok(UploadResult) or Err(UploadError).