Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
double-quoted HTML attributes or CSS strings; this keeps text formats like SVG short and readable
- `DataUrl::write_to()`, which writes a data URL to any `std::io::Write` without building it as a string first
- `DataUrl::encoded_len()`, the exact length of the serialized data URL, e.g. for preallocating buffers
- `extension_for_media_type()`, the counterpart of `detect_media_type()`, which suggests a file extension
for a media type (e.g. `svg` for `image/svg+xml`)
- CLI options:
- `-s`/`--shortest` and `-m`/`--minimal`, for `DataEncoding::Shortest` and `Escaping::Minimal`
- `-i` can be given several times, which outputs one data URL per line
- `--size` prints the lengths of the percent-encoded and base64-encoded data URLs instead of the data URL
- `-O`/`--output-dir` decodes into files named after the input files, with extensions matching their media types

### Changed

Expand Down
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,31 @@ cat file.txt | dataurl -i - -o - | dataurl -d
cat file.png | dataurl -b
```

```console
dataurl -s -m -i logo.svg -i photo.jpg -i font.woff2 > urls.txt
```

```console
dataurl --size -i logo.svg -i photo.jpg
```

```console
dataurl -d -O assets/ -i logo.txt -i photo.txt
```

### Flags and options

- `-b`: Encode data using base64
- `-s`: Use base64 or percent-encoding, whichever results in a shorter data URL
- `-m`: Only escape what has to be when percent-encoding (for data URLs that go inside double-quoted HTML attributes or CSS strings)
- `-c`: Use custom `charset` (text given as an argument gets converted to it)
- `-d`: Attempt to parse input, output resulting data
- `-f`: Append `fragment`
- `-i`: Specify `file` to read data from (use `-` for STDIN)
- `-o`: Provide `file` to write output to (use `-` for STDOUT)
- `-t`: Adjust `media type` (detected automatically for files and STDIN)
- `-i`: Specify `file` to read data from (use `-` for STDIN); can be given several times, for one data URL per line
- `-o`: Provide `file` to write output to (use `-` for STDOUT)
- `--size`: Print the lengths of the percent-encoded and base64-encoded data URLs instead, tab-separated, followed by the file name (if any)
- `-d`: Attempt to parse input, output resulting data
- `-O`: Decode into files in `directory`, named after the input files, with extensions matching their media types (the names written get printed)


---------------------------------------------------
Expand Down Expand Up @@ -136,6 +152,15 @@ assert_eq!(detect_media_type(b"\x89PNG\r\n\x1a\n", None), Some("image/png"));
assert_eq!(detect_media_type(b"body{}", Some("style.css")), Some("text/css"));
```

`extension_for_media_type()` goes the other way, e.g. for saving decoded data to files:

```rust
use dataurl::extension_for_media_type;

assert_eq!(extension_for_media_type("image/svg+xml"), Some("svg"));
assert_eq!(extension_for_media_type("application/javascript"), Some("js"));
```

If all you need is parsing, [data-url](https://crates.io/crates/data-url) (from the Servo project) is another spec-compliant option;
dataurl also generates data URLs (picking the shortest encoding if asked to), handles charsets, detects media types, and comes with a CLI tool.

Expand Down
189 changes: 129 additions & 60 deletions src/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,125 @@ use infer::MatcherType;

const SVG: &str = "image/svg+xml";

/// Media types for file extensions (lowercase), using the names browsers expect.
/// Media types and their file extensions (lowercase, the preferred one first),
/// using the media type names browsers expect.
/// This is also used to normalize what infer returns, since some of its names are legacy ones
/// (e.g. application/font-woff for both WOFF and WOFF2).
const MEDIA_TYPES: &[(&str, &[&str])] = &[
// Images
("image/apng", &["apng"]),
("image/avif", &["avif"]),
("image/bmp", &["bmp"]),
("image/gif", &["gif"]),
("image/x-icon", &["ico", "cur"]),
("image/jpeg", &["jpg", "jpeg", "jfif", "pjpeg", "pjp"]),
("image/jxl", &["jxl"]),
("image/png", &["png"]),
(SVG, &["svg", "svgz"]),
("image/tiff", &["tif", "tiff"]),
("image/webp", &["webp"]),
// Audio
("audio/aac", &["aac"]),
("audio/flac", &["flac"]),
("audio/mp4", &["m4a"]),
("audio/midi", &["mid", "midi"]),
("audio/mpeg", &["mp3"]),
("audio/ogg", &["oga", "ogg"]),
("audio/opus", &["opus"]),
("audio/wav", &["wav"]),
("audio/webm", &["weba"]),
// Video
("video/avi", &["avi"]),
("video/mp4", &["mp4", "m4v"]),
("video/x-matroska", &["mkv"]),
("video/quicktime", &["mov"]),
("video/mpeg", &["mpeg", "mpg"]),
("video/ogg", &["ogv"]),
("video/webm", &["webm"]),
// Fonts
("application/vnd.ms-fontobject", &["eot"]),
("font/otf", &["otf"]),
("font/ttf", &["ttf"]),
("font/woff", &["woff"]),
("font/woff2", &["woff2"]),
// Text and documents
("text/css", &["css"]),
("text/csv", &["csv"]),
("text/html", &["html", "htm"]),
("text/calendar", &["ics"]),
("text/javascript", &["js", "mjs", "cjs"]),
("application/json", &["json", "map"]),
("application/ld+json", &["jsonld"]),
("text/markdown", &["md", "markdown"]),
("application/pdf", &["pdf"]),
("application/rss+xml", &["rss"]),
("text/plain", &["txt"]),
("text/vtt", &["vtt"]),
("application/manifest+json", &["webmanifest"]),
("application/xhtml+xml", &["xhtml", "xht"]),
("text/xml", &["xml", "xsl"]),
// Other
("application/x-shockwave-flash", &["swf"]),
("application/wasm", &["wasm"]),
("application/zip", &["zip"]),
];

/// Legacy and alternative names for media types in [`MEDIA_TYPES`]
const ALIASES: &[(&str, &str)] = &[
("application/javascript", "text/javascript"),
("application/x-javascript", "text/javascript"),
("text/ecmascript", "text/javascript"),
("application/xml", "text/xml"),
("image/jpg", "image/jpeg"),
("image/pjpeg", "image/jpeg"),
("image/vnd.microsoft.icon", "image/x-icon"),
("audio/mp3", "audio/mpeg"),
("audio/x-wav", "audio/wav"),
("audio/wave", "audio/wav"),
("audio/x-flac", "audio/flac"),
("video/x-msvideo", "video/avi"),
("application/font-woff", "font/woff"),
("application/x-font-ttf", "font/ttf"),
("application/x-font-otf", "font/otf"),
];

fn media_type_for_extension(extension: &str) -> Option<&'static str> {
Some(match extension {
// Images
"apng" => "image/apng",
"avif" => "image/avif",
"bmp" => "image/bmp",
"gif" => "image/gif",
"ico" | "cur" => "image/x-icon",
"jpg" | "jpeg" | "jfif" | "pjpeg" | "pjp" => "image/jpeg",
"jxl" => "image/jxl",
"png" => "image/png",
"svg" | "svgz" => SVG,
"tif" | "tiff" => "image/tiff",
"webp" => "image/webp",
// Audio
"aac" => "audio/aac",
"flac" => "audio/flac",
"m4a" => "audio/mp4",
"mid" | "midi" => "audio/midi",
"mp3" => "audio/mpeg",
"oga" | "ogg" => "audio/ogg",
"opus" => "audio/opus",
"wav" => "audio/wav",
"weba" => "audio/webm",
// Video
"avi" => "video/avi",
"m4v" | "mp4" => "video/mp4",
"mkv" => "video/x-matroska",
"mov" => "video/quicktime",
"mpeg" | "mpg" => "video/mpeg",
"ogv" => "video/ogg",
"webm" => "video/webm",
// Fonts
"eot" => "application/vnd.ms-fontobject",
"otf" => "font/otf",
"ttf" => "font/ttf",
"woff" => "font/woff",
"woff2" => "font/woff2",
// Text and documents
"css" => "text/css",
"csv" => "text/csv",
"htm" | "html" => "text/html",
"ics" => "text/calendar",
"js" | "mjs" | "cjs" => "text/javascript",
"json" | "map" => "application/json",
"jsonld" => "application/ld+json",
"md" | "markdown" => "text/markdown",
"pdf" => "application/pdf",
"rss" => "application/rss+xml",
"txt" => "text/plain",
"vtt" => "text/vtt",
"webmanifest" => "application/manifest+json",
"xhtml" | "xht" => "application/xhtml+xml",
"xml" | "xsl" => "text/xml",
// Other
"swf" => "application/x-shockwave-flash",
"wasm" => "application/wasm",
"zip" => "application/zip",
_ => return None,
})
MEDIA_TYPES
.iter()
.find(|(_, extensions)| extensions.contains(&extension))
.map(|&(media_type, _)| media_type)
}

/// Suggests a file extension (without the dot) for a media type, e.g. `"png"` for `"image/png"`.
///
/// Matching is case-insensitive, parameters are ignored, and common legacy names
/// (like `application/javascript` or `image/jpg`) are recognized too.
/// This is the counterpart of [`detect_media_type()`], meant for saving decoded data to files.
///
/// ```
/// use dataurl::extension_for_media_type;
///
/// assert_eq!(extension_for_media_type("image/png"), Some("png"));
/// assert_eq!(extension_for_media_type("Image/SVG+XML"), Some("svg"));
/// assert_eq!(extension_for_media_type("text/html; charset=utf-8"), Some("html"));
/// assert_eq!(extension_for_media_type("application/javascript"), Some("js"));
/// assert_eq!(extension_for_media_type("x/unknown"), None);
/// ```
pub fn extension_for_media_type(media_type: &str) -> Option<&'static str> {
let essence = media_type
.split(';')
.next()
.unwrap_or("")
.trim()
.to_ascii_lowercase();
let essence = ALIASES
.iter()
.find(|(alias, _)| *alias == essence)
.map_or(essence.as_str(), |&(_, media_type)| media_type);
MEDIA_TYPES
.iter()
.find(|(media_type, _)| *media_type == essence)
.map(|(_, extensions)| extensions[0])
}

fn file_extension(file_name: &str) -> Option<String> {
Expand Down Expand Up @@ -159,6 +215,19 @@ mod tests {
assert_eq!(file_extension("trailing."), None);
}

#[test]
fn every_media_type_maps_back_to_its_extensions() {
for &(media_type, extensions) in MEDIA_TYPES {
assert_eq!(extension_for_media_type(media_type), Some(extensions[0]));
for extension in extensions {
assert_eq!(media_type_for_extension(extension), Some(media_type));
}
}
for &(alias, media_type) in ALIASES {
assert!(MEDIA_TYPES.iter().any(|&(m, _)| m == media_type), "{alias}");
}
}

#[test]
fn svg_tag_needs_a_delimiter() {
assert!(starts_with_svg_tag(b"<svg>"));
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ mod detect;
pub mod mime;
mod percent;

pub use detect::detect_media_type;
pub use detect::{detect_media_type, extension_for_media_type};
pub use mime::MimeType;

use encoding_rs::{Encoding, REPLACEMENT, UTF_16BE, UTF_16LE, WINDOWS_1252};
Expand Down
Loading
Loading