mirror of
https://github.com/rust-lang-nursery/rust-cookbook
synced 2024-11-21 19:13:07 +00:00
Mime type from filename
This commit is contained in:
commit
4b1ed71d95
5 changed files with 45 additions and 0 deletions
0
libtest.rmeta
Normal file
0
libtest.rmeta
Normal file
|
@ -32,4 +32,5 @@
|
||||||
[ex-toml-config]: encoding/complex.html#deserialize-a-toml-configuration-file
|
[ex-toml-config]: encoding/complex.html#deserialize-a-toml-configuration-file
|
||||||
[ex-byteorder-le]: encoding/complex.html#read-and-write-integers-in-little-endian-byte-order
|
[ex-byteorder-le]: encoding/complex.html#read-and-write-integers-in-little-endian-byte-order
|
||||||
|
|
||||||
|
|
||||||
{{#include links.md}}
|
{{#include links.md}}
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
| Recipe | Crates | Categories |
|
| Recipe | Crates | Categories |
|
||||||
|--------|--------|------------|
|
|--------|--------|------------|
|
||||||
| [Get MIME type from string][ex-mime-from-string] | [![mime-badge]][mime] | [![cat-encoding-badge]][cat-encoding] |
|
| [Get MIME type from string][ex-mime-from-string] | [![mime-badge]][mime] | [![cat-encoding-badge]][cat-encoding] |
|
||||||
|
| [Get MIME type from filename][ex-mime-from-filename] | [![mime-badge]][mime] | [![cat-encoding-badge]][cat-encoding] |
|
||||||
| [Parse the MIME type of a HTTP response][ex-http-response-mime-type] | [![mime-badge]][mime] [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding] |
|
| [Parse the MIME type of a HTTP response][ex-http-response-mime-type] | [![mime-badge]][mime] [![reqwest-badge]][reqwest] | [![cat-net-badge]][cat-net] [![cat-encoding-badge]][cat-encoding] |
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,6 +40,7 @@
|
||||||
[ex-url-rm-frag]: web/url.html#remove-fragment-identifiers-and-query-pairs-from-a-url
|
[ex-url-rm-frag]: web/url.html#remove-fragment-identifiers-and-query-pairs-from-a-url
|
||||||
|
|
||||||
[ex-mime-from-string]: web/mime.html#get-mime-type-from-string
|
[ex-mime-from-string]: web/mime.html#get-mime-type-from-string
|
||||||
|
[ex-mime-from-filename]: web/mime.html#get-mime-type-from-filename
|
||||||
[ex-http-response-mime-type]: web/mime.html#parse-the-mime-type-of-a-http-response
|
[ex-http-response-mime-type]: web/mime.html#parse-the-mime-type-of-a-http-response
|
||||||
|
|
||||||
{{#include links.md}}
|
{{#include links.md}}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
{{#include mime/string.md}}
|
{{#include mime/string.md}}
|
||||||
|
|
||||||
|
{{#include mime/filename.md}}
|
||||||
|
|
||||||
{{#include mime/request.md}}
|
{{#include mime/request.md}}
|
||||||
|
|
||||||
{{#include ../links.md}}
|
{{#include ../links.md}}
|
||||||
|
|
40
src/web/mime/filename.md
Normal file
40
src/web/mime/filename.md
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
## Get MIME type from filename
|
||||||
|
|
||||||
|
[![mime-badge]][mime] [![cat-encoding-badge]][cat-encoding]
|
||||||
|
|
||||||
|
The following example shows how to return the correct MIME type from a given
|
||||||
|
filename using the [mime] crate. The program will check for file extensions
|
||||||
|
and match against a known list. The return value is [`mime:Mime`].
|
||||||
|
|
||||||
|
```rust
|
||||||
|
extern crate mime;
|
||||||
|
use mime::Mime;
|
||||||
|
|
||||||
|
fn find_mimetype (filename : &String) -> Mime{
|
||||||
|
|
||||||
|
let parts : Vec<&str> = filename.split('.').collect();
|
||||||
|
|
||||||
|
let res = match parts.last() {
|
||||||
|
Some(v) =>
|
||||||
|
match *v {
|
||||||
|
"png" => mime::IMAGE_PNG,
|
||||||
|
"jpg" => mime::IMAGE_JPEG,
|
||||||
|
"json" => mime::APPLICATION_JSON,
|
||||||
|
&_ => mime::TEXT_PLAIN,
|
||||||
|
},
|
||||||
|
None => mime::TEXT_PLAIN,
|
||||||
|
};
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let filenames = vec!("foobar.jpg", "foo.bar", "foobar.png");
|
||||||
|
for file in filenames {
|
||||||
|
let mime = find_mimetype(&file.to_owned());
|
||||||
|
println!("MIME for {}: {}", file, mime);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
[`mime:Mime`]: https://docs.rs/mime/*/mime/struct.Mime.html
|
Loading…
Reference in a new issue