~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git

rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.


8d4838e1 BBaoVanC

2 years ago
Add mime-guess feature to store mimetype as metadata
Files changed (5) hide show
  1. Cargo.toml +1 -0
  2. impl/Cargo.toml +1 -0
  3. impl/src/lib.rs +7 -1
  4. utils/Cargo.toml +2 -0
  5. utils/src/lib.rs +20 -0
Cargo.toml CHANGED
@@ -79,6 +79,7 @@ sha2 = "0.10"
79
79
  debug-embed = ["rust-embed-impl/debug-embed", "rust-embed-utils/debug-embed"]
80
80
  interpolate-folder-path = ["rust-embed-impl/interpolate-folder-path"]
81
81
  compression = ["rust-embed-impl/compression", "include-flate"]
82
+ mime-guess = ["rust-embed-impl/mime-guess"]
82
83
  include-exclude = ["rust-embed-impl/include-exclude", "rust-embed-utils/include-exclude"]
83
84
  actix = ["actix-web", "mime_guess"]
84
85
  warp-ex = ["warp", "tokio", "mime_guess"]
impl/Cargo.toml CHANGED
@@ -30,4 +30,5 @@ optional = true
30
30
  debug-embed = []
31
31
  interpolate-folder-path = ["shellexpand"]
32
32
  compression = []
33
+ mime-guess = ["rust-embed-utils/mime-guess"]
33
34
  include-exclude = ["rust-embed-utils/include-exclude"]
impl/src/lib.rs CHANGED
@@ -174,6 +174,12 @@ fn embed_file(rel_path: &str, full_canonical_path: &str) -> TokenStream2 {
174
174
  Some(last_modified) => quote! { Some(#last_modified) },
175
175
  None => quote! { None },
176
176
  };
177
+ let mimetype = if cfg!(feature = "mime-guess") {
178
+ let mt = file.metadata.mimetype().to_string();
179
+ quote! { #mt }
180
+ } else {
181
+ quote! { None }
182
+ };
177
183
 
178
184
  let embedding_code = if cfg!(feature = "compression") {
179
185
  quote! {
@@ -192,7 +198,7 @@ fn embed_file(rel_path: &str, full_canonical_path: &str) -> TokenStream2 {
192
198
 
193
199
  Some(rust_embed::EmbeddedFile {
194
200
  data: std::borrow::Cow::from(bytes),
195
- metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified)
201
+ metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified, #mimetype)
196
202
  })
197
203
  }
198
204
  }
utils/Cargo.toml CHANGED
@@ -14,6 +14,7 @@ edition = "2018"
14
14
  [dependencies]
15
15
  walkdir = "2.3.1"
16
16
  sha2 = "0.10.5"
17
+ mime_guess = { version = "2.0.4", optional = true }
17
18
 
18
19
  [dependencies.globset]
19
20
  version = "0.4.8"
@@ -21,4 +22,5 @@ optional = true
21
22
 
22
23
  [features]
23
24
  debug-embed = []
25
+ mime-guess = ["mime_guess"]
24
26
  include-exclude = ["globset"]
utils/src/lib.rs CHANGED
@@ -87,14 +87,23 @@ pub struct EmbeddedFile {
87
87
  pub struct Metadata {
88
88
  hash: [u8; 32],
89
89
  last_modified: Option<u64>,
90
+ #[cfg(feature = "mime-guess")]
91
+ mimetype: Cow<'static, str>,
90
92
  }
91
93
 
92
94
  impl Metadata {
93
95
  #[doc(hidden)]
96
+ #[cfg(not(feature = "mime-guess"))]
94
97
  pub fn __rust_embed_new(hash: [u8; 32], last_modified: Option<u64>) -> Self {
95
98
  Self { hash, last_modified }
96
99
  }
97
100
 
101
+ #[doc(hidden)]
102
+ #[cfg(feature = "mime-guess")]
103
+ pub fn __rust_embed_new(hash: [u8; 32], last_modified: Option<u64>, mimetype: &'static str) -> Self {
104
+ Self { hash, last_modified, mimetype: mimetype.into() }
105
+ }
106
+
98
107
  /// The SHA256 hash of the file
99
108
  pub fn sha256_hash(&self) -> [u8; 32] {
100
109
  self.hash
@@ -105,6 +114,12 @@ impl Metadata {
105
114
  pub fn last_modified(&self) -> Option<u64> {
106
115
  self.last_modified
107
116
  }
117
+
118
+ /// The mime type of the file
119
+ #[cfg(feature = "mime-guess")]
120
+ pub fn mimetype(&self) -> &Cow<str> {
121
+ &self.mimetype
122
+ }
108
123
  }
109
124
 
110
125
  pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
@@ -127,11 +142,16 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
127
142
  .as_secs()
128
143
  });
129
144
 
145
+ #[cfg(feature = "mime-guess")]
146
+ let mimetype = mime_guess::from_path(file_path).first_or_octet_stream().to_string();
147
+
130
148
  Ok(EmbeddedFile {
131
149
  data,
132
150
  metadata: Metadata {
133
151
  hash,
134
152
  last_modified: source_date_epoch.or(last_modified),
153
+ #[cfg(feature = "mime-guess")]
154
+ mimetype: mimetype.into(),
135
155
  },
136
156
  })
137
157
  }