Skip to content

Commit

Permalink
feat(dialog): set len and modified_at fields in FileResponse on…
Browse files Browse the repository at this point in the history
… desktop (#1295)
  • Loading branch information
luis-cicada authored May 13, 2024
1 parent 58330f9 commit cd57dcd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changes/dialog-metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"dialog": "patch"
"dialog-js": "patch"
---

Fill file `len` and `modified_at` fields of `FileResponse` when using the open dialog.
24 changes: 21 additions & 3 deletions plugins/dialog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tauri::{
};

use std::{
fs,
path::{Path, PathBuf},
sync::mpsc::sync_channel,
};
Expand Down Expand Up @@ -213,7 +214,7 @@ impl<R: Runtime> MessageDialogBuilder<R> {
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct FileResponse {
pub base64_data: Option<String>,
Expand All @@ -230,16 +231,18 @@ pub struct FileResponse {
impl FileResponse {
#[cfg(desktop)]
fn new(path: PathBuf) -> Self {
let metadata = fs::metadata(&path);
let metadata = metadata.as_ref();
Self {
base64_data: None,
duration: None,
height: None,
width: None,
mime_type: None,
modified_at: None,
modified_at: metadata.ok().and_then(|m| to_msec(m.modified())),
name: path.file_name().map(|f| f.to_string_lossy().into_owned()),
path,
size: 0,
size: metadata.map(|m| m.len()).unwrap_or(0),
}
}
}
Expand Down Expand Up @@ -574,3 +577,18 @@ impl<R: Runtime> FileDialogBuilder<R> {
blocking_fn!(self, save_file)
}
}

// taken from deno source code: https://github.com/denoland/deno/blob/ffffa2f7c44bd26aec5ae1957e0534487d099f48/runtime/ops/fs.rs#L913
#[inline]
fn to_msec(maybe_time: std::result::Result<std::time::SystemTime, std::io::Error>) -> Option<u64> {
match maybe_time {
Ok(time) => {
let msec = time
.duration_since(std::time::UNIX_EPOCH)
.map(|t| t.as_millis() as u64)
.unwrap_or_else(|err| err.duration().as_millis() as u64);
Some(msec)
}
Err(_) => None,
}
}

0 comments on commit cd57dcd

Please sign in to comment.