Files

MIME types

MIME types from file names and extensions, the extensions of a type, and Content-Type headers with a charset for text.

Version 1.0.0 · 4 functions · 1 crate

#files#downloads#uploads#http

Functions

What use mime.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use mime_of, mime_all from mime (Quillo 0.6).

mime_of(name: str) → str

The MIME type of a file name, a path or a bare extension ("report.pdf", "css"), in any case: "application/octet-stream" when it is unknown.

mime_all(name: str) → list

Every MIME type a name may have, the usual one first: [] when it is unknown.

mime_extensions(mime: str) → list

The extensions of a MIME type ("image/jpeg" gives jpe, jpeg, jpg), without dots: [] for an unknown type.

mime_content_type(name: str) → str

A Content-Type header for a file name: its MIME type, with "; charset=utf-8" added for text, JSON, JavaScript, XML and SVG.

Get it

$ quillo install mime               # mime.qwer into this folder$ quillo install mime --example     # and its example, mime.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/mime.qwer$ curl -O https://quillolang.org/plugins/files/mime.qwe

Then, in your program, all of it or only what you call:

main.qwe
use mime.qweruse mime_of, mime_all from mime      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
mime_guess = { version = "2", default-features = false, features = ["rev-mappings"] }

Example

mime.qwe
// MIME types for downloads, and the extensions an upload form accepts.////   quillo run mime.qwe      then open http://localhost:8080/typesuse "mime.qwer"route GET /types = ["report.pdf", "Photo.JPG", "style.css", "data.json", "song.mp3", "backup.tar.gz", "css", "notes.unknown"]    | map((n) => { name: n, type: mime_of(n), header: mime_content_type(n) })route GET /extensions ? { type: str = "image/jpeg" } = { type, extensions: mime_extensions(type), all_for_xml: mime_all("feed.xml") }// A file served with the type its name says.route GET /docs/:name {    write_file("data/docs/notes.md", "# Notes\n\nShipped on Friday.\n")    let text = read_file(`data/docs/${name}`) ?? reply 404 "No such document"    respond(200, text, { "content-type": mime_content_type(name) })}
$ quillo run mime.qwe$ curl 'localhost:8080/types'$ curl 'localhost:8080/extensions?type=image/png'

Source

mime.qwer
//! MIME types from file names and extensions, and the extensions of a MIME type: for downloads, uploads and Content-Type headers.//! version: 1.0.0//!//! [dependencies]//! mime_guess = { version = "2", default-features = false, features = ["rev-mappings"] }fn guess(name: &str) -> mime_guess::MimeGuess {    let name = name.trim();    // A bare extension ("css", ".css") is taken as one.    if !name.contains('.') || (name.starts_with('.') && name[1..].find('.').is_none()) {        mime_guess::from_ext(name.trim_start_matches('.'))    } else {        mime_guess::from_path(name)    }}/// The MIME type of a file name, a path or a bare extension ("report.pdf", "css"), in any case: "application/octet-stream" when it is unknown.pub fn mime_of(name: &str) -> String {    guess(name).first_or_octet_stream().essence_str().to_string()}/// Every MIME type a name may have, the usual one first: `[]` when it is unknown.pub fn mime_all(name: &str) -> Vec<String> {    guess(name).iter().map(|m| m.essence_str().to_string()).collect()}/// The extensions of a MIME type ("image/jpeg" gives jpe, jpeg, jpg), without dots: `[]` for an unknown type.pub fn mime_extensions(mime: &str) -> Vec<String> {    let m = mime.split(';').next().unwrap_or("").trim().to_lowercase();    mime_guess::get_mime_extensions_str(&m).map(|e| e.iter().map(|s| s.to_string()).collect()).unwrap_or_default()}/// A Content-Type header for a file name: its MIME type, with "; charset=utf-8" added for text, JSON, JavaScript, XML and SVG.pub fn mime_content_type(name: &str) -> String {    let m = guess(name).first_or_octet_stream();    let textual = m.type_() == "text" || matches!(m.subtype().as_str(), "json" | "javascript" | "xml" | "svg") || m.suffix().map(|s| s.as_str() == "json" || s.as_str() == "xml").unwrap_or(false);    if textual { format!("{}; charset=utf-8", m.essence_str()) } else { m.essence_str().to_string() }}
Thread
87Useful? Like it, or add to its thread below.

Thread 0 posts

On MIME types: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.