From 39fc3e9c72f0b8312889e269023fed5a88b429c2 Mon Sep 17 00:00:00 2001 From: Chris Rogus Date: Tue, 16 Apr 2024 19:16:39 -0600 Subject: [PATCH] add file size to FileEngine --- packages/html/src/file_data.rs | 8 ++++++++ packages/html/src/native_bind/native_file_engine.rs | 5 +++++ packages/web/src/file_engine.rs | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/packages/html/src/file_data.rs b/packages/html/src/file_data.rs index b15eaab2bb..cb1492e307 100644 --- a/packages/html/src/file_data.rs +++ b/packages/html/src/file_data.rs @@ -20,6 +20,11 @@ impl FileEngine for SerializedFileEngine { self.files.keys().cloned().collect() } + async fn file_size(&self, file: &str) -> Option { + let file = self.files.get(file)?; + Some(file.len() as u64) + } + async fn read_file(&self, file: &str) -> Option> { self.files.get(file).cloned() } @@ -42,6 +47,9 @@ pub trait FileEngine { // get a list of file names fn files(&self) -> Vec; + // get the size of a file + async fn file_size(&self, file: &str) -> Option; + // read a file to bytes async fn read_file(&self, file: &str) -> Option>; diff --git a/packages/html/src/native_bind/native_file_engine.rs b/packages/html/src/native_bind/native_file_engine.rs index 81070bf0d7..1301051b2a 100644 --- a/packages/html/src/native_bind/native_file_engine.rs +++ b/packages/html/src/native_bind/native_file_engine.rs @@ -25,6 +25,11 @@ impl FileEngine for NativeFileEngine { .collect() } + async fn file_size(&self, file: &str) -> Option { + let file = File::open(file).await.ok()?; + Some(file.metadata().await.ok()?.len()) + } + async fn read_file(&self, file: &str) -> Option> { let mut file = File::open(file).await.ok()?; diff --git a/packages/web/src/file_engine.rs b/packages/web/src/file_engine.rs index f98f726e24..50968ffe8e 100644 --- a/packages/web/src/file_engine.rs +++ b/packages/web/src/file_engine.rs @@ -42,6 +42,11 @@ impl FileEngine for WebFileEngine { .collect() } + async fn file_size(&self, file: &str) -> Option { + let file = self.find(file)?; + Some(file.size() as u64) + } + // read a file to bytes async fn read_file(&self, file: &str) -> Option> { let file = self.find(file)?;