1use json::{JsonValue, object};
2
3#[derive(Clone, Debug)]
4pub struct ContentType {
5 pub content_type: String,
6 pub suffix: String,
7}
8
9impl ContentType {
10 pub fn from(content_type: &str) -> Self {
11 let data = ContentType::type_suffix();
12 let suffix = data[content_type].as_str().unwrap_or("");
13 let suffix = suffix.to_lowercase();
14 let suffix = suffix.as_str();
15 Self {
16 content_type: content_type.to_string(),
17 suffix: suffix.to_string(),
18 }
19 }
20 pub fn from_suffix(suffix: &str) -> Self {
21 let suffix = suffix.to_lowercase();
22 let suffix = suffix.as_str();
23 let data = ContentType::suffix_type();
24
25 let content_type = data[suffix].as_str().unwrap_or("");
26 Self {
27 content_type: content_type.to_string(),
28 suffix: suffix.to_string(),
29 }
30 }
31 fn type_suffix() -> JsonValue {
32 object! {
33 "image/png" => "png",
34 "image/jpeg" => "jpeg",
35 "image/jpg" => "jpg",
36 "video/mp4" => "mp4",
37 "image/gif"=>"gif",
38 "image/tiff"=>"tiff",
39 "image/tif"=>"tif",
40 "image/bmp"=>"bmp",
41 "image/avif" => "avif",
42 "image/webp" => "webp",
43 "application/pdf" => "pdf",
44 "application/zip" => "zip",
45 "application/x-msdos-program" => "exe",
46 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "xlsx",
47 "application/vnd.ms-excel" => "xls",
48 "application/vnd.ms-excel.sheet.macroenabled.12" => "xlsm",
49 "application/vnd.apple.numbers" => "numbers",
50 "image/x-icon" => "ico",
51 "image/vnd.dwg" => "dwg",
52 "application/xml" => "xml",
53 "text/xml" => "xml",
54 "text/markdown" => "md",
55 "image/heic" => "heic",
56 "text/csv" => "csv",
57 "text/html" => "html",
58 "image/svg+xml" => "svg",
59 "application/msword" => "doc",
60 "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "docx",
61
62 "application/x-www-form-urlencoded" => "x-www-form-urlencoded",
63 "application/javascript" => "js",
64 "multipart/form-data" => "form-data",
65 "application/json" => "json",
66 "text/plain" => "txt"
67 }
68 }
69 fn suffix_type() -> JsonValue {
70 let mut data = object! {};
71 let res = ContentType::type_suffix();
72 for (content_type, suffix) in res.entries() {
73 data[suffix.as_str().unwrap()] = content_type.to_string().into();
74 }
75 data
76 }
77 pub fn suffix_type_name(self) -> &'static str {
78 match self.suffix.as_str() {
79 "png" | "jpeg" | "jpg" | "ico" | "heic" | "svg" |
80 "avif" | "webp" | "gif" | "tiff" | "tif" | "bmp" => "图片",
81 "txt" | "xml" | "md" | "doc" | "docx" => "文档",
82 "pdf" => "PDF",
83 "numbers" | "xlsx" | "xlsm" | "csv" | "xls" => "表格",
84 "zip" => "压缩包",
85 "log" => "日志",
86 "dwg" => "图纸",
87 "mp3" | "mp4" => "视频",
88 "wav" | "mpeg" => "音频",
89 "exe" => "程序",
90 "html" => "网页",
91 _ => "未知"
92 }
93 }
94}