-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathmime.rs
More file actions
110 lines (104 loc) · 4.28 KB
/
mime.rs
File metadata and controls
110 lines (104 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2022 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.
// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.
/// MIME type recorded on the top-level claim for ingredient working-store archives.
///
/// See [`Builder::write_ingredient_archive`](crate::Builder::write_ingredient_archive).
pub const INGREDIENT_ARCHIVE_MIME: &str = "application/vnd.contentauth.c2pa.ingredient";
/// Converts a file extension to a MIME type
pub fn extension_to_mime(extension: &str) -> Option<&'static str> {
Some(match extension.to_lowercase().as_str() {
"jpg" | "jpeg" => "image/jpeg",
"png" => "image/png",
"gif" => "image/gif",
"psd" => "image/vnd.adobe.photoshop",
"tiff" | "tif" => "image/tiff",
"svg" => "image/svg+xml",
"ico" => "image/x-icon",
"bmp" => "image/bmp",
"webp" => "image/webp",
"dng" => "image/x-adobe-dng",
"heic" => "image/heic",
"heif" => "image/heif",
"mp2" | "mpa" | "mpe" | "mpeg" | "mpg" | "mpv2" => "video/mpeg",
"mp4" => "video/mp4",
"avi" => "video/avi",
"avif" => "image/avif",
"mov" | "qt" => "video/quicktime",
"m4a" => "audio/mp4",
"mid" | "rmi" => "audio/mid",
"mp3" => "audio/mpeg",
"flac" => "audio/flac",
"wav" => "audio/wav",
"aif" | "aifc" | "aiff" => "audio/aiff",
"ogg" => "audio/ogg",
"pdf" => "application/pdf",
"ai" => "application/postscript",
"arw" => "image/x-sony-arw",
"nef" => "image/x-nikon-nef",
"c2pa" | "application/x-c2pa-manifest-store" | "application/c2pa" => "application/c2pa",
_ => return None,
})
}
/// Convert a format to a MIME type
/// formats can be passed in as extensions, e.g. "jpg" or "jpeg"
/// or as MIME types, e.g. "image/jpeg"
pub fn format_to_mime(format: &str) -> String {
match extension_to_mime(format) {
Some(mime) => mime,
None => format,
}
.to_string()
}
/// Converts a format to a file extension
#[cfg(feature = "file_io")]
pub fn format_to_extension(format: &str) -> Option<&'static str> {
Some(match format.to_lowercase().as_str() {
"jpg" | "jpeg" | "image/jpeg" => "jpg",
"png" | "image/png" => "png",
"gif" | "image/gif" => "gif",
"psd" | "image/vnd.adobe.photoshop" => "psd",
"tiff" | "tif" | "image/tiff" => "tiff",
"svg" | "image/svg+xml" => "svg",
"ico" | "image/x-icon" => "ico",
"bmp" | "image/bmp" => "bmp",
"webp" | "image/webp" => "webp",
"dng" | "image/dng" => "dng",
"heic" | "image/heic" => "heic",
"heif" | "image/heif" => "heif",
"mp2" | "mpa" | "mpe" | "mpeg" | "mpg" | "mpv2" | "video/mpeg" => "mp2",
"mp4" | "video/mp4" => "mp4",
"avif" | "image/avif" => "avif",
"avi" | "video/avi" => "avi",
"mov" | "qt" | "video/quicktime" => "mov",
"m4a" | "audio/mp4" => "m4a",
"mid" | "rmi" | "audio/mid" => "mid",
"mp3" | "audio/mpeg" => "mp3",
"flac" | "audio/flac" => "flac",
"wav" | "audio/wav" | "audio/wave" | "audio.vnd.wave" => "wav",
"aif" | "aifc" | "aiff" | "audio/aiff" => "aif",
"ogg" | "audio/ogg" => "ogg",
"pdf" | "application/pdf" => "pdf",
"ai" | "application/postscript" => "ai",
"arw" | "image/x-sony-arw" => "arw",
"nef" | "image/x-nikon-nef" => "nef",
"c2pa" | "application/x-c2pa-manifest-store" | "application/c2pa" => "c2pa",
_ => return None,
})
}
/// Return a MIME type given a file path.
///
/// This function will use the file extension to determine the MIME type.
pub fn format_from_path<P: AsRef<std::path::Path>>(path: P) -> Option<String> {
path.as_ref().extension().map(|ext| {
crate::utils::mime::format_to_mime(ext.to_string_lossy().to_lowercase().as_ref())
})
}