mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-30 17:48:50 +02:00
feat(dialog): set len and modified_at fields in FileResponse on desktop (#1295)
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"dialog": "patch"
|
||||||
|
"dialog-js": "patch"
|
||||||
|
---
|
||||||
|
|
||||||
|
Fill file `len` and `modified_at` fields of `FileResponse` when using the open dialog.
|
||||||
@@ -18,6 +18,7 @@ use tauri::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
fs,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::mpsc::sync_channel,
|
sync::mpsc::sync_channel,
|
||||||
};
|
};
|
||||||
@@ -213,7 +214,7 @@ impl<R: Runtime> MessageDialogBuilder<R> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize, Default)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct FileResponse {
|
pub struct FileResponse {
|
||||||
pub base64_data: Option<String>,
|
pub base64_data: Option<String>,
|
||||||
@@ -230,16 +231,18 @@ pub struct FileResponse {
|
|||||||
impl FileResponse {
|
impl FileResponse {
|
||||||
#[cfg(desktop)]
|
#[cfg(desktop)]
|
||||||
fn new(path: PathBuf) -> Self {
|
fn new(path: PathBuf) -> Self {
|
||||||
|
let metadata = fs::metadata(&path);
|
||||||
|
let metadata = metadata.as_ref();
|
||||||
Self {
|
Self {
|
||||||
base64_data: None,
|
base64_data: None,
|
||||||
duration: None,
|
duration: None,
|
||||||
height: None,
|
height: None,
|
||||||
width: None,
|
width: None,
|
||||||
mime_type: 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()),
|
name: path.file_name().map(|f| f.to_string_lossy().into_owned()),
|
||||||
path,
|
path,
|
||||||
size: 0,
|
size: metadata.map(|m| m.len()).unwrap_or(0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -574,3 +577,18 @@ impl<R: Runtime> FileDialogBuilder<R> {
|
|||||||
blocking_fn!(self, save_file)
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user