Skip to content

Commit

Permalink
Merge pull request #225 from ngalaiko/master
Browse files Browse the repository at this point in the history
include created into file metadata
  • Loading branch information
pyrossh committed Dec 5, 2023
2 parents 207d1be + d308513 commit 91899db
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
6 changes: 5 additions & 1 deletion impl/src/lib.rs
Expand Up @@ -209,6 +209,10 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful
Some(last_modified) => quote! { Some(#last_modified) },
None => quote! { None },
};
let created = match file.metadata.created() {
Some(created) => quote! { Some(#created) },
None => quote! { None },
};
#[cfg(feature = "mime-guess")]
let mimetype_tokens = {
let mt = file.metadata.mimetype();
Expand Down Expand Up @@ -241,7 +245,7 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful

rust_embed::EmbeddedFile {
data: std::borrow::Cow::Borrowed(&BYTES),
metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified #mimetype_tokens)
metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified, #created #mimetype_tokens)
}
}
})
Expand Down
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -55,6 +55,7 @@ pub struct EmbeddedFile {
pub struct Metadata {
hash: [u8; 32],
last_modified: Option<u64>,
created: Option<u64>,
}
```

Expand Down
10 changes: 10 additions & 0 deletions tests/metadata.rs
Expand Up @@ -25,3 +25,13 @@ fn last_modified_is_accurate() {

assert_eq!(index_file.metadata.last_modified(), Some(expected_datetime_utc));
}

#[test]
fn create_is_accurate() {
let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");

let metadata = fs::metadata(format!("{}/examples/public/index.html", env!("CARGO_MANIFEST_DIR"))).unwrap();
let expected_datetime_utc = metadata.created().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();

assert_eq!(index_file.metadata.created(), Some(expected_datetime_utc));
}
22 changes: 20 additions & 2 deletions utils/src/lib.rs
Expand Up @@ -89,16 +89,20 @@ pub struct EmbeddedFile {
pub struct Metadata {
hash: [u8; 32],
last_modified: Option<u64>,
created: Option<u64>,
#[cfg(feature = "mime-guess")]
mimetype: Cow<'static, str>,
}

impl Metadata {
#[doc(hidden)]
pub const fn __rust_embed_new(hash: [u8; 32], last_modified: Option<u64>, #[cfg(feature = "mime-guess")] mimetype: &'static str) -> Self {
pub const fn __rust_embed_new(
hash: [u8; 32], last_modified: Option<u64>, created: Option<u64>, #[cfg(feature = "mime-guess")] mimetype: &'static str,
) -> Self {
Self {
hash,
last_modified,
created,
#[cfg(feature = "mime-guess")]
mimetype: Cow::Borrowed(mimetype),
}
Expand All @@ -115,6 +119,12 @@ impl Metadata {
self.last_modified
}

/// The created data in seconds since the UNIX epoch. If the underlying
/// platform/file-system does not support this, None is returned.
pub fn created(&self) -> Option<u64> {
self.created
}

/// The mime type of the file
#[cfg(feature = "mime-guess")]
pub fn mimetype(&self) -> &str {
Expand All @@ -135,12 +145,19 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
Err(_) => None,
};

let last_modified = fs::metadata(file_path)?.modified().ok().map(|last_modified| {
let metadata = fs::metadata(file_path)?;
let last_modified = metadata.modified().ok().map(|last_modified| {
last_modified
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Time before the UNIX epoch is unsupported")
.as_secs()
});
let created = metadata.created().ok().map(|created| {
created
.duration_since(SystemTime::UNIX_EPOCH)
.expect("Time before the UNIX epoch is unsupported")
.as_secs()
});

#[cfg(feature = "mime-guess")]
let mimetype = mime_guess::from_path(file_path).first_or_octet_stream().to_string();
Expand All @@ -150,6 +167,7 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
metadata: Metadata {
hash,
last_modified: source_date_epoch.or(last_modified),
created: source_date_epoch.or(created),
#[cfg(feature = "mime-guess")]
mimetype: mimetype.into(),
},
Expand Down

0 comments on commit 91899db

Please sign in to comment.