Skip to content

Commit

Permalink
add file size to FileEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
rogusdev committed May 28, 2024
1 parent b3cd4fa commit 39fc3e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/html/src/file_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ impl FileEngine for SerializedFileEngine {
self.files.keys().cloned().collect()
}

async fn file_size(&self, file: &str) -> Option<u64> {
let file = self.files.get(file)?;
Some(file.len() as u64)
}

async fn read_file(&self, file: &str) -> Option<Vec<u8>> {
self.files.get(file).cloned()
}
Expand All @@ -42,6 +47,9 @@ pub trait FileEngine {
// get a list of file names
fn files(&self) -> Vec<String>;

// get the size of a file
async fn file_size(&self, file: &str) -> Option<u64>;

// read a file to bytes
async fn read_file(&self, file: &str) -> Option<Vec<u8>>;

Expand Down
5 changes: 5 additions & 0 deletions packages/html/src/native_bind/native_file_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ impl FileEngine for NativeFileEngine {
.collect()
}

async fn file_size(&self, file: &str) -> Option<u64> {
let file = File::open(file).await.ok()?;
Some(file.metadata().await.ok()?.len())
}

async fn read_file(&self, file: &str) -> Option<Vec<u8>> {
let mut file = File::open(file).await.ok()?;

Expand Down
5 changes: 5 additions & 0 deletions packages/web/src/file_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ impl FileEngine for WebFileEngine {
.collect()
}

async fn file_size(&self, file: &str) -> Option<u64> {
let file = self.find(file)?;
Some(file.size() as u64)
}

// read a file to bytes
async fn read_file(&self, file: &str) -> Option<Vec<u8>> {
let file = self.find(file)?;
Expand Down

0 comments on commit 39fc3e9

Please sign in to comment.