refrakt/upload
File upload handling.
Provides helpers for processing multipart file uploads from Wisp’s form data.
Usage
import refrakt/upload
pub fn create(req: Request, ctx: Context) -> Response {
use form_data <- wisp.require_form(req)
case upload.get_file(form_data, "avatar") {
Ok(file) -> {
// Save to disk
let assert Ok(path) = upload.save(file, to: "priv/uploads")
// path is like "priv/uploads/abc123_avatar.jpg"
}
Error(_) -> // No file uploaded
}
}
Types
Upload configuration.
pub type UploadConfig {
UploadConfig(
max_size_bytes: Int,
allowed_types: List(String),
upload_dir: String,
)
}
Constructors
-
UploadConfig( max_size_bytes: Int, allowed_types: List(String), upload_dir: String, )
An uploaded file with metadata.
pub type UploadedFile {
UploadedFile(
field_name: String,
file_name: String,
path: String,
content_type: String,
)
}
Constructors
-
UploadedFile( field_name: String, file_name: String, path: String, content_type: String, )
Values
pub fn default_config() -> UploadConfig
Default upload config: 10MB max, common image types.
pub fn get_file(
form_data: wisp.FormData,
field_name: String,
) -> Result(UploadedFile, String)
Extract an uploaded file from form data by field name.
pub fn save(
file: UploadedFile,
to dest_dir: String,
) -> Result(String, String)
Save an uploaded file to the given directory. Returns the full path of the saved file.