Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include created into file metadata #225

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion impl/src/lib.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading